import keyBy from 'lodash/keyBy'; import type {Series} from 'sentry/types/echarts'; import {DiscoverDatasets} from 'sentry/utils/discover/types'; import type {MutableSearch} from 'sentry/utils/tokenizeSearch'; import usePageFilters from 'sentry/utils/usePageFilters'; import {getSeriesEventView} from 'sentry/views/starfish/queries/getSeriesEventView'; import type {MetricsProperty, SpanMetricsProperty} from 'sentry/views/starfish/types'; import {useWrappedDiscoverTimeseriesQuery} from 'sentry/views/starfish/utils/useSpansQuery'; export interface MetricTimeseriesRow { [key: string]: number; interval: number; } interface UseMetricsSeriesOptions { enabled?: boolean; referrer?: string; search?: MutableSearch; yAxis?: Fields; } export const useSpanMetricsSeries = ( options: UseMetricsSeriesOptions = {}, referrer: string ) => { return useDiscoverSeries(options, DiscoverDatasets.SPANS_METRICS, referrer); }; export const useMetricsSeries = ( options: UseMetricsSeriesOptions = {}, referrer: string ) => { return useDiscoverSeries(options, DiscoverDatasets.METRICS, referrer); }; const useDiscoverSeries = ( options: UseMetricsSeriesOptions = {}, dataset: DiscoverDatasets, referrer: string ) => { const {search = undefined, yAxis = []} = options; const pageFilters = usePageFilters(); const eventView = getSeriesEventView( search, undefined, pageFilters.selection, yAxis, undefined, dataset ); const result = useWrappedDiscoverTimeseriesQuery({ eventView, initialData: [], referrer, enabled: options.enabled, }); const parsedData = keyBy( yAxis.map(seriesName => { const series: Series = { seriesName, data: (result?.data ?? []).map(datum => ({ value: datum[seriesName], name: datum?.interval, })), }; return series; }), 'seriesName' ) as Record; return {...result, data: parsedData}; };