useDiscoverSeries.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import keyBy from 'lodash/keyBy';
  2. import type {Series} from 'sentry/types/echarts';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import type {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import {getSeriesEventView} from 'sentry/views/insights/common/queries/getSeriesEventView';
  7. import {useWrappedDiscoverTimeseriesQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
  8. import type {
  9. MetricsProperty,
  10. SpanFunctions,
  11. SpanIndexedField,
  12. SpanMetricsProperty,
  13. } from 'sentry/views/insights/types';
  14. export interface MetricTimeseriesRow {
  15. [key: string]: number;
  16. interval: number;
  17. }
  18. interface UseMetricsSeriesOptions<Fields> {
  19. enabled?: boolean;
  20. interval?: string;
  21. overriddenRoute?: string;
  22. referrer?: string;
  23. search?: MutableSearch;
  24. yAxis?: Fields;
  25. }
  26. export const useSpanMetricsSeries = <Fields extends SpanMetricsProperty[]>(
  27. options: UseMetricsSeriesOptions<Fields> = {},
  28. referrer: string
  29. ) => {
  30. return useDiscoverSeries<Fields>(options, DiscoverDatasets.SPANS_METRICS, referrer);
  31. };
  32. export const useMetricsSeries = <Fields extends MetricsProperty[]>(
  33. options: UseMetricsSeriesOptions<Fields> = {},
  34. referrer: string
  35. ) => {
  36. return useDiscoverSeries<Fields>(options, DiscoverDatasets.METRICS, referrer);
  37. };
  38. /**
  39. * TODO: Remove string type, added to fix types for 'count()'
  40. */
  41. export const useSpanIndexedSeries = <
  42. Fields extends SpanIndexedField[] | SpanFunctions[] | string[],
  43. >(
  44. options: UseMetricsSeriesOptions<Fields> = {},
  45. referrer: string,
  46. dataset?: DiscoverDatasets
  47. ) => {
  48. return useDiscoverSeries<Fields>(
  49. options,
  50. dataset ?? DiscoverDatasets.SPANS_INDEXED,
  51. referrer
  52. );
  53. };
  54. const useDiscoverSeries = <T extends string[]>(
  55. options: UseMetricsSeriesOptions<T> = {},
  56. dataset: DiscoverDatasets,
  57. referrer: string
  58. ) => {
  59. const {search = undefined, yAxis = [], interval = undefined} = options;
  60. const pageFilters = usePageFilters();
  61. const eventView = getSeriesEventView(
  62. search,
  63. undefined,
  64. pageFilters.selection,
  65. yAxis,
  66. undefined,
  67. dataset
  68. );
  69. if (interval) {
  70. eventView.interval = interval;
  71. }
  72. const result = useWrappedDiscoverTimeseriesQuery<MetricTimeseriesRow[]>({
  73. eventView,
  74. initialData: [],
  75. referrer,
  76. enabled: options.enabled,
  77. overriddenRoute: options.overriddenRoute,
  78. });
  79. const parsedData = keyBy(
  80. yAxis.map(seriesName => {
  81. const series: Series = {
  82. seriesName,
  83. data: (result?.data ?? []).map(datum => ({
  84. value: datum[seriesName],
  85. name: datum?.interval,
  86. })),
  87. };
  88. return series;
  89. }),
  90. 'seriesName'
  91. ) as Record<T[number], Series>;
  92. return {...result, data: parsedData};
  93. };