memoryChart.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import {forwardRef, memo, useEffect, useRef} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import moment from 'moment';
  5. import {AreaChart, AreaChartProps} from 'sentry/components/charts/areaChart';
  6. import Grid from 'sentry/components/charts/components/grid';
  7. import {ChartTooltip} 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 EmptyMessage from 'sentry/components/emptyMessage';
  11. import Placeholder from 'sentry/components/placeholder';
  12. import {showPlayerTime} from 'sentry/components/replays/utils';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {ReactEchartsRef, Series} from 'sentry/types/echarts';
  16. import {formatBytesBase2} from 'sentry/utils';
  17. import {getFormattedDate} from 'sentry/utils/dates';
  18. import type {MemoryFrame} from 'sentry/utils/replays/types';
  19. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  20. interface Props {
  21. memoryFrames: undefined | MemoryFrame[];
  22. setCurrentHoverTime: (time: undefined | number) => void;
  23. setCurrentTime: (time: number) => void;
  24. startTimestampMs: undefined | number;
  25. }
  26. interface MemoryChartProps extends Props {
  27. forwardedRef: React.Ref<ReactEchartsRef>;
  28. }
  29. const formatTimestamp = timestamp =>
  30. getFormattedDate(timestamp, 'MMM D, YYYY hh:mm:ss A z', {local: false});
  31. function MemoryChart({
  32. forwardedRef,
  33. memoryFrames,
  34. startTimestampMs = 0,
  35. setCurrentTime,
  36. setCurrentHoverTime,
  37. }: MemoryChartProps) {
  38. const theme = useTheme();
  39. if (!memoryFrames) {
  40. return (
  41. <MemoryChartWrapper>
  42. <Placeholder height="100%" />
  43. </MemoryChartWrapper>
  44. );
  45. }
  46. if (!memoryFrames.length) {
  47. return (
  48. <EmptyMessage
  49. data-test-id="replay-details-memory-tab"
  50. title={t('No memory metrics found')}
  51. description={t(
  52. 'Memory metrics are only captured within Chromium based browser sessions.'
  53. )}
  54. />
  55. );
  56. }
  57. const chartOptions: Omit<AreaChartProps, 'series'> = {
  58. grid: Grid({
  59. // makes space for the title
  60. top: '40px',
  61. left: space(1),
  62. right: space(1),
  63. }),
  64. tooltip: ChartTooltip({
  65. appendToBody: true,
  66. trigger: 'axis',
  67. renderMode: 'html',
  68. chartId: 'replay-memory-chart',
  69. formatter: values => {
  70. const seriesTooltips = values.map(
  71. value => `
  72. <div>
  73. <span className="tooltip-label">${value.marker}<strong>${
  74. value.seriesName
  75. }</strong></span>
  76. ${formatBytesBase2(value.data[1])}
  77. </div>
  78. `
  79. );
  80. // showPlayerTime expects a timestamp so we take the captured time in seconds and convert it to a UTC timestamp
  81. const template = [
  82. '<div class="tooltip-series">',
  83. ...seriesTooltips,
  84. '</div>',
  85. `<div class="tooltip-footer" style="display: inline-block; width: max-content;">${t(
  86. 'Span Time'
  87. )}:
  88. ${formatTimestamp(values[0].axisValue)}
  89. </div>`,
  90. `<div class="tooltip-footer" style="border: none;">${'Relative Time'}:
  91. ${showPlayerTime(
  92. moment(values[0].axisValue).toDate().toUTCString(),
  93. startTimestampMs
  94. )}
  95. </div>`,
  96. '<div class="tooltip-arrow"></div>',
  97. ].join('');
  98. return template;
  99. },
  100. }),
  101. xAxis: XAxis({
  102. type: 'time',
  103. axisLabel: {
  104. formatter: formatTimestamp,
  105. },
  106. theme,
  107. }),
  108. yAxis: YAxis({
  109. type: 'value',
  110. name: t('Heap Size'),
  111. theme,
  112. nameTextStyle: {
  113. padding: 8,
  114. fontSize: theme.fontSizeLarge,
  115. fontWeight: 600,
  116. lineHeight: 1.2,
  117. color: theme.gray300,
  118. },
  119. // input is in bytes, minInterval is a megabyte
  120. minInterval: 1024 * 1024,
  121. // maxInterval is a terabyte
  122. maxInterval: Math.pow(1024, 4),
  123. // format the axis labels to be whole number values
  124. axisLabel: {
  125. formatter: value => formatBytesBase2(value, 0),
  126. },
  127. }),
  128. // XXX: For area charts, mouse events *only* occurs when interacting with
  129. // the "line" of the area chart. Mouse events do not fire when interacting
  130. // with the "area" under the line.
  131. onMouseOver: ({data}) => {
  132. if (data[0]) {
  133. setCurrentHoverTime(data[0] - startTimestampMs);
  134. }
  135. },
  136. onMouseOut: () => {
  137. setCurrentHoverTime(undefined);
  138. },
  139. onClick: ({data}) => {
  140. if (data.value) {
  141. setCurrentTime(data.value - startTimestampMs);
  142. }
  143. },
  144. };
  145. const series: Series[] = [
  146. {
  147. seriesName: t('Used Heap Memory'),
  148. data: memoryFrames.map(frame => ({
  149. value: frame.data.memory.usedJSHeapSize,
  150. name: frame.endTimestampMs,
  151. })),
  152. stack: 'heap-memory',
  153. lineStyle: {
  154. opacity: 0.75,
  155. width: 1,
  156. },
  157. },
  158. {
  159. seriesName: t('Free Heap Memory'),
  160. data: memoryFrames.map(frame => ({
  161. value: frame.data.memory.totalJSHeapSize - frame.data.memory.usedJSHeapSize,
  162. name: frame.endTimestampMs,
  163. })),
  164. stack: 'heap-memory',
  165. lineStyle: {
  166. opacity: 0.75,
  167. width: 1,
  168. },
  169. },
  170. // Inserting this here so we can update in Container
  171. {
  172. id: 'currentTime',
  173. seriesName: t('Current player time'),
  174. data: [],
  175. markLine: {
  176. symbol: ['', ''],
  177. data: [],
  178. label: {
  179. show: false,
  180. },
  181. lineStyle: {
  182. type: 'solid' as const,
  183. color: theme.purple300,
  184. width: 2,
  185. },
  186. },
  187. },
  188. {
  189. id: 'hoverTime',
  190. seriesName: t('Hover player time'),
  191. data: [],
  192. markLine: {
  193. symbol: ['', ''],
  194. data: [],
  195. label: {
  196. show: false,
  197. },
  198. lineStyle: {
  199. type: 'solid' as const,
  200. color: theme.purple200,
  201. width: 2,
  202. },
  203. },
  204. },
  205. ];
  206. return (
  207. <MemoryChartWrapper id="replay-memory-chart">
  208. <AreaChart forwardedRef={forwardedRef} series={series} {...chartOptions} />
  209. </MemoryChartWrapper>
  210. );
  211. }
  212. const MemoryChartWrapper = styled(FluidHeight)`
  213. border-radius: ${space(0.5)};
  214. border: 1px solid ${p => p.theme.border};
  215. `;
  216. const MemoizedMemoryChart = memo(
  217. forwardRef<ReactEchartsRef, Props>((props, ref) => (
  218. <MemoryChart forwardedRef={ref} {...props} />
  219. ))
  220. );
  221. interface MemoryChartContainerProps extends Props {
  222. currentHoverTime: number | undefined;
  223. currentTime: number;
  224. }
  225. /**
  226. * This container is used to update echarts outside of React. `currentTime` is
  227. * the current time of the player -- if replay is currently playing, this will
  228. * be updated quite frequently causing the chart to constantly re-render. The
  229. * re-renders will conflict with mouse interactions (e.g. hovers and tooltips).
  230. *
  231. * We need `MemoryChart` (which wraps an `<AreaChart>`) to re-render as
  232. * infrequently as possible, so we use React.memo and only pass in props that
  233. * are not frequently updated.
  234. */
  235. function MemoryChartContainer({
  236. currentTime,
  237. currentHoverTime,
  238. startTimestampMs = 0,
  239. ...props
  240. }: MemoryChartContainerProps) {
  241. const chart = useRef<ReactEchartsRef>(null);
  242. const theme = useTheme();
  243. useEffect(() => {
  244. if (!chart.current) {
  245. return;
  246. }
  247. const echarts = chart.current.getEchartsInstance();
  248. echarts.setOption({
  249. series: [
  250. {
  251. id: 'currentTime',
  252. markLine: {
  253. data: [
  254. {
  255. xAxis: currentTime + startTimestampMs,
  256. },
  257. ],
  258. },
  259. },
  260. ],
  261. });
  262. }, [currentTime, startTimestampMs, theme]);
  263. useEffect(() => {
  264. if (!chart.current) {
  265. return;
  266. }
  267. const echarts = chart.current.getEchartsInstance();
  268. echarts.setOption({
  269. series: [
  270. {
  271. id: 'hoverTime',
  272. markLine: {
  273. data: [
  274. ...(currentHoverTime
  275. ? [
  276. {
  277. xAxis: currentHoverTime + startTimestampMs,
  278. },
  279. ]
  280. : []),
  281. ],
  282. },
  283. },
  284. ],
  285. });
  286. }, [currentHoverTime, startTimestampMs, theme]);
  287. return (
  288. <MemoizedMemoryChart ref={chart} startTimestampMs={startTimestampMs} {...props} />
  289. );
  290. }
  291. export default MemoryChartContainer;