index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {Fragment, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import EmptyMessage from 'sentry/components/emptyMessage';
  4. import Placeholder from 'sentry/components/placeholder';
  5. import {useReplayContext} from 'sentry/components/replays/replayContext';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {useQuery} from 'sentry/utils/queryClient';
  9. import countDomNodes from 'sentry/utils/replays/countDomNodes';
  10. import useCurrentHoverTime from 'sentry/utils/replays/playback/providers/useCurrentHoverTime';
  11. import type ReplayReader from 'sentry/utils/replays/replayReader';
  12. import DomNodesChart from 'sentry/views/replays/detail/memoryPanel/domNodesChart';
  13. import MemoryChart from 'sentry/views/replays/detail/memoryPanel/memoryChart';
  14. function useCountDomNodes({replay}: {replay: null | ReplayReader}) {
  15. return useQuery(
  16. ['countDomNodes', replay],
  17. () =>
  18. countDomNodes({
  19. frames: replay?.getRRWebMutations(),
  20. rrwebEvents: replay?.getRRWebFrames(),
  21. startTimestampMs: replay?.getStartTimestampMs() ?? 0,
  22. }),
  23. {enabled: Boolean(replay), cacheTime: Infinity}
  24. );
  25. }
  26. export default function MemoryPanel() {
  27. const {currentTime, isFetching, replay, setCurrentTime} = useReplayContext();
  28. const [currentHoverTime, setCurrentHoverTime] = useCurrentHoverTime();
  29. const memoryFrames = replay?.getMemoryFrames();
  30. const {data: frameToCount} = useCountDomNodes({replay});
  31. const domNodeData = useMemo(
  32. () => Array.from(frameToCount?.values() || []),
  33. [frameToCount]
  34. );
  35. const memoryChart =
  36. !replay || isFetching ? (
  37. <Placeholder height="100%" />
  38. ) : !replay || !memoryFrames?.length ? (
  39. <EmptyMessage
  40. data-test-id="replay-details-memory-tab"
  41. title={t('No memory metrics found')}
  42. description={t(
  43. 'Memory metrics are only captured within Chromium based browser sessions.'
  44. )}
  45. />
  46. ) : (
  47. <Fragment>
  48. <ChartTitle>{t('Heap Size')}</ChartTitle>
  49. <MemoryChart
  50. currentHoverTime={currentHoverTime}
  51. currentTime={currentTime}
  52. durationMs={replay.getDurationMs()}
  53. memoryFrames={memoryFrames}
  54. setCurrentHoverTime={setCurrentHoverTime}
  55. setCurrentTime={setCurrentTime}
  56. startTimestampMs={replay.getStartTimestampMs()}
  57. />
  58. </Fragment>
  59. );
  60. const domNodesChart =
  61. !replay || isFetching ? (
  62. <Placeholder height="100%" />
  63. ) : (
  64. <Fragment>
  65. <ChartTitle>{t('DOM Nodes')}</ChartTitle>
  66. <DomNodesChart
  67. currentHoverTime={currentHoverTime}
  68. currentTime={currentTime}
  69. durationMs={replay.getDurationMs()}
  70. datapoints={domNodeData}
  71. setCurrentHoverTime={setCurrentHoverTime}
  72. setCurrentTime={setCurrentTime}
  73. startTimestampMs={replay.getStartTimestampMs()}
  74. />
  75. </Fragment>
  76. );
  77. return (
  78. <Grid>
  79. <ChartWrapper>{memoryChart}</ChartWrapper>
  80. <ChartWrapper>{domNodesChart}</ChartWrapper>
  81. </Grid>
  82. );
  83. }
  84. const Grid = styled('div')`
  85. display: grid;
  86. grid-template-rows: 1fr 1fr;
  87. grid-template-columns: 1fr;
  88. gap: ${space(1)};
  89. justify-content: center;
  90. height: 100%;
  91. `;
  92. const ChartWrapper = styled('div')`
  93. border: 1px solid ${p => p.theme.border};
  94. border-radius: ${space(0.5)};
  95. padding: ${space(1)};
  96. overflow: hidden;
  97. display: flex;
  98. flex-direction: column;
  99. & > * {
  100. flex-grow: 1;
  101. }
  102. `;
  103. const ChartTitle = styled('h5')`
  104. font-size: ${p => p.theme.fontSizeLarge};
  105. font-weight: ${p => p.theme.text.cardTitle.fontWeight};
  106. line-height: ${p => p.theme.text.cardTitle.lineHeight};
  107. color: ${p => p.theme.subText};
  108. flex: 0 1 auto;
  109. margin: 0;
  110. `;