123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- import {forwardRef, memo, useEffect, useRef} from 'react';
- import {useTheme} from '@emotion/react';
- import styled from '@emotion/styled';
- import moment from 'moment';
- import {AreaChart, AreaChartProps} from 'sentry/components/charts/areaChart';
- import Grid from 'sentry/components/charts/components/grid';
- import Tooltip from 'sentry/components/charts/components/tooltip';
- import XAxis from 'sentry/components/charts/components/xAxis';
- import YAxis from 'sentry/components/charts/components/yAxis';
- import EmptyMessage from 'sentry/components/emptyMessage';
- import {showPlayerTime} from 'sentry/components/replays/utils';
- import {t} from 'sentry/locale';
- import space from 'sentry/styles/space';
- import {ReactEchartsRef, Series} from 'sentry/types/echarts';
- import {formatBytesBase2} from 'sentry/utils';
- import {getFormattedDate} from 'sentry/utils/dates';
- import type {MemorySpanType} from 'sentry/views/replays/types';
- interface Props {
- memorySpans: MemorySpanType[];
- setCurrentHoverTime: (time: undefined | number) => void;
- setCurrentTime: (time: number) => void;
- startTimestampMs: number | undefined;
- }
- interface MemoryChartProps extends Props {
- forwardedRef: React.Ref<ReactEchartsRef>;
- }
- const formatTimestamp = timestamp =>
- getFormattedDate(timestamp * 1000, 'MMM D, YYYY hh:mm:ss A z', {local: false});
- function MemoryChart({
- forwardedRef,
- memorySpans,
- startTimestampMs = 0,
- setCurrentTime,
- setCurrentHoverTime,
- }: MemoryChartProps) {
- const theme = useTheme();
- if (memorySpans.length <= 0) {
- return (
- <EmptyMessage
- title={t('No memory metrics found')}
- description={t(
- 'Memory metrics are only captured within Chromium based browser sessions.'
- )}
- />
- );
- }
- const chartOptions: Omit<AreaChartProps, 'series'> = {
- grid: Grid({
- // makes space for the title
- top: '40px',
- left: space(1),
- right: space(1),
- }),
- tooltip: Tooltip({
- appendToBody: true,
- trigger: 'axis',
- renderMode: 'html',
- chartId: 'replay-memory-chart',
- formatter: values => {
- const seriesTooltips = values.map(
- value => `
- <div>
- <span className="tooltip-label">${value.marker}<strong>${
- value.seriesName
- }</strong></span>
- ${formatBytesBase2(value.data[1])}
- </div>
- `
- );
- // showPlayerTime expects a timestamp so we take the captured time in seconds and convert it to a UTC timestamp
- const template = [
- '<div class="tooltip-series">',
- ...seriesTooltips,
- '</div>',
- `<div class="tooltip-date" style="display: inline-block; width: max-content;">${t(
- 'Span Time'
- )}:
- ${formatTimestamp(values[0].axisValue)}
- </div>`,
- `<div class="tooltip-date" style="border: none;">${'Relative Time'}:
- ${showPlayerTime(
- moment(values[0].axisValue * 1000)
- .toDate()
- .toUTCString(),
- startTimestampMs
- )}
- </div>`,
- '<div class="tooltip-arrow"></div>',
- ].join('');
- return template;
- },
- }),
- xAxis: XAxis({
- type: 'time',
- axisLabel: {
- formatter: formatTimestamp,
- },
- theme,
- }),
- yAxis: YAxis({
- type: 'value',
- name: t('Heap Size'),
- theme,
- nameTextStyle: {
- padding: 8,
- fontSize: theme.fontSizeLarge,
- fontWeight: 600,
- lineHeight: 1.2,
- color: theme.gray300,
- },
- // input is in bytes, minInterval is a megabyte
- minInterval: 1024 * 1024,
- // maxInterval is a terabyte
- maxInterval: Math.pow(1024, 4),
- // format the axis labels to be whole number values
- axisLabel: {
- formatter: value => formatBytesBase2(value, 0),
- },
- }),
- // XXX: For area charts, mouse events *only* occurs when interacting with
- // the "line" of the area chart. Mouse events do not fire when interacting
- // with the "area" under the line.
- onMouseOver: ({data}) => {
- if (data[0]) {
- setCurrentHoverTime(data[0] * 1000 - startTimestampMs);
- }
- },
- onMouseOut: () => {
- setCurrentHoverTime(undefined);
- },
- onClick: ({data}) => {
- if (data.value) {
- setCurrentTime(data.value * 1000 - startTimestampMs);
- }
- },
- };
- const series: Series[] = [
- {
- seriesName: t('Used Heap Memory'),
- data: memorySpans.map(span => ({
- value: span.data.memory.usedJSHeapSize,
- name: span.endTimestamp,
- })),
- stack: 'heap-memory',
- lineStyle: {
- opacity: 0.75,
- width: 1,
- },
- },
- {
- seriesName: t('Free Heap Memory'),
- data: memorySpans.map(span => ({
- value: span.data.memory.totalJSHeapSize - span.data.memory.usedJSHeapSize,
- name: span.endTimestamp,
- })),
- stack: 'heap-memory',
- lineStyle: {
- opacity: 0.75,
- width: 1,
- },
- },
- // Inserting this here so we can update in Container
- {
- id: 'currentTime',
- seriesName: t('Current player time'),
- data: [],
- markLine: {
- symbol: ['', ''],
- data: [],
- label: {
- show: false,
- },
- lineStyle: {
- type: 'solid' as const,
- color: theme.purple300,
- width: 2,
- },
- },
- },
- {
- id: 'hoverTime',
- seriesName: t('Hover player time'),
- data: [],
- markLine: {
- symbol: ['', ''],
- data: [],
- label: {
- show: false,
- },
- lineStyle: {
- type: 'solid' as const,
- color: theme.purple200,
- width: 2,
- },
- },
- },
- ];
- return (
- <MemoryChartWrapper id="replay-memory-chart">
- <AreaChart forwardedRef={forwardedRef} series={series} {...chartOptions} />
- </MemoryChartWrapper>
- );
- }
- const MemoryChartWrapper = styled('div')`
- margin-top: ${space(2)};
- margin-bottom: ${space(3)};
- border-radius: ${space(0.5)};
- border: 1px solid ${p => p.theme.border};
- `;
- const MemoizedMemoryChart = memo(
- forwardRef<ReactEchartsRef, Props>((props, ref) => (
- <MemoryChart forwardedRef={ref} {...props} />
- ))
- );
- interface MemoryChartContainerProps extends Props {
- currentHoverTime: number | undefined;
- currentTime: number;
- }
- /**
- * This container is used to update echarts outside of React. `currentTime` is
- * the current time of the player -- if replay is currently playing, this will be
- * updated quite frequently causing the chart to constantly re-render. The
- * re-renders will conflict with mouse interactions (e.g. hovers and
- * tooltips).
- *
- * We need `MemoryChart` (which wraps an `<AreaChart>`) to re-render as
- * infrequently as possible, so we use React.memo and only pass in props that
- * are not frequently updated.
- * */
- function MemoryChartContainer({
- currentTime,
- currentHoverTime,
- startTimestampMs = 0,
- ...props
- }: MemoryChartContainerProps) {
- const chart = useRef<ReactEchartsRef>(null);
- const theme = useTheme();
- useEffect(() => {
- if (!chart.current) {
- return;
- }
- const echarts = chart.current.getEchartsInstance();
- echarts.setOption({
- series: [
- {
- id: 'currentTime',
- markLine: {
- data: [
- {
- xAxis: currentTime + startTimestampMs,
- },
- ],
- },
- },
- ],
- });
- }, [currentTime, startTimestampMs, theme]);
- useEffect(() => {
- if (!chart.current) {
- return;
- }
- const echarts = chart.current.getEchartsInstance();
- echarts.setOption({
- series: [
- {
- id: 'hoverTime',
- markLine: {
- data: [
- ...(currentHoverTime
- ? [
- {
- xAxis: currentHoverTime + startTimestampMs,
- },
- ]
- : []),
- ],
- },
- },
- ],
- });
- }, [currentHoverTime, startTimestampMs, theme]);
- return (
- <MemoizedMemoryChart ref={chart} startTimestampMs={startTimestampMs} {...props} />
- );
- }
- export default MemoryChartContainer;
|