useSpanMetricsSeries.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import keyBy from 'lodash/keyBy';
  2. import type {Series} from 'sentry/types/echarts';
  3. import type {MutableSearch} from 'sentry/utils/tokenizeSearch';
  4. import usePageFilters from 'sentry/utils/usePageFilters';
  5. import {getSeriesEventView} from 'sentry/views/starfish/queries/getSeriesEventView';
  6. import type {MetricsProperty} from 'sentry/views/starfish/types';
  7. import {useWrappedDiscoverTimeseriesQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  8. interface SpanMetricTimeseriesRow {
  9. [key: string]: number;
  10. interval: number;
  11. }
  12. interface UseSpanMetricsSeriesOptions<Fields> {
  13. enabled?: boolean;
  14. referrer?: string;
  15. search?: MutableSearch;
  16. yAxis?: Fields;
  17. }
  18. export const useSpanMetricsSeries = <Fields extends MetricsProperty[]>(
  19. options: UseSpanMetricsSeriesOptions<Fields> = {}
  20. ) => {
  21. const {search = undefined, yAxis = [], referrer = 'span-metrics-series'} = options;
  22. const pageFilters = usePageFilters();
  23. const eventView = getSeriesEventView(
  24. search,
  25. undefined,
  26. pageFilters.selection,
  27. yAxis,
  28. undefined
  29. );
  30. const result = useWrappedDiscoverTimeseriesQuery<SpanMetricTimeseriesRow[]>({
  31. eventView,
  32. initialData: [],
  33. referrer,
  34. enabled: options.enabled,
  35. });
  36. const parsedData = keyBy(
  37. yAxis.map(seriesName => {
  38. const series: Series = {
  39. seriesName,
  40. data: (result?.data ?? []).map(datum => ({
  41. value: datum[seriesName],
  42. name: datum?.interval,
  43. })),
  44. };
  45. return series;
  46. }),
  47. 'seriesName'
  48. ) as Record<Fields[number], Series>;
  49. return {...result, data: parsedData};
  50. };