useDiscoverSeries.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/starfish/queries/getSeriesEventView';
  7. import type {MetricsProperty, SpanMetricsProperty} from 'sentry/views/starfish/types';
  8. import {useWrappedDiscoverTimeseriesQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  9. export interface MetricTimeseriesRow {
  10. [key: string]: number;
  11. interval: number;
  12. }
  13. interface UseMetricsSeriesOptions<Fields> {
  14. enabled?: boolean;
  15. referrer?: string;
  16. search?: MutableSearch;
  17. yAxis?: Fields;
  18. }
  19. export const useSpanMetricsSeries = <Fields extends SpanMetricsProperty[]>(
  20. options: UseMetricsSeriesOptions<Fields> = {},
  21. referrer: string
  22. ) => {
  23. return useDiscoverSeries<Fields>(options, DiscoverDatasets.SPANS_METRICS, referrer);
  24. };
  25. export const useMetricsSeries = <Fields extends MetricsProperty[]>(
  26. options: UseMetricsSeriesOptions<Fields> = {},
  27. referrer: string
  28. ) => {
  29. return useDiscoverSeries<Fields>(options, DiscoverDatasets.METRICS, referrer);
  30. };
  31. const useDiscoverSeries = <T extends string[]>(
  32. options: UseMetricsSeriesOptions<T> = {},
  33. dataset: DiscoverDatasets,
  34. referrer: string
  35. ) => {
  36. const {search = undefined, yAxis = []} = options;
  37. const pageFilters = usePageFilters();
  38. const eventView = getSeriesEventView(
  39. search,
  40. undefined,
  41. pageFilters.selection,
  42. yAxis,
  43. undefined,
  44. dataset
  45. );
  46. const result = useWrappedDiscoverTimeseriesQuery<MetricTimeseriesRow[]>({
  47. eventView,
  48. initialData: [],
  49. referrer,
  50. enabled: options.enabled,
  51. });
  52. const parsedData = keyBy(
  53. yAxis.map(seriesName => {
  54. const series: Series = {
  55. seriesName,
  56. data: (result?.data ?? []).map(datum => ({
  57. value: datum[seriesName],
  58. name: datum?.interval,
  59. })),
  60. };
  61. return series;
  62. }),
  63. 'seriesName'
  64. ) as Record<T[number], Series>;
  65. return {...result, data: parsedData};
  66. };