domNodesChart.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {useMemo} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import type {AreaChartProps} from 'sentry/components/charts/areaChart';
  4. import {AreaChart} from 'sentry/components/charts/areaChart';
  5. import Grid from 'sentry/components/charts/components/grid';
  6. import {ChartTooltip} 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 type {Series} from 'sentry/types/echarts';
  14. import {getFormattedDate} from 'sentry/utils/dates';
  15. import {axisLabelFormatter} from 'sentry/utils/discover/charts';
  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. startOffsetMs: number;
  26. startTimestampMs: number;
  27. }
  28. export default function DomNodesChart({
  29. currentHoverTime,
  30. currentTime,
  31. durationMs,
  32. datapoints,
  33. setCurrentHoverTime,
  34. setCurrentTime,
  35. startOffsetMs,
  36. startTimestampMs,
  37. }: Props) {
  38. const theme = useTheme();
  39. const chartOptions: Omit<AreaChartProps, 'series'> = {
  40. autoHeightResize: true,
  41. height: 'auto',
  42. grid: Grid({
  43. top: '40px',
  44. left: space(1),
  45. right: space(1),
  46. }),
  47. tooltip: ChartTooltip({
  48. appendToBody: true,
  49. trigger: 'axis',
  50. renderMode: 'html',
  51. chartId: 'replay-dom-nodes-chart',
  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(startOffsetMs + 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. xAxis: XAxis({
  75. type: 'time',
  76. axisLabel: {
  77. formatter: (time: number) => formatTime(time, false),
  78. },
  79. theme,
  80. }),
  81. yAxis: YAxis({
  82. type: 'value',
  83. name: t('DOM Nodes'),
  84. theme,
  85. nameTextStyle: {
  86. padding: [8, 8, 8, 48],
  87. fontSize: theme.fontSizeLarge,
  88. fontWeight: 600,
  89. lineHeight: 1.2,
  90. fontFamily: theme.text.family,
  91. color: theme.gray300,
  92. },
  93. minInterval: 100,
  94. maxInterval: Math.pow(1024, 4),
  95. axisLabel: {
  96. formatter: (value: number) => axisLabelFormatter(value, 'number', true),
  97. },
  98. }),
  99. onMouseOver: ({data}) => {
  100. if (data[0]) {
  101. setCurrentHoverTime(data[0]);
  102. }
  103. },
  104. onMouseOut: () => {
  105. setCurrentHoverTime(undefined);
  106. },
  107. onClick: ({data}) => {
  108. if (data.value) {
  109. setCurrentTime(data.value);
  110. }
  111. },
  112. };
  113. const staticSeries: Series[] = useMemo(
  114. () => [
  115. {
  116. id: 'nodeCount',
  117. seriesName: t('Number of DOM nodes'),
  118. data: datapoints.map(d => ({
  119. value: d.count,
  120. name: d.endTimestampMs - startTimestampMs,
  121. })),
  122. stack: 'dom-nodes',
  123. lineStyle: {opacity: 0, width: 2},
  124. },
  125. {
  126. id: 'replayStart',
  127. seriesName: 'Replay Start',
  128. data: [{value: 0, name: 0}],
  129. lineStyle: {opacity: 0, width: 0},
  130. },
  131. {
  132. id: 'replayEnd',
  133. seriesName: 'Replay End',
  134. data: [{value: 0, name: durationMs}],
  135. lineStyle: {opacity: 0, width: 0},
  136. },
  137. ],
  138. [datapoints, durationMs, startTimestampMs]
  139. );
  140. const dynamicSeries = useMemo(
  141. (): Series[] => [
  142. {
  143. id: 'hoverTime',
  144. seriesName: t('Hover player time'),
  145. data: [],
  146. markLine: {
  147. symbol: ['', ''],
  148. data: currentHoverTime ? [{xAxis: currentHoverTime}] : [],
  149. label: {show: false},
  150. lineStyle: {type: 'solid', color: theme.purple200, width: 2},
  151. },
  152. },
  153. {
  154. id: 'currentTime',
  155. seriesName: t('Current player time'),
  156. data: [],
  157. markLine: {
  158. symbol: ['', ''],
  159. data: [{xAxis: currentTime}],
  160. label: {show: false},
  161. lineStyle: {type: 'solid', color: theme.purple300, width: 2},
  162. },
  163. },
  164. ],
  165. [currentHoverTime, currentTime, theme.purple200, theme.purple300]
  166. );
  167. const series = useMemo(
  168. () => staticSeries.concat(dynamicSeries),
  169. [dynamicSeries, staticSeries]
  170. );
  171. return <AreaChart series={series} {...chartOptions} />;
  172. }