memoryChart.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 {formatBytesBase2} from 'sentry/utils';
  14. import toArray from 'sentry/utils/array/toArray';
  15. import {getFormattedDate} from 'sentry/utils/dates';
  16. import domId from 'sentry/utils/domId';
  17. import type {MemoryFrame} from 'sentry/utils/replays/types';
  18. interface Props
  19. extends Pick<
  20. ReturnType<typeof useReplayContext>,
  21. 'currentTime' | 'currentHoverTime' | 'setCurrentTime' | 'setCurrentHoverTime'
  22. > {
  23. durationMs: number;
  24. memoryFrames: MemoryFrame[];
  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('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: 1024 * 1024, // input is in bytes, minInterval is a megabyte
  87. maxInterval: Math.pow(1024, 4), // maxInterval is a terabyte
  88. axisLabel: {
  89. // format the axis labels to be whole number values
  90. formatter: value => formatBytesBase2(value, 0),
  91. },
  92. }),
  93. onMouseOver: ({data}) => {
  94. if (data[0]) {
  95. setCurrentHoverTime(data[0]);
  96. }
  97. },
  98. onMouseOut: () => {
  99. setCurrentHoverTime(undefined);
  100. },
  101. onClick: ({data}) => {
  102. if (data.value) {
  103. setCurrentTime(data.value);
  104. }
  105. },
  106. }),
  107. [setCurrentHoverTime, setCurrentTime, startTimestampMs, theme]
  108. );
  109. const staticSeries = useMemo<AreaChartSeries[]>(
  110. () => [
  111. {
  112. id: 'usedMemory',
  113. seriesName: t('Used Heap Memory'),
  114. data: memoryFrames.map(frame => ({
  115. value: frame.data.memory.usedJSHeapSize,
  116. name: frame.offsetMs,
  117. })),
  118. stack: 'heap-memory',
  119. lineStyle: {opacity: 0, width: 2},
  120. },
  121. {
  122. id: 'replayStart',
  123. seriesName: 'Replay Start',
  124. data: [{value: 0, name: 0}],
  125. lineStyle: {opacity: 0, width: 0},
  126. },
  127. {
  128. id: 'replayEnd',
  129. seriesName: 'Replay End',
  130. data: [{value: 0, name: durationMs}],
  131. lineStyle: {opacity: 0, width: 0},
  132. },
  133. ],
  134. [durationMs, memoryFrames]
  135. );
  136. const dynamicSeries = useMemo<AreaChartSeries[]>(
  137. () => [
  138. {
  139. id: 'currentTime',
  140. seriesName: t('Current player time'),
  141. data: [],
  142. markLine: {
  143. symbol: ['', ''],
  144. data: [{xAxis: currentTime}],
  145. label: {show: false},
  146. lineStyle: {type: 'solid', color: theme.purple300, width: 2},
  147. },
  148. },
  149. {
  150. id: 'hoverTime',
  151. seriesName: t('Hover player time'),
  152. data: [],
  153. markLine: {
  154. symbol: ['', ''],
  155. data: currentHoverTime ? [{xAxis: currentHoverTime}] : [],
  156. label: {show: false},
  157. lineStyle: {type: 'solid', color: theme.purple200, width: 2},
  158. },
  159. },
  160. ],
  161. [currentTime, currentHoverTime, theme.purple200, theme.purple300]
  162. );
  163. const series = useMemo(
  164. () => staticSeries.concat(dynamicSeries),
  165. [dynamicSeries, staticSeries]
  166. );
  167. return (
  168. <div id={idRef.current}>
  169. <AreaChart series={series} {...chartOptions} />
  170. </div>
  171. );
  172. }