memoryChart.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 {formatBytesBase2} from 'sentry/utils';
  15. import {getFormattedDate} from 'sentry/utils/dates';
  16. import type {MemoryFrame} from 'sentry/utils/replays/types';
  17. import toArray from 'sentry/utils/toArray';
  18. interface Props
  19. extends Pick<
  20. ReturnType<typeof useReplayContext>,
  21. 'currentTime' | 'currentHoverTime' | 'setCurrentTime' | 'setCurrentHoverTime'
  22. > {
  23. durationMs: number;
  24. memoryFrames: MemoryFrame[];
  25. startOffsetMs: number;
  26. }
  27. export default function MemoryChart({
  28. currentHoverTime,
  29. currentTime,
  30. durationMs,
  31. memoryFrames,
  32. setCurrentHoverTime,
  33. setCurrentTime,
  34. startOffsetMs,
  35. }: Props) {
  36. const theme = useTheme();
  37. const chartOptions: Omit<AreaChartProps, 'series'> = useMemo(
  38. () => ({
  39. autoHeightResize: true,
  40. height: 'auto',
  41. grid: Grid({
  42. // makes space for the title
  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-memory-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. ${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(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('Heap Size'),
  84. theme,
  85. nameTextStyle: {
  86. padding: [8, 8, 8, -25],
  87. fontSize: theme.fontSizeLarge,
  88. fontWeight: 600,
  89. lineHeight: 1.2,
  90. fontFamily: theme.text.family,
  91. color: theme.gray300,
  92. },
  93. // input is in bytes, minInterval is a megabyte
  94. minInterval: 1024 * 1024,
  95. // maxInterval is a terabyte
  96. maxInterval: Math.pow(1024, 4),
  97. // format the axis labels to be whole number values
  98. axisLabel: {
  99. formatter: value => formatBytesBase2(value, 0),
  100. },
  101. }),
  102. onMouseOver: ({data}) => {
  103. if (data[0]) {
  104. setCurrentHoverTime(data[0]);
  105. }
  106. },
  107. onMouseOut: () => {
  108. setCurrentHoverTime(undefined);
  109. },
  110. onClick: ({data}) => {
  111. if (data.value) {
  112. setCurrentTime(data.value);
  113. }
  114. },
  115. }),
  116. [setCurrentHoverTime, setCurrentTime, startOffsetMs, theme]
  117. );
  118. const staticSeries: Series[] = useMemo(
  119. () => [
  120. {
  121. id: 'usedMemory',
  122. seriesName: t('Used Heap Memory'),
  123. data: memoryFrames.map(frame => ({
  124. value: frame.data.memory.usedJSHeapSize,
  125. name: frame.offsetMs,
  126. })),
  127. stack: 'heap-memory',
  128. lineStyle: {opacity: 0, width: 2},
  129. },
  130. {
  131. id: 'replayStart',
  132. seriesName: 'Replay Start',
  133. data: [{value: 0, name: 0}],
  134. lineStyle: {opacity: 0, width: 0},
  135. },
  136. {
  137. id: 'replayEnd',
  138. seriesName: 'Replay End',
  139. data: [{value: 0, name: durationMs}],
  140. lineStyle: {opacity: 0, width: 0},
  141. },
  142. ],
  143. [durationMs, memoryFrames]
  144. );
  145. const dynamicSeries = useMemo(
  146. (): Series[] => [
  147. {
  148. id: 'currentTime',
  149. seriesName: t('Current player time'),
  150. data: [],
  151. markLine: {
  152. symbol: ['', ''],
  153. data: [{xAxis: currentTime}],
  154. label: {show: false},
  155. lineStyle: {type: 'solid', color: theme.purple300, width: 2},
  156. },
  157. },
  158. {
  159. id: 'hoverTime',
  160. seriesName: t('Hover player time'),
  161. data: [],
  162. markLine: {
  163. symbol: ['', ''],
  164. data: currentHoverTime ? [{xAxis: currentHoverTime}] : [],
  165. label: {show: false},
  166. lineStyle: {type: 'solid', color: theme.purple200, width: 2},
  167. },
  168. },
  169. ],
  170. [currentTime, currentHoverTime, theme.purple200, theme.purple300]
  171. );
  172. const series = useMemo(
  173. () => staticSeries.concat(dynamicSeries),
  174. [dynamicSeries, staticSeries]
  175. );
  176. return (
  177. <div id="replay-memory-chart">
  178. <AreaChart autoHeightResize height="auto" series={series} {...chartOptions} />
  179. </div>
  180. );
  181. }