useIndexedSpans.tsx 1.4 KB

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