profilingSparklineChart.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import {useMemo} from 'react';
  2. import {type Theme, useTheme} from '@emotion/react';
  3. import {
  4. LineChart,
  5. LineChartProps,
  6. LineChartSeries,
  7. } from 'sentry/components/charts/lineChart';
  8. import {t} from 'sentry/locale';
  9. import {tooltipFormatter} from 'sentry/utils/discover/charts';
  10. import {makeFormatter} from 'sentry/utils/profiling/units/units';
  11. const durationFormatter = makeFormatter('nanoseconds', 0);
  12. function asSeries(
  13. seriesName: string,
  14. color: string | undefined,
  15. data: {timestamp: number; value: number}[]
  16. ) {
  17. return {
  18. data: data.map(p => ({
  19. name: p.timestamp * 1e3,
  20. value: p.value ?? 0,
  21. })),
  22. color,
  23. seriesName,
  24. };
  25. }
  26. function getTooltipFormatter(label: string, baseline: number) {
  27. return [
  28. '<div class="tooltip-series tooltip-series-solo">',
  29. '<div>',
  30. `<span class="tooltip-label"><strong>${label}</strong></span>`,
  31. tooltipFormatter(baseline / 1e6, 'duration'),
  32. '</div>',
  33. '</div>',
  34. '<div class="tooltip-arrow"></div>',
  35. ].join('');
  36. }
  37. interface BaseSparklineChartProps {
  38. name: string;
  39. points: {timestamp: number; value: number}[];
  40. chartProps?: Partial<LineChartProps>;
  41. color?: string;
  42. }
  43. interface ProfilingSparklineChartPropsWithBreakpoint extends BaseSparklineChartProps {
  44. aggregate_range_1: number;
  45. aggregate_range_2: number;
  46. breakpoint: number;
  47. end: number;
  48. start: number;
  49. }
  50. interface ProfilingSparklineChartPropsWithoutBreakpoint extends BaseSparklineChartProps {}
  51. function isBreakPointProps(
  52. props: ProfilingSparklineChartProps
  53. ): props is ProfilingSparklineChartPropsWithBreakpoint {
  54. return typeof (props as any).breakpoint === 'number';
  55. }
  56. type ProfilingSparklineChartProps =
  57. | ProfilingSparklineChartPropsWithBreakpoint
  58. | ProfilingSparklineChartPropsWithoutBreakpoint;
  59. function makeSeriesBeforeAfterLines(
  60. start: number,
  61. breakpoint: number,
  62. end: number,
  63. aggregate_range_1: number,
  64. aggregate_range_2: number,
  65. theme: Theme
  66. ): LineChartSeries[] {
  67. const dividingLine = {
  68. data: [],
  69. color: theme.textColor,
  70. seriesName: 'dividing line',
  71. markLine: {},
  72. };
  73. dividingLine.markLine = {
  74. data: [{xAxis: breakpoint * 1e3}],
  75. label: {show: false},
  76. lineStyle: {
  77. color: theme.textColor,
  78. type: 'solid',
  79. width: 2,
  80. },
  81. symbol: ['none', 'none'],
  82. tooltip: {
  83. show: false,
  84. },
  85. silent: true,
  86. };
  87. const beforeLine = {
  88. data: [],
  89. color: theme.textColor,
  90. seriesName: 'before line',
  91. markLine: {},
  92. };
  93. beforeLine.markLine = {
  94. data: [
  95. [
  96. {value: 'Past', coord: [start * 1e3, aggregate_range_1]},
  97. {coord: [breakpoint * 1e3, aggregate_range_1]},
  98. ],
  99. ],
  100. label: {
  101. show: false,
  102. },
  103. lineStyle: {
  104. color: theme.textColor,
  105. type: 'dashed',
  106. width: 1,
  107. },
  108. symbol: ['none', 'none'],
  109. tooltip: {
  110. formatter: getTooltipFormatter(t('Past Baseline'), aggregate_range_1),
  111. },
  112. };
  113. const afterLine = {
  114. data: [],
  115. color: theme.textColor,
  116. seriesName: 'after line',
  117. markLine: {},
  118. };
  119. afterLine.markLine = {
  120. data: [
  121. [
  122. {
  123. value: 'Present',
  124. coord: [breakpoint * 1e3, aggregate_range_2],
  125. },
  126. {coord: [end * 1e3, aggregate_range_2]},
  127. ],
  128. ],
  129. label: {
  130. show: false,
  131. },
  132. lineStyle: {
  133. color: theme.textColor,
  134. type: 'dashed',
  135. width: 1,
  136. },
  137. symbol: ['none', 'none'],
  138. tooltip: {
  139. formatter: getTooltipFormatter(t('Present Baseline'), aggregate_range_2),
  140. },
  141. };
  142. return [dividingLine, beforeLine, afterLine];
  143. }
  144. export function ProfilingSparklineChart(props: ProfilingSparklineChartProps) {
  145. const theme = useTheme();
  146. const chartProps: LineChartProps = useMemo(() => {
  147. const additionalSeries: LineChartSeries[] = [];
  148. if (isBreakPointProps(props)) {
  149. additionalSeries.push(
  150. ...makeSeriesBeforeAfterLines(
  151. props.start,
  152. props.breakpoint,
  153. props.end,
  154. props.aggregate_range_1,
  155. props.aggregate_range_2,
  156. theme
  157. )
  158. );
  159. }
  160. const baseProps: LineChartProps = {
  161. height: 26,
  162. width: 'auto',
  163. series: [asSeries(props.name, props.color, props.points), ...additionalSeries],
  164. grid: [
  165. {
  166. containLabel: false,
  167. top: '2px',
  168. left: '2px',
  169. right: '2px',
  170. bottom: '2px',
  171. },
  172. {
  173. containLabel: false,
  174. top: '2px',
  175. left: '2px',
  176. right: '2px',
  177. bottom: '2px',
  178. },
  179. ],
  180. tooltip: {
  181. valueFormatter: (v: number) => durationFormatter(v),
  182. },
  183. axisPointer: {},
  184. xAxes: [
  185. {
  186. gridIndex: 0,
  187. type: 'time' as const,
  188. show: false,
  189. },
  190. ],
  191. yAxes: [
  192. {
  193. scale: true,
  194. show: false,
  195. },
  196. ],
  197. ...(props.chartProps ? props.chartProps : {}),
  198. };
  199. return baseProps;
  200. }, [props, theme]);
  201. return <LineChart {...chartProps} isGroupedByDate showTimeInTooltip />;
  202. }