domNodesChart.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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).toDate().toUTCString(),
  85. startTimestampMs
  86. )}
  87. </div>`,
  88. '<div class="tooltip-arrow"></div>',
  89. ].join('');
  90. return template;
  91. },
  92. }),
  93. xAxis: XAxis({
  94. type: 'time',
  95. axisLabel: {
  96. formatter: formatTimestampTrim,
  97. },
  98. theme,
  99. }),
  100. yAxis: YAxis({
  101. type: 'value',
  102. name: t('DOM Nodes'),
  103. theme,
  104. nameTextStyle: {
  105. padding: [8, 8, 8, 48],
  106. fontSize: theme.fontSizeLarge,
  107. fontWeight: 600,
  108. lineHeight: 1.2,
  109. fontFamily: theme.text.family,
  110. color: theme.gray300,
  111. },
  112. minInterval: 100,
  113. maxInterval: Math.pow(1024, 4),
  114. axisLabel: {
  115. formatter: (value: number) => axisLabelFormatter(value, 'number', true),
  116. },
  117. }),
  118. onMouseOver: ({data}) => {
  119. if (data[0]) {
  120. setCurrentHoverTime(data[0] - startTimestampMs);
  121. }
  122. },
  123. onMouseOut: () => {
  124. setCurrentHoverTime(undefined);
  125. },
  126. onClick: ({data}) => {
  127. if (data.value) {
  128. setCurrentTime(data.value - startTimestampMs);
  129. }
  130. },
  131. };
  132. const series: Series[] = [
  133. {
  134. seriesName: t('Number of DOM nodes'),
  135. data: datapoints.map(d => ({
  136. value: d.count,
  137. name: d.endTimestampMs,
  138. })),
  139. stack: 'dom-nodes',
  140. lineStyle: {
  141. opacity: 0.75,
  142. width: 1,
  143. },
  144. },
  145. {
  146. id: 'currentTime',
  147. seriesName: t('Current player time'),
  148. data: [],
  149. markLine: {
  150. symbol: ['', ''],
  151. data: [],
  152. label: {
  153. show: false,
  154. },
  155. lineStyle: {
  156. type: 'solid' as const,
  157. color: theme.purple300,
  158. width: 2,
  159. },
  160. },
  161. },
  162. {
  163. id: 'hoverTime',
  164. seriesName: t('Hover player time'),
  165. data: [],
  166. markLine: {
  167. symbol: ['', ''],
  168. data: [],
  169. label: {
  170. show: false,
  171. },
  172. lineStyle: {
  173. type: 'solid' as const,
  174. color: theme.purple200,
  175. width: 2,
  176. },
  177. },
  178. },
  179. ];
  180. return (
  181. <DomNodesChartWrapper id="replay-dom-nodes-chart">
  182. <AreaChart forwardedRef={forwardedRef} series={series} {...chartOptions} />
  183. </DomNodesChartWrapper>
  184. );
  185. }
  186. const DomNodesChartWrapper = styled(FluidHeight)`
  187. border: 1px solid ${p => p.theme.border};
  188. border-radius: ${space(0.5)};
  189. justify-content: center;
  190. padding: ${space(1)};
  191. `;
  192. const MemoizedDomNodesChart = memo(
  193. forwardRef<ReactEchartsRef, Props>((props, ref) => (
  194. <DomNodesChart forwardedRef={ref} {...props} />
  195. ))
  196. );
  197. function useCountDomNodes({replay}: {replay: null | ReplayReader}) {
  198. return useQuery(
  199. ['countDomNodes', replay],
  200. () =>
  201. countDomNodes({
  202. frames: replay?.getRRWebMutations(),
  203. rrwebEvents: replay?.getRRWebFrames(),
  204. startTimestampMs: replay?.getReplay().started_at.getTime() ?? 0,
  205. }),
  206. {enabled: Boolean(replay), cacheTime: Infinity}
  207. );
  208. }
  209. function DomNodesChartContainer() {
  210. const {currentTime, currentHoverTime, replay, setCurrentTime, setCurrentHoverTime} =
  211. useReplayContext();
  212. const chart = useRef<ReactEchartsRef>(null);
  213. const theme = useTheme();
  214. const {data: frameToCount} = useCountDomNodes({replay});
  215. const startTimestampMs = replay?.getReplay()?.started_at?.getTime() ?? 0;
  216. const datapoints = useMemo(
  217. () => Array.from(frameToCount?.values() || []),
  218. [frameToCount]
  219. );
  220. useEffect(() => {
  221. if (!chart.current) {
  222. return;
  223. }
  224. const echarts = chart.current.getEchartsInstance();
  225. echarts.setOption({
  226. series: [
  227. {
  228. id: 'currentTime',
  229. markLine: {
  230. data: [
  231. {
  232. xAxis: currentTime + startTimestampMs,
  233. },
  234. ],
  235. },
  236. },
  237. ],
  238. });
  239. }, [currentTime, startTimestampMs, theme]);
  240. useEffect(() => {
  241. if (!chart.current) {
  242. return;
  243. }
  244. const echarts = chart.current.getEchartsInstance();
  245. echarts.setOption({
  246. series: [
  247. {
  248. id: 'hoverTime',
  249. markLine: {
  250. data: [
  251. ...(currentHoverTime
  252. ? [
  253. {
  254. xAxis: currentHoverTime + startTimestampMs,
  255. },
  256. ]
  257. : []),
  258. ],
  259. },
  260. },
  261. ],
  262. });
  263. }, [currentHoverTime, startTimestampMs, theme]);
  264. return (
  265. <MemoizedDomNodesChart
  266. ref={chart}
  267. datapoints={datapoints}
  268. setCurrentHoverTime={setCurrentHoverTime}
  269. setCurrentTime={setCurrentTime}
  270. startTimestampMs={startTimestampMs}
  271. />
  272. );
  273. }
  274. export default DomNodesChartContainer;