useIndexedSpans.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. cursor,
  16. limit,
  17. enabled = true,
  18. referrer,
  19. fields,
  20. }: {
  21. fields: SpanIndexedField[];
  22. filters: Filters;
  23. limit: number;
  24. referrer: string;
  25. sorts: Sort[];
  26. cursor?: string;
  27. enabled?: boolean;
  28. }) => {
  29. const location = useLocation();
  30. const eventView = getEventView(filters, location, fields, sorts);
  31. return useSpansQuery<SpanIndexedFieldTypes[]>({
  32. eventView,
  33. cursor,
  34. limit,
  35. initialData: [],
  36. enabled,
  37. referrer,
  38. });
  39. };
  40. function getEventView(
  41. filters: Filters,
  42. location: Location,
  43. fields: SpanIndexedField[],
  44. sorts?: Sort[]
  45. ) {
  46. // TODO: Add a `MutableSearch` constructor that accept a key-value mapping
  47. const search = new MutableSearch([]);
  48. for (const filterName in filters) {
  49. const filter = filters[filterName];
  50. if (Array.isArray(filter)) {
  51. search.addFilterValues(filterName, filter);
  52. } else {
  53. search.addFilterValue(filterName, filter);
  54. }
  55. }
  56. const eventView = EventView.fromNewQueryWithLocation(
  57. {
  58. name: '',
  59. query: search.formatString(),
  60. fields,
  61. dataset: DiscoverDatasets.SPANS_INDEXED,
  62. version: 2,
  63. },
  64. location
  65. );
  66. if (sorts) {
  67. eventView.sorts = sorts;
  68. }
  69. return eventView;
  70. }