useIndexedSpans.tsx 1.6 KB

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