useIndexedSpans.tsx 1.6 KB

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