useDiscover.ts 3.2 KB

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