useIndexedSpans.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. export interface Filters {
  10. [key: string]: 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. const filter = filters[filterName];
  47. if (Array.isArray(filter)) {
  48. search.addFilterValues(filterName, filter);
  49. } else {
  50. search.addFilterValue(filterName, filter);
  51. }
  52. }
  53. const eventView = EventView.fromNewQueryWithLocation(
  54. {
  55. name: '',
  56. query: search.formatString(),
  57. fields,
  58. dataset: DiscoverDatasets.SPANS_INDEXED,
  59. version: 2,
  60. },
  61. location
  62. );
  63. if (sorts) {
  64. eventView.sorts = sorts;
  65. }
  66. return eventView;
  67. }