useSpanMetricsSeries.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import keyBy from 'lodash/keyBy';
  2. import {getInterval} from 'sentry/components/charts/utils';
  3. import {PageFilters} from 'sentry/types';
  4. import {Series} from 'sentry/types/echarts';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import {SpanSummaryQueryFilters} from 'sentry/views/starfish/queries/useSpanMetrics';
  9. import {SpanMetricsField} from 'sentry/views/starfish/types';
  10. import {STARFISH_CHART_INTERVAL_FIDELITY} from 'sentry/views/starfish/utils/constants';
  11. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  12. const {SPAN_GROUP, RESOURCE_RENDER_BLOCKING_STATUS} = SpanMetricsField;
  13. export type SpanMetrics = {
  14. interval: number;
  15. 'p95(span.self_time)': number;
  16. 'spm()': number;
  17. 'sum(span.self_time)': number;
  18. 'time_spent_percentage()': number;
  19. };
  20. export const useSpanMetricsSeries = (
  21. group: string,
  22. queryFilters: SpanSummaryQueryFilters,
  23. yAxis: string[] = [],
  24. referrer = 'span-metrics-series'
  25. ) => {
  26. const pageFilters = usePageFilters();
  27. const eventView = group
  28. ? getEventView(group, pageFilters.selection, yAxis, queryFilters)
  29. : undefined;
  30. const enabled =
  31. Boolean(group) && Object.values(queryFilters).every(value => Boolean(value));
  32. const result = useSpansQuery<SpanMetrics[]>({
  33. eventView,
  34. initialData: [],
  35. referrer,
  36. enabled,
  37. });
  38. const parsedData = keyBy(
  39. yAxis.map(seriesName => {
  40. const series: Series = {
  41. seriesName,
  42. data: (result?.data ?? []).map(datum => ({
  43. value: datum[seriesName],
  44. name: datum.interval,
  45. })),
  46. };
  47. return series;
  48. }),
  49. 'seriesName'
  50. );
  51. return {...result, data: parsedData};
  52. };
  53. function getEventView(
  54. group: string,
  55. pageFilters: PageFilters,
  56. yAxis: string[],
  57. queryFilters: SpanSummaryQueryFilters
  58. ) {
  59. const query = [
  60. `${SPAN_GROUP}:${group}`,
  61. ...(queryFilters?.transactionName
  62. ? [`transaction:"${queryFilters?.transactionName}"`]
  63. : []),
  64. ...(queryFilters?.['transaction.method']
  65. ? [`transaction.method:${queryFilters?.['transaction.method']}`]
  66. : []),
  67. ...(queryFilters?.release ? [`release:${queryFilters?.release}`] : []),
  68. ...(queryFilters?.[RESOURCE_RENDER_BLOCKING_STATUS]
  69. ? [
  70. `${RESOURCE_RENDER_BLOCKING_STATUS}:${queryFilters[RESOURCE_RENDER_BLOCKING_STATUS]}`,
  71. ]
  72. : []),
  73. ].join(' ');
  74. return EventView.fromNewQueryWithPageFilters(
  75. {
  76. name: '',
  77. query,
  78. fields: [],
  79. yAxis,
  80. dataset: DiscoverDatasets.SPANS_METRICS,
  81. interval: getInterval(pageFilters.datetime, STARFISH_CHART_INTERVAL_FIDELITY),
  82. version: 2,
  83. },
  84. pageFilters
  85. );
  86. }