useIndexedSpans.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type {Location} from 'history';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import type {Sort} from 'sentry/utils/discover/fields';
  4. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  5. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import type {
  8. SpanIndexedField,
  9. SpanIndexedFieldTypes,
  10. SpanMeasurements,
  11. } from 'sentry/views/starfish/types';
  12. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  13. interface Filters {
  14. [key: string]: string;
  15. }
  16. export const useIndexedSpans = ({
  17. filters,
  18. sorts,
  19. limit,
  20. enabled = true,
  21. referrer,
  22. fields,
  23. }: {
  24. fields: (SpanIndexedField | SpanMeasurements)[];
  25. filters: Filters;
  26. limit: number;
  27. referrer: string;
  28. sorts: Sort[];
  29. enabled?: boolean;
  30. }) => {
  31. const location = useLocation();
  32. const eventView = getEventView(filters, location, fields, sorts);
  33. return useSpansQuery<SpanIndexedFieldTypes[]>({
  34. eventView,
  35. limit,
  36. initialData: [],
  37. enabled,
  38. referrer,
  39. });
  40. };
  41. function getEventView(
  42. filters: Filters,
  43. location: Location,
  44. fields: (SpanIndexedField | SpanMeasurements)[],
  45. sorts?: Sort[]
  46. ) {
  47. // TODO: Add a `MutableSearch` constructor that accept a key-value mapping
  48. const search = new MutableSearch([]);
  49. for (const filterName in filters) {
  50. search.addFilterValue(filterName, filters[filterName]);
  51. }
  52. const eventView = EventView.fromNewQueryWithLocation(
  53. {
  54. name: '',
  55. query: search.formatString(),
  56. fields,
  57. dataset: DiscoverDatasets.SPANS_INDEXED,
  58. version: 2,
  59. },
  60. location
  61. );
  62. if (sorts) {
  63. eventView.sorts = sorts;
  64. }
  65. return eventView;
  66. }