useIndexedSpans.tsx 1.6 KB

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