useDiscoverSeries.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ) => {
  47. return useDiscoverSeries<Fields>(options, DiscoverDatasets.SPANS_INDEXED, referrer);
  48. };
  49. const useDiscoverSeries = <T extends string[]>(
  50. options: UseMetricsSeriesOptions<T> = {},
  51. dataset: DiscoverDatasets,
  52. referrer: string
  53. ) => {
  54. const {search = undefined, yAxis = [], interval = undefined} = options;
  55. const pageFilters = usePageFilters();
  56. const eventView = getSeriesEventView(
  57. search,
  58. undefined,
  59. pageFilters.selection,
  60. yAxis,
  61. undefined,
  62. dataset
  63. );
  64. if (interval) {
  65. eventView.interval = interval;
  66. }
  67. const result = useWrappedDiscoverTimeseriesQuery<MetricTimeseriesRow[]>({
  68. eventView,
  69. initialData: [],
  70. referrer,
  71. enabled: options.enabled,
  72. overriddenRoute: options.overriddenRoute,
  73. });
  74. const parsedData = keyBy(
  75. yAxis.map(seriesName => {
  76. const series: Series = {
  77. seriesName,
  78. data: (result?.data ?? []).map(datum => ({
  79. value: datum[seriesName],
  80. name: datum?.interval,
  81. })),
  82. };
  83. return series;
  84. }),
  85. 'seriesName'
  86. ) as Record<T[number], Series>;
  87. return {...result, data: parsedData};
  88. };