domNodesChart.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {useMemo, useRef} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import type {AreaChartProps, AreaChartSeries} from 'sentry/components/charts/areaChart';
  4. import {AreaChart} from 'sentry/components/charts/areaChart';
  5. import Grid from 'sentry/components/charts/components/grid';
  6. import {computeChartTooltip} from 'sentry/components/charts/components/tooltip';
  7. import XAxis from 'sentry/components/charts/components/xAxis';
  8. import YAxis from 'sentry/components/charts/components/yAxis';
  9. import type {useReplayContext} from 'sentry/components/replays/replayContext';
  10. import {formatTime} from 'sentry/components/replays/utils';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {getFormattedDate} from 'sentry/utils/dates';
  14. import {axisLabelFormatter} from 'sentry/utils/discover/charts';
  15. import domId from 'sentry/utils/domId';
  16. import type {DomNodeChartDatapoint} from 'sentry/utils/replays/countDomNodes';
  17. import toArray from 'sentry/utils/toArray';
  18. interface Props
  19. extends Pick<
  20. ReturnType<typeof useReplayContext>,
  21. 'currentTime' | 'currentHoverTime' | 'setCurrentTime' | 'setCurrentHoverTime'
  22. > {
  23. datapoints: DomNodeChartDatapoint[];
  24. durationMs: number;
  25. startTimestampMs: number;
  26. }
  27. export default function DomNodesChart({
  28. currentHoverTime,
  29. currentTime,
  30. durationMs,
  31. datapoints,
  32. setCurrentHoverTime,
  33. setCurrentTime,
  34. startTimestampMs,
  35. }: Props) {
  36. const theme = useTheme();
  37. const idRef = useRef(domId('replay-dom-nodes-chart-'));
  38. const chartOptions: Omit<AreaChartProps, 'series'> = useMemo(
  39. () => ({
  40. autoHeightResize: true,
  41. height: 'auto',
  42. grid: Grid({
  43. left: space(1),
  44. right: space(1),
  45. }),
  46. tooltip: computeChartTooltip(
  47. {
  48. appendToBody: true,
  49. trigger: 'axis',
  50. renderMode: 'html',
  51. chartId: idRef.current,
  52. formatter: values => {
  53. const firstValue = Array.isArray(values) ? values[0] : values;
  54. const seriesTooltips = toArray(values).map(
  55. value => `
  56. <div>
  57. <span className="tooltip-label">${value.marker}<strong>${value.seriesName}</strong></span>
  58. ${value.data[1]}
  59. </div>
  60. `
  61. );
  62. return `
  63. <div class="tooltip-series">${seriesTooltips.join('')}</div>
  64. <div class="tooltip-footer">
  65. ${t('Date: %s', getFormattedDate(startTimestampMs + firstValue.axisValue, 'MMM D, YYYY hh:mm:ss A z', {local: false}))}
  66. </div>
  67. <div class="tooltip-footer" style="border: none;">
  68. ${t('Time within replay: %s', formatTime(firstValue.axisValue))}
  69. </div>
  70. <div class="tooltip-arrow"></div>
  71. `;
  72. },
  73. },
  74. theme
  75. ),
  76. xAxis: XAxis({
  77. type: 'time',
  78. axisLabel: {
  79. formatter: (time: number) => formatTime(startTimestampMs + time, false),
  80. },
  81. theme,
  82. }),
  83. yAxis: YAxis({
  84. type: 'value',
  85. theme,
  86. minInterval: 100,
  87. maxInterval: 10_000,
  88. axisLabel: {
  89. formatter: (value: number) => axisLabelFormatter(value, 'number', true),
  90. },
  91. }),
  92. onMouseOver: ({data}) => {
  93. if (data[0]) {
  94. setCurrentHoverTime(data[0]);
  95. }
  96. },
  97. onMouseOut: () => {
  98. setCurrentHoverTime(undefined);
  99. },
  100. onClick: ({data}) => {
  101. if (data.value) {
  102. setCurrentTime(data.value);
  103. }
  104. },
  105. }),
  106. [setCurrentHoverTime, setCurrentTime, startTimestampMs, theme]
  107. );
  108. const staticSeries = useMemo<AreaChartSeries[]>(
  109. () => [
  110. {
  111. id: 'nodeCount',
  112. seriesName: t('Total DOM nodes'),
  113. data: datapoints.map(d => ({
  114. value: d.count,
  115. name: d.endTimestampMs - startTimestampMs,
  116. })),
  117. stack: 'node-count',
  118. lineStyle: {opacity: 1, width: 2},
  119. },
  120. {
  121. id: 'addedCount',
  122. seriesName: t('DOM Nodes added'),
  123. data: datapoints.map(d => ({
  124. value: d.added,
  125. name: d.endTimestampMs - startTimestampMs,
  126. })),
  127. stack: 'added-count',
  128. emphasis: {
  129. focus: 'series',
  130. },
  131. color: theme.green300,
  132. lineStyle: {opacity: 1, color: theme.green300, width: 2},
  133. areaStyle: {opacity: 0, color: 'transparent'},
  134. },
  135. {
  136. id: 'removedCount',
  137. seriesName: t('DOM Nodes removed'),
  138. data: datapoints.map(d => ({
  139. value: d.removed,
  140. name: d.endTimestampMs - startTimestampMs,
  141. })),
  142. stack: 'removed-count',
  143. emphasis: {
  144. focus: 'series',
  145. },
  146. color: theme.red300,
  147. lineStyle: {opacity: 1, color: theme.red300, width: 2},
  148. areaStyle: {opacity: 0, color: 'transparent'},
  149. },
  150. {
  151. id: 'replayStart',
  152. seriesName: 'Replay Start',
  153. data: [{value: 0, name: 0}],
  154. lineStyle: {opacity: 0, width: 0},
  155. },
  156. {
  157. id: 'replayEnd',
  158. seriesName: 'Replay End',
  159. data: [{value: 0, name: durationMs}],
  160. lineStyle: {opacity: 0, width: 0},
  161. },
  162. ],
  163. [datapoints, durationMs, startTimestampMs, theme]
  164. );
  165. const dynamicSeries = useMemo<AreaChartSeries[]>(
  166. () => [
  167. {
  168. id: 'hoverTime',
  169. seriesName: t('Hover player time'),
  170. data: [],
  171. markLine: {
  172. symbol: ['', ''],
  173. data: currentHoverTime ? [{xAxis: currentHoverTime}] : [],
  174. label: {show: false},
  175. lineStyle: {type: 'solid', color: theme.purple200, width: 2},
  176. },
  177. },
  178. {
  179. id: 'currentTime',
  180. seriesName: t('Current player time'),
  181. data: [],
  182. markLine: {
  183. symbol: ['', ''],
  184. data: [{xAxis: currentTime}],
  185. label: {show: false},
  186. lineStyle: {type: 'solid', color: theme.purple300, width: 2},
  187. },
  188. },
  189. ],
  190. [currentHoverTime, currentTime, theme.purple200, theme.purple300]
  191. );
  192. const series = useMemo(
  193. () => staticSeries.concat(dynamicSeries),
  194. [dynamicSeries, staticSeries]
  195. );
  196. return (
  197. <div id={idRef.current}>
  198. <AreaChart series={series} {...chartOptions} />
  199. </div>
  200. );
  201. }