useDiscover.ts 3.3 KB

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