useSpanMetricsTopNSeries.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type {Series} from 'sentry/types/echarts';
  2. import type {Sort} from 'sentry/utils/discover/fields';
  3. import type {MutableSearch} from 'sentry/utils/tokenizeSearch';
  4. import usePageFilters from 'sentry/utils/usePageFilters';
  5. import {getSeriesEventView} from 'sentry/views/insights/common/queries/getSeriesEventView';
  6. import {useWrappedDiscoverTimeseriesQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
  7. import type {SpanMetricsProperty} from 'sentry/views/insights/types';
  8. interface SpanMetricTimeseriesRow {
  9. [key: string]: number;
  10. interval: number;
  11. }
  12. interface UseSpanMetricsSeriesOptions<Fields> {
  13. topEvents: number;
  14. enabled?: boolean;
  15. fields?: Fields;
  16. referrer?: string;
  17. search?: MutableSearch;
  18. sorts?: Sort[];
  19. yAxis?: Fields;
  20. }
  21. export const useSpanMetricsTopNSeries = <Fields extends SpanMetricsProperty[]>(
  22. options: UseSpanMetricsSeriesOptions<Fields> = {topEvents: DEFAULT_EVENT_COUNT}
  23. ) => {
  24. const {
  25. search = undefined,
  26. fields = [],
  27. yAxis = [],
  28. topEvents,
  29. sorts = [],
  30. referrer = 'span-metrics-top-n-series',
  31. } = options;
  32. if (yAxis.length > 1) {
  33. throw new Error(
  34. 'Multi-axis top-N queries are not supported by this hook. Try using `useSpansQuery` directly.'
  35. );
  36. }
  37. const pageFilters = usePageFilters();
  38. const eventView = getSeriesEventView(
  39. search,
  40. fields,
  41. pageFilters.selection,
  42. yAxis,
  43. topEvents
  44. );
  45. if (sorts.length > 0) {
  46. eventView.sorts = sorts;
  47. }
  48. const result = useWrappedDiscoverTimeseriesQuery<SpanMetricTimeseriesRow[]>({
  49. eventView,
  50. initialData: [],
  51. referrer,
  52. enabled: options.enabled,
  53. });
  54. const seriesByKey: {[key: string]: Series} = {};
  55. (result?.data ?? []).forEach(datum => {
  56. // `interval` is the timestamp of the data point. Every other key is the value of a requested or found timeseries. `groups` is used to disambiguate top-N multi-axis series, which aren't supported here so the value is useless
  57. const {interval, group: _group, ...data} = datum;
  58. Object.keys(data).forEach(key => {
  59. const value = {
  60. name: interval,
  61. value: datum[key],
  62. };
  63. if (seriesByKey[key]) {
  64. seriesByKey[key].data.push(value);
  65. } else {
  66. seriesByKey[key] = {
  67. seriesName: key,
  68. data: [value],
  69. };
  70. }
  71. });
  72. });
  73. return {...result, data: seriesByKey as {[key: string]: Series}};
  74. };
  75. const DEFAULT_EVENT_COUNT = 5;