index.tsx 3.6 KB

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