useSpanMetricsSeries.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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} = 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. // TODO: Add referrer
  33. const result = useSpansQuery<SpanMetrics[]>({
  34. eventView,
  35. initialData: [],
  36. referrer,
  37. enabled,
  38. });
  39. const parsedData = keyBy(
  40. yAxis.map(seriesName => {
  41. const series: Series = {
  42. seriesName,
  43. data: (result?.data ?? []).map(datum => ({
  44. value: datum[seriesName],
  45. name: datum.interval,
  46. })),
  47. };
  48. return series;
  49. }),
  50. 'seriesName'
  51. );
  52. return {...result, data: parsedData};
  53. };
  54. function getEventView(
  55. group: string,
  56. pageFilters: PageFilters,
  57. yAxis: string[],
  58. queryFilters: SpanSummaryQueryFilters
  59. ) {
  60. return EventView.fromNewQueryWithPageFilters(
  61. {
  62. name: '',
  63. query: `${SPAN_GROUP}:${group}${
  64. queryFilters?.transactionName
  65. ? ` transaction:"${queryFilters?.transactionName}"`
  66. : ''
  67. }${
  68. queryFilters?.['transaction.method']
  69. ? ` transaction.method:${queryFilters?.['transaction.method']}`
  70. : ''
  71. }${queryFilters?.release ? ` release:${queryFilters?.release}` : ''}`,
  72. fields: [],
  73. yAxis,
  74. dataset: DiscoverDatasets.SPANS_METRICS,
  75. interval: getInterval(pageFilters.datetime, STARFISH_CHART_INTERVAL_FIDELITY),
  76. version: 2,
  77. },
  78. pageFilters
  79. );
  80. }