domNodesChart.tsx 7.8 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 type {AreaChartProps} from 'sentry/components/charts/areaChart';
  6. import {AreaChart} from 'sentry/components/charts/areaChart';
  7. import Grid from 'sentry/components/charts/components/grid';
  8. import {ChartTooltip} from 'sentry/components/charts/components/tooltip';
  9. import XAxis from 'sentry/components/charts/components/xAxis';
  10. import YAxis from 'sentry/components/charts/components/yAxis';
  11. import Placeholder from 'sentry/components/placeholder';
  12. import {useReplayContext} from 'sentry/components/replays/replayContext';
  13. import {showPlayerTime} from 'sentry/components/replays/utils';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {ReactEchartsRef, Series} from 'sentry/types/echarts';
  17. import {getFormattedDate} from 'sentry/utils/dates';
  18. import {axisLabelFormatter} from 'sentry/utils/discover/charts';
  19. import {useQuery} from 'sentry/utils/queryClient';
  20. import type {DomNodeChartDatapoint} from 'sentry/utils/replays/countDomNodes';
  21. import countDomNodes from 'sentry/utils/replays/countDomNodes';
  22. import type ReplayReader from 'sentry/utils/replays/replayReader';
  23. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  24. interface Props {
  25. datapoints: DomNodeChartDatapoint[];
  26. setCurrentHoverTime: (time: undefined | number) => void;
  27. setCurrentTime: (time: number) => void;
  28. startTimestampMs: undefined | number;
  29. }
  30. interface DomNodesChartProps extends Props {
  31. forwardedRef: React.Ref<ReactEchartsRef>;
  32. }
  33. const formatTimestamp = timestamp =>
  34. getFormattedDate(timestamp, 'MMM D, YYYY hh:mm:ss A z', {local: false});
  35. const formatTimestampTrim = timestamp =>
  36. getFormattedDate(timestamp, 'MMM D hh:mm', {local: false});
  37. function DomNodesChart({
  38. forwardedRef,
  39. datapoints,
  40. startTimestampMs = 0,
  41. setCurrentTime,
  42. setCurrentHoverTime,
  43. }: DomNodesChartProps) {
  44. const theme = useTheme();
  45. if (!datapoints) {
  46. return null;
  47. }
  48. if (!datapoints.length) {
  49. return (
  50. <DomNodesChartWrapper>
  51. <Placeholder height="100%" />
  52. </DomNodesChartWrapper>
  53. );
  54. }
  55. const chartOptions: Omit<AreaChartProps, 'series'> = {
  56. grid: Grid({
  57. top: '40px',
  58. left: space(1),
  59. right: space(1),
  60. }),
  61. tooltip: ChartTooltip({
  62. appendToBody: true,
  63. trigger: 'axis',
  64. renderMode: 'html',
  65. chartId: 'replay-dom-nodes-chart',
  66. formatter: values => {
  67. const seriesTooltips = values.map(
  68. value => `
  69. <div>
  70. <span className="tooltip-label">${value.marker}<strong>${value.seriesName}</strong></span>
  71. ${value.data[1]}
  72. </div>
  73. `
  74. );
  75. const template = [
  76. '<div class="tooltip-series">',
  77. ...seriesTooltips,
  78. '</div>',
  79. `<div class="tooltip-footer" style="display: inline-block; width: max-content;">${t(
  80. 'Span Time'
  81. )}:
  82. ${formatTimestamp(values[0].axisValue)}
  83. </div>`,
  84. `<div class="tooltip-footer" style="border: none;">${'Relative Time'}:
  85. ${showPlayerTime(
  86. moment(values[0].axisValue).toDate().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;