useIndexedSpans.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 {SpanIndexedFields, 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. return useSpansQuery<SpanIndexedFieldTypes[]>({
  21. eventView,
  22. limit,
  23. initialData: [],
  24. enabled,
  25. referrer,
  26. });
  27. };
  28. function getEventView(filters: Filters, location: Location) {
  29. // TODO: Add a `MutableSearch` constructor that accept a key-value mapping
  30. const search = new MutableSearch([]);
  31. for (const filterName in filters) {
  32. search.addFilterValue(filterName, filters[filterName]);
  33. }
  34. return EventView.fromNewQueryWithLocation(
  35. {
  36. name: '',
  37. query: search.formatString(),
  38. fields: Object.values(SpanIndexedFields),
  39. dataset: DiscoverDatasets.SPANS_INDEXED,
  40. version: 2,
  41. },
  42. location
  43. );
  44. }