useDiscover.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. pageFilters?: PageFilters;
  20. referrer?: string;
  21. search?: MutableSearch | string; // TODO - ideally this probably would be only `Mutable Search`, but it doesn't handle some situations well
  22. sorts?: Sort[];
  23. }
  24. export const useSpanMetrics = <Fields extends SpanMetricsProperty[]>(
  25. options: UseMetricsOptions<Fields> = {},
  26. referrer: string
  27. ) => {
  28. return useDiscover<Fields, SpanMetricsResponse>(
  29. options,
  30. DiscoverDatasets.SPANS_METRICS,
  31. referrer
  32. );
  33. };
  34. export const useMetrics = <Fields extends MetricsProperty[]>(
  35. options: UseMetricsOptions<Fields> = {},
  36. referrer: string
  37. ) => {
  38. return useDiscover<Fields, MetricsResponse>(
  39. options,
  40. DiscoverDatasets.METRICS,
  41. referrer
  42. );
  43. };
  44. const useDiscover = <T extends Extract<keyof ResponseType, string>[], ResponseType>(
  45. options: UseMetricsOptions<T> = {},
  46. dataset: DiscoverDatasets,
  47. referrer: string
  48. ) => {
  49. const {
  50. fields = [],
  51. search = undefined,
  52. sorts = [],
  53. limit,
  54. cursor,
  55. pageFilters: pageFiltersFromOptions,
  56. } = options;
  57. const pageFilters = usePageFilters();
  58. const eventView = getEventView(
  59. search,
  60. fields,
  61. sorts,
  62. pageFiltersFromOptions ?? pageFilters.selection,
  63. dataset
  64. );
  65. const result = useWrappedDiscoverQuery({
  66. eventView,
  67. initialData: [],
  68. limit,
  69. enabled: options.enabled,
  70. referrer,
  71. cursor,
  72. });
  73. // 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
  74. // eslint-disable-next-line @typescript-eslint/ban-types
  75. const data = (result?.data ?? []) as Pick<ResponseType, T[number]>[];
  76. return {
  77. ...result,
  78. data,
  79. isEnabled: options.enabled,
  80. };
  81. };
  82. function getEventView(
  83. search: MutableSearch | string | undefined,
  84. fields: string[] = [],
  85. sorts: Sort[] = [],
  86. pageFilters: PageFilters,
  87. dataset: DiscoverDatasets
  88. ) {
  89. const query = typeof search === 'string' ? search : search?.formatString() ?? '';
  90. const eventView = EventView.fromNewQueryWithPageFilters(
  91. {
  92. name: '',
  93. query,
  94. fields,
  95. dataset,
  96. version: 2,
  97. },
  98. pageFilters
  99. );
  100. if (sorts.length > 0) {
  101. eventView.sorts = sorts;
  102. }
  103. return eventView;
  104. }