domNodesChart.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import {forwardRef, memo, useEffect, useMemo, useRef} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import moment from 'moment';
  5. import {AreaChart, AreaChartProps} from 'sentry/components/charts/areaChart';
  6. import Grid from 'sentry/components/charts/components/grid';
  7. import {ChartTooltip} from 'sentry/components/charts/components/tooltip';
  8. import XAxis from 'sentry/components/charts/components/xAxis';
  9. import YAxis from 'sentry/components/charts/components/yAxis';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import {useReplayContext} from 'sentry/components/replays/replayContext';
  12. import {showPlayerTime} from 'sentry/components/replays/utils';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {ReactEchartsRef, Series} from 'sentry/types/echarts';
  16. import {getFormattedDate} from 'sentry/utils/dates';
  17. import {axisLabelFormatter} from 'sentry/utils/discover/charts';
  18. import {useQuery} from 'sentry/utils/queryClient';
  19. import countDomNodes, {DomNodeChartDatapoint} from 'sentry/utils/replays/countDomNodes';
  20. import ReplayReader from 'sentry/utils/replays/replayReader';
  21. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  22. interface Props {
  23. datapoints: DomNodeChartDatapoint[];
  24. setCurrentHoverTime: (time: undefined | number) => void;
  25. setCurrentTime: (time: number) => void;
  26. startTimestampMs: undefined | number;
  27. }
  28. interface DomNodesChartProps extends Props {
  29. forwardedRef: React.Ref<ReactEchartsRef>;
  30. }
  31. const formatTimestamp = timestamp =>
  32. getFormattedDate(timestamp, 'MMM D, YYYY hh:mm:ss A z', {local: false});
  33. const formatTimestampTrim = timestamp =>
  34. getFormattedDate(timestamp, 'MMM D hh:mm', {local: false});
  35. function DomNodesChart({
  36. forwardedRef,
  37. datapoints,
  38. startTimestampMs = 0,
  39. setCurrentTime,
  40. setCurrentHoverTime,
  41. }: DomNodesChartProps) {
  42. const theme = useTheme();
  43. if (!datapoints) {
  44. return null;
  45. }
  46. if (!datapoints.length) {
  47. return (
  48. <DomNodesChartWrapper>
  49. <Placeholder height="100%" />
  50. </DomNodesChartWrapper>
  51. );
  52. }
  53. const chartOptions: Omit<AreaChartProps, 'series'> = {
  54. grid: Grid({
  55. top: '40px',
  56. left: space(1),
  57. right: space(1),
  58. }),
  59. tooltip: ChartTooltip({
  60. appendToBody: true,
  61. trigger: 'axis',
  62. renderMode: 'html',
  63. chartId: 'replay-dom-nodes-chart',
  64. formatter: values => {
  65. const seriesTooltips = values.map(
  66. value => `
  67. <div>
  68. <span className="tooltip-label">${value.marker}<strong>${value.seriesName}</strong></span>
  69. ${value.data[1]}
  70. </div>
  71. `
  72. );
  73. const template = [
  74. '<div class="tooltip-series">',
  75. ...seriesTooltips,
  76. '</div>',
  77. `<div class="tooltip-footer" style="display: inline-block; width: max-content;">${t(
  78. 'Span Time'
  79. )}:
  80. ${formatTimestamp(values[0].axisValue)}
  81. </div>`,
  82. `<div class="tooltip-footer" style="border: none;">${'Relative Time'}:
  83. ${showPlayerTime(
  84. moment(values[0].axisValue)
  85. .toDate()
  86. .toUTCString(),
  87. startTimestampMs
  88. )}
  89. </div>`,
  90. '<div class="tooltip-arrow"></div>',
  91. ].join('');
  92. return template;
  93. },
  94. }),
  95. xAxis: XAxis({
  96. type: 'time',
  97. axisLabel: {
  98. formatter: formatTimestampTrim,
  99. },
  100. theme,
  101. }),
  102. yAxis: YAxis({
  103. type: 'value',
  104. name: t('DOM Nodes'),
  105. theme,
  106. nameTextStyle: {
  107. padding: [8, 8, 8, 48],
  108. fontSize: theme.fontSizeLarge,
  109. fontWeight: 600,
  110. lineHeight: 1.2,
  111. fontFamily: theme.text.family,
  112. color: theme.gray300,
  113. },
  114. minInterval: 100,
  115. maxInterval: Math.pow(1024, 4),
  116. axisLabel: {
  117. formatter: (value: number) => axisLabelFormatter(value, 'number', true),
  118. },
  119. }),
  120. onMouseOver: ({data}) => {
  121. if (data[0]) {
  122. setCurrentHoverTime(data[0] - startTimestampMs);
  123. }
  124. },
  125. onMouseOut: () => {
  126. setCurrentHoverTime(undefined);
  127. },
  128. onClick: ({data}) => {
  129. if (data.value) {
  130. setCurrentTime(data.value - startTimestampMs);
  131. }
  132. },
  133. };
  134. const series: Series[] = [
  135. {
  136. seriesName: t('Number of DOM nodes'),
  137. data: datapoints.map(d => ({
  138. value: d.count,
  139. name: d.endTimestampMs,
  140. })),
  141. stack: 'dom-nodes',
  142. lineStyle: {
  143. opacity: 0.75,
  144. width: 1,
  145. },
  146. },
  147. {
  148. id: 'currentTime',
  149. seriesName: t('Current player time'),
  150. data: [],
  151. markLine: {
  152. symbol: ['', ''],
  153. data: [],
  154. label: {
  155. show: false,
  156. },
  157. lineStyle: {
  158. type: 'solid' as const,
  159. color: theme.purple300,
  160. width: 2,
  161. },
  162. },
  163. },
  164. {
  165. id: 'hoverTime',
  166. seriesName: t('Hover player time'),
  167. data: [],
  168. markLine: {
  169. symbol: ['', ''],
  170. data: [],
  171. label: {
  172. show: false,
  173. },
  174. lineStyle: {
  175. type: 'solid' as const,
  176. color: theme.purple200,
  177. width: 2,
  178. },
  179. },
  180. },
  181. ];
  182. return (
  183. <DomNodesChartWrapper id="replay-dom-nodes-chart">
  184. <AreaChart forwardedRef={forwardedRef} series={series} {...chartOptions} />
  185. </DomNodesChartWrapper>
  186. );
  187. }
  188. const DomNodesChartWrapper = styled(FluidHeight)`
  189. border: 1px solid ${p => p.theme.border};
  190. border-radius: ${space(0.5)};
  191. justify-content: center;
  192. padding: ${space(1)};
  193. `;
  194. const MemoizedDomNodesChart = memo(
  195. forwardRef<ReactEchartsRef, Props>((props, ref) => (
  196. <DomNodesChart forwardedRef={ref} {...props} />
  197. ))
  198. );
  199. function useCountDomNodes({replay}: {replay: null | ReplayReader}) {
  200. return useQuery(
  201. ['countDomNodes', replay],
  202. () =>
  203. countDomNodes({
  204. frames: replay?.getRRWebMutations(),
  205. rrwebEvents: replay?.getRRWebFrames(),
  206. startTimestampMs: replay?.getReplay().started_at.getTime() ?? 0,
  207. }),
  208. {enabled: Boolean(replay), cacheTime: Infinity}
  209. );
  210. }
  211. function DomNodesChartContainer() {
  212. const {currentTime, currentHoverTime, replay, setCurrentTime, setCurrentHoverTime} =
  213. useReplayContext();
  214. const chart = useRef<ReactEchartsRef>(null);
  215. const theme = useTheme();
  216. const {data: frameToCount} = useCountDomNodes({replay});
  217. const startTimestampMs = replay?.getReplay()?.started_at?.getTime() ?? 0;
  218. const datapoints = useMemo(
  219. () => Array.from(frameToCount?.values() || []),
  220. [frameToCount]
  221. );
  222. useEffect(() => {
  223. if (!chart.current) {
  224. return;
  225. }
  226. const echarts = chart.current.getEchartsInstance();
  227. echarts.setOption({
  228. series: [
  229. {
  230. id: 'currentTime',
  231. markLine: {
  232. data: [
  233. {
  234. xAxis: currentTime + startTimestampMs,
  235. },
  236. ],
  237. },
  238. },
  239. ],
  240. });
  241. }, [currentTime, startTimestampMs, theme]);
  242. useEffect(() => {
  243. if (!chart.current) {
  244. return;
  245. }
  246. const echarts = chart.current.getEchartsInstance();
  247. echarts.setOption({
  248. series: [
  249. {
  250. id: 'hoverTime',
  251. markLine: {
  252. data: [
  253. ...(currentHoverTime
  254. ? [
  255. {
  256. xAxis: currentHoverTime + startTimestampMs,
  257. },
  258. ]
  259. : []),
  260. ],
  261. },
  262. },
  263. ],
  264. });
  265. }, [currentHoverTime, startTimestampMs, theme]);
  266. return (
  267. <MemoizedDomNodesChart
  268. ref={chart}
  269. datapoints={datapoints}
  270. setCurrentHoverTime={setCurrentHoverTime}
  271. setCurrentTime={setCurrentTime}
  272. startTimestampMs={startTimestampMs}
  273. />
  274. );
  275. }
  276. export default DomNodesChartContainer;