useDiscover.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type {PageFilters} from 'sentry/types/core';
  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 type {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import type {
  8. MetricsProperty,
  9. MetricsResponse,
  10. SpanMetricsProperty,
  11. SpanMetricsResponse,
  12. } from 'sentry/views/starfish/types';
  13. import {useWrappedDiscoverQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  14. interface UseMetricsOptions<Fields> {
  15. cursor?: string;
  16. enabled?: boolean;
  17. fields?: Fields;
  18. limit?: number;
  19. referrer?: string;
  20. search?: MutableSearch | string; // TODO - ideally this probably would be only `Mutable Search`, but it doesn't handle some situations well
  21. sorts?: Sort[];
  22. }
  23. export const useSpanMetrics = <Fields extends SpanMetricsProperty[]>(
  24. options: UseMetricsOptions<Fields> = {},
  25. referrer: string
  26. ) => {
  27. return useDiscover<Fields, SpanMetricsResponse>(
  28. options,
  29. DiscoverDatasets.SPANS_METRICS,
  30. referrer
  31. );
  32. };
  33. export const useMetrics = <Fields extends MetricsProperty[]>(
  34. options: UseMetricsOptions<Fields> = {},
  35. referrer: string
  36. ) => {
  37. return useDiscover<Fields, MetricsResponse>(
  38. options,
  39. DiscoverDatasets.METRICS,
  40. referrer
  41. );
  42. };
  43. const useDiscover = <T extends Extract<keyof ResponseType, string>[], ResponseType>(
  44. options: UseMetricsOptions<T> = {},
  45. dataset: DiscoverDatasets,
  46. referrer: string
  47. ) => {
  48. const {fields = [], search = undefined, sorts = [], limit, cursor} = options;
  49. const pageFilters = usePageFilters();
  50. const eventView = getEventView(search, fields, sorts, pageFilters.selection, dataset);
  51. const result = useWrappedDiscoverQuery({
  52. eventView,
  53. initialData: [],
  54. limit,
  55. enabled: options.enabled,
  56. referrer,
  57. cursor,
  58. });
  59. // This type is a little awkward but it explicitly states that the response could be empty. This doesn't enable unchecked access errors, but it at least indicates that it's possible that there's no data
  60. // eslint-disable-next-line @typescript-eslint/ban-types
  61. const data = (result?.data ?? []) as Pick<ResponseType, T[number]>[];
  62. return {
  63. ...result,
  64. data,
  65. isEnabled: options.enabled,
  66. };
  67. };
  68. function getEventView(
  69. search: MutableSearch | string | undefined,
  70. fields: string[] = [],
  71. sorts: Sort[] = [],
  72. pageFilters: PageFilters,
  73. dataset: DiscoverDatasets
  74. ) {
  75. const query = typeof search === 'string' ? search : search?.formatString() ?? '';
  76. const eventView = EventView.fromNewQueryWithPageFilters(
  77. {
  78. name: '',
  79. query,
  80. fields,
  81. dataset,
  82. version: 2,
  83. },
  84. pageFilters
  85. );
  86. if (sorts.length > 0) {
  87. eventView.sorts = sorts;
  88. }
  89. return eventView;
  90. }