useIndexedSpans.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import type {PageFilters} from 'sentry/types/core';
  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 type {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import type {IndexedProperty, SpanIndexedFieldTypes} from 'sentry/views/starfish/types';
  8. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  9. interface UseIndexedSpansOptions<Fields> {
  10. cursor?: string;
  11. enabled?: boolean;
  12. fields?: Fields;
  13. limit?: number;
  14. referrer?: string;
  15. search?: MutableSearch;
  16. sorts?: Sort[];
  17. }
  18. export const useIndexedSpans = <Fields extends IndexedProperty[]>(
  19. options: UseIndexedSpansOptions<Fields> = {}
  20. ) => {
  21. const {
  22. fields = [],
  23. search = undefined,
  24. sorts = [],
  25. limit,
  26. cursor,
  27. referrer,
  28. enabled,
  29. } = options;
  30. const pageFilters = usePageFilters();
  31. const eventView = getEventView(search, fields, sorts, pageFilters.selection);
  32. return useSpansQuery<SpanIndexedFieldTypes[]>({
  33. eventView,
  34. cursor,
  35. limit,
  36. initialData: [],
  37. enabled,
  38. referrer,
  39. });
  40. };
  41. function getEventView(
  42. search: MutableSearch | undefined,
  43. fields: string[] = [],
  44. sorts: Sort[] = [],
  45. pageFilters: PageFilters
  46. ) {
  47. const eventView = EventView.fromNewQueryWithPageFilters(
  48. {
  49. name: '',
  50. query: search?.formatString() ?? '',
  51. fields,
  52. dataset: DiscoverDatasets.SPANS_INDEXED,
  53. version: 2,
  54. },
  55. pageFilters
  56. );
  57. if (sorts.length > 0) {
  58. eventView.sorts = sorts;
  59. }
  60. return eventView;
  61. }