memoryChart.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import type {Dispatch, SetStateAction} from 'react';
  2. import {useMemo, useRef} from 'react';
  3. import {useTheme} from '@emotion/react';
  4. import type {AreaChartProps, AreaChartSeries} from 'sentry/components/charts/areaChart';
  5. import {AreaChart} from 'sentry/components/charts/areaChart';
  6. import Grid from 'sentry/components/charts/components/grid';
  7. import {computeChartTooltip} 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 type {useReplayContext} from 'sentry/components/replays/replayContext';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import toArray from 'sentry/utils/array/toArray';
  14. import {formatBytesBase2} from 'sentry/utils/bytes/formatBytesBase2';
  15. import {getFormattedDate} from 'sentry/utils/dates';
  16. import domId from 'sentry/utils/domId';
  17. import formatDuration from 'sentry/utils/duration/formatDuration';
  18. import type {MemoryFrame} from 'sentry/utils/replays/types';
  19. interface Props
  20. extends Pick<ReturnType<typeof useReplayContext>, 'currentTime' | 'setCurrentTime'> {
  21. currentHoverTime: undefined | number;
  22. durationMs: number;
  23. memoryFrames: MemoryFrame[];
  24. setCurrentHoverTime: Dispatch<SetStateAction<number | undefined>>;
  25. startTimestampMs: number;
  26. }
  27. export default function MemoryChart({
  28. currentHoverTime,
  29. currentTime,
  30. durationMs,
  31. memoryFrames,
  32. setCurrentHoverTime,
  33. setCurrentTime,
  34. startTimestampMs,
  35. }: Props) {
  36. const theme = useTheme();
  37. const idRef = useRef(domId('replay-memory-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. ${formatBytesBase2(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(
  69. 'Time within replay: %s',
  70. formatDuration({
  71. duration: [firstValue.axisValue, 'ms'],
  72. precision: 'ms',
  73. style: 'hh:mm:ss.sss',
  74. })
  75. )}
  76. </div>
  77. <div class="tooltip-arrow"></div>
  78. `;
  79. },
  80. },
  81. theme
  82. ),
  83. xAxis: XAxis({
  84. type: 'time',
  85. axisLabel: {
  86. formatter: (time: number) =>
  87. formatDuration({
  88. duration: [time, 'ms'],
  89. precision: 'sec',
  90. style: 'hh:mm:ss',
  91. }),
  92. },
  93. theme,
  94. }),
  95. yAxis: YAxis({
  96. type: 'value',
  97. theme,
  98. minInterval: 1024 * 1024, // input is in bytes, minInterval is a megabyte
  99. maxInterval: Math.pow(1024, 4), // maxInterval is a terabyte
  100. axisLabel: {
  101. // format the axis labels to be whole number values
  102. formatter: value => formatBytesBase2(value, 0),
  103. },
  104. }),
  105. onMouseOver: ({data}) => {
  106. if (data[0]) {
  107. setCurrentHoverTime(data[0]);
  108. }
  109. },
  110. onMouseOut: () => {
  111. setCurrentHoverTime(undefined);
  112. },
  113. onClick: ({data}) => {
  114. if (data.value) {
  115. setCurrentTime(data.value);
  116. }
  117. },
  118. }),
  119. [setCurrentHoverTime, setCurrentTime, startTimestampMs, theme]
  120. );
  121. const staticSeries = useMemo<AreaChartSeries[]>(
  122. () => [
  123. {
  124. id: 'usedMemory',
  125. seriesName: t('Used Heap Memory'),
  126. data: memoryFrames.map(frame => ({
  127. value: frame.data.memory.usedJSHeapSize,
  128. name: frame.offsetMs,
  129. })),
  130. stack: 'heap-memory',
  131. lineStyle: {opacity: 0, width: 2},
  132. },
  133. {
  134. id: 'replayStart',
  135. seriesName: 'Replay Start',
  136. data: [{value: 0, name: 0}],
  137. lineStyle: {opacity: 0, width: 0},
  138. },
  139. {
  140. id: 'replayEnd',
  141. seriesName: 'Replay End',
  142. data: [{value: 0, name: durationMs}],
  143. lineStyle: {opacity: 0, width: 0},
  144. },
  145. ],
  146. [durationMs, memoryFrames]
  147. );
  148. const dynamicSeries = useMemo<AreaChartSeries[]>(
  149. () => [
  150. {
  151. id: 'currentTime',
  152. seriesName: t('Current player time'),
  153. data: [],
  154. markLine: {
  155. symbol: ['', ''],
  156. data: [{xAxis: currentTime}],
  157. label: {show: false},
  158. lineStyle: {type: 'solid', color: theme.purple300, width: 2},
  159. },
  160. },
  161. {
  162. id: 'hoverTime',
  163. seriesName: t('Hover player time'),
  164. data: [],
  165. markLine: {
  166. symbol: ['', ''],
  167. data: currentHoverTime ? [{xAxis: currentHoverTime}] : [],
  168. label: {show: false},
  169. lineStyle: {type: 'solid', color: theme.purple200, width: 2},
  170. },
  171. },
  172. ],
  173. [currentTime, currentHoverTime, theme.purple200, theme.purple300]
  174. );
  175. const series = useMemo(
  176. () => staticSeries.concat(dynamicSeries),
  177. [dynamicSeries, staticSeries]
  178. );
  179. return (
  180. <div id={idRef.current}>
  181. <AreaChart series={series} {...chartOptions} />
  182. </div>
  183. );
  184. }