useSpanMetricsSeries.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {Location} from 'history';
  2. import keyBy from 'lodash/keyBy';
  3. import {getInterval} from 'sentry/components/charts/utils';
  4. import {PageFilters} from 'sentry/types';
  5. import {Series} from 'sentry/types/echarts';
  6. import EventView from 'sentry/utils/discover/eventView';
  7. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import usePageFilters from 'sentry/utils/usePageFilters';
  10. import type {IndexedSpan} from 'sentry/views/starfish/queries/types';
  11. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  12. export type SpanMetrics = {
  13. interval: number;
  14. 'p95(span.self_time)': number;
  15. 'sps()': number;
  16. 'sum(span.self_time)': number;
  17. 'time_spent_percentage()': number;
  18. };
  19. export const useSpanMetricsSeries = (
  20. span?: Pick<IndexedSpan, 'group'>,
  21. queryFilters: {transactionName?: string} = {},
  22. yAxis: string[] = [],
  23. referrer = 'span-metrics-series'
  24. ) => {
  25. const location = useLocation();
  26. const pageFilters = usePageFilters();
  27. const eventView = span
  28. ? getEventView(
  29. span,
  30. location,
  31. pageFilters.selection,
  32. yAxis,
  33. queryFilters.transactionName
  34. )
  35. : undefined;
  36. // TODO: Add referrer
  37. const {isLoading, data} = useSpansQuery<SpanMetrics[]>({
  38. eventView,
  39. initialData: [],
  40. referrer,
  41. });
  42. const parsedData = keyBy(
  43. yAxis.map(seriesName => {
  44. const series: Series = {
  45. seriesName,
  46. data: data.map(datum => ({value: datum[seriesName], name: datum.interval})),
  47. };
  48. return series;
  49. }),
  50. 'seriesName'
  51. );
  52. return {isLoading, data: parsedData};
  53. };
  54. function getEventView(
  55. span: {group: string},
  56. location: Location,
  57. pageFilters: PageFilters,
  58. yAxis: string[],
  59. transaction?: string
  60. ) {
  61. const cleanGroupId = span.group.replaceAll('-', '').slice(-16);
  62. return EventView.fromNewQueryWithLocation(
  63. {
  64. name: '',
  65. query: `span.group:${cleanGroupId}${
  66. transaction ? ` transaction:${transaction}` : ''
  67. }`,
  68. fields: [],
  69. yAxis,
  70. dataset: DiscoverDatasets.SPANS_METRICS,
  71. interval: getInterval(pageFilters.datetime, 'low'),
  72. projects: [1],
  73. version: 2,
  74. },
  75. location
  76. );
  77. }