pageOverviewSidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import ChartZoom from 'sentry/components/charts/chartZoom';
  5. import type {LineChartSeries} from 'sentry/components/charts/lineChart';
  6. import {LineChart} from 'sentry/components/charts/lineChart';
  7. import {shouldFetchPreviousPeriod} from 'sentry/components/charts/utils';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import QuestionTooltip from 'sentry/components/questionTooltip';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import type {PageFilters} from 'sentry/types/core';
  13. import type {SeriesDataUnit} from 'sentry/types/echarts';
  14. import {formatAbbreviatedNumber} from 'sentry/utils/formatters';
  15. import {getPeriod} from 'sentry/utils/getPeriod';
  16. import usePageFilters from 'sentry/utils/usePageFilters';
  17. import useRouter from 'sentry/utils/useRouter';
  18. import {MiniAggregateWaterfall} from 'sentry/views/performance/browser/webVitals/components/miniAggregateWaterfall';
  19. import PerformanceScoreRingWithTooltips from 'sentry/views/performance/browser/webVitals/components/performanceScoreRingWithTooltips';
  20. import {useProjectRawWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsValuesTimeseriesQuery';
  21. import type {ProjectScore} from 'sentry/views/performance/browser/webVitals/utils/types';
  22. import {SidebarSpacer} from 'sentry/views/performance/transactionSummary/utils';
  23. const CHART_HEIGHTS = 100;
  24. type Props = {
  25. transaction: string;
  26. projectScore?: ProjectScore;
  27. projectScoreIsLoading?: boolean;
  28. search?: string;
  29. };
  30. export function PageOverviewSidebar({
  31. projectScore,
  32. transaction,
  33. projectScoreIsLoading,
  34. }: Props) {
  35. const theme = useTheme();
  36. const router = useRouter();
  37. const pageFilters = usePageFilters();
  38. const {period, start, end, utc} = pageFilters.selection.datetime;
  39. const shouldDoublePeriod = shouldFetchPreviousPeriod({
  40. includePrevious: true,
  41. period,
  42. start,
  43. end,
  44. });
  45. const doubledPeriod = getPeriod({period, start, end}, {shouldDoublePeriod});
  46. const doubledDatetime: PageFilters['datetime'] = {
  47. period: doubledPeriod.statsPeriod ?? null,
  48. start: doubledPeriod.start ?? null,
  49. end: doubledPeriod.end ?? null,
  50. utc,
  51. };
  52. const {data, isLoading: isLoading} = useProjectRawWebVitalsValuesTimeseriesQuery({
  53. transaction,
  54. datetime: doubledDatetime,
  55. });
  56. const {countDiff, currentSeries, currentCount, initialCount} = processSeriesData(
  57. data?.count,
  58. isLoading,
  59. pageFilters.selection.datetime,
  60. shouldDoublePeriod
  61. );
  62. const throughtputData: LineChartSeries[] = [
  63. {
  64. data: currentSeries,
  65. seriesName: t('Page Loads'),
  66. },
  67. ];
  68. const {
  69. countDiff: inpCountDiff,
  70. currentSeries: currentInpSeries,
  71. currentCount: currentInpCount,
  72. initialCount: initialInpCount,
  73. } = processSeriesData(
  74. data.countInp,
  75. isLoading,
  76. pageFilters.selection.datetime,
  77. shouldDoublePeriod
  78. );
  79. const inpThroughtputData: LineChartSeries[] = [
  80. {
  81. data: currentInpSeries,
  82. seriesName: t('Interactions'),
  83. },
  84. ];
  85. const diffToColor = (diff?: number, reverse?: boolean) => {
  86. if (diff === undefined) {
  87. return undefined;
  88. }
  89. if (diff > 1) {
  90. if (reverse) {
  91. return theme.red300;
  92. }
  93. return theme.green300;
  94. }
  95. if (diff < 1) {
  96. if (reverse) {
  97. return theme.green300;
  98. }
  99. return theme.red300;
  100. }
  101. return undefined;
  102. };
  103. const ringSegmentColors = theme.charts.getColorPalette(3);
  104. const ringBackgroundColors = ringSegmentColors.map(color => `${color}50`);
  105. // Gets weights to dynamically size the performance score ring segments
  106. const weights = projectScore
  107. ? {
  108. lcp: projectScore.lcpWeight,
  109. fcp: projectScore.fcpWeight,
  110. fid: 0,
  111. inp: projectScore.inpWeight,
  112. cls: projectScore.clsWeight,
  113. ttfb: projectScore.ttfbWeight,
  114. }
  115. : undefined;
  116. return (
  117. <Fragment>
  118. <SectionHeading>
  119. {t('Performance Score')}
  120. <QuestionTooltip
  121. isHoverable
  122. size="sm"
  123. title={
  124. <span>
  125. {t('The overall performance rating of this page.')}
  126. <br />
  127. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/#performance-score">
  128. {t('How is this calculated?')}
  129. </ExternalLink>
  130. </span>
  131. }
  132. />
  133. </SectionHeading>
  134. <SidebarPerformanceScoreRingContainer>
  135. {!projectScoreIsLoading && projectScore && (
  136. <PerformanceScoreRingWithTooltips
  137. projectScore={projectScore}
  138. text={projectScore.totalScore}
  139. width={220}
  140. height={200}
  141. ringBackgroundColors={ringBackgroundColors}
  142. ringSegmentColors={ringSegmentColors}
  143. weights={weights}
  144. />
  145. )}
  146. {projectScoreIsLoading && <ProjectScoreEmptyLoadingElement />}
  147. </SidebarPerformanceScoreRingContainer>
  148. <SidebarSpacer />
  149. <SectionHeading>
  150. {t('Page Loads')}
  151. <QuestionTooltip
  152. size="sm"
  153. title={t(
  154. 'The total number of times that users have loaded this page. This number does not include any page navigations beyond initial page loads.'
  155. )}
  156. />
  157. </SectionHeading>
  158. <ChartValue>
  159. {currentCount ? formatAbbreviatedNumber(currentCount) : null}
  160. </ChartValue>
  161. {initialCount && currentCount && countDiff && shouldDoublePeriod ? (
  162. <ChartSubText color={diffToColor(countDiff)}>
  163. {getChartSubText(
  164. countDiff,
  165. formatAbbreviatedNumber(initialCount),
  166. formatAbbreviatedNumber(currentCount)
  167. )}
  168. </ChartSubText>
  169. ) : null}
  170. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  171. {zoomRenderProps => (
  172. <LineChart
  173. {...zoomRenderProps}
  174. height={CHART_HEIGHTS}
  175. series={throughtputData}
  176. xAxis={{show: false}}
  177. grid={{
  178. left: 0,
  179. right: 15,
  180. top: 10,
  181. bottom: -10,
  182. }}
  183. yAxis={{axisLabel: {formatter: number => formatAbbreviatedNumber(number)}}}
  184. tooltip={{valueFormatter: number => formatAbbreviatedNumber(number)}}
  185. />
  186. )}
  187. </ChartZoom>
  188. <MiniAggregateWaterfallContainer>
  189. <MiniAggregateWaterfall transaction={transaction} />
  190. </MiniAggregateWaterfallContainer>
  191. <SidebarSpacer />
  192. <SidebarSpacer />
  193. <SectionHeading>
  194. {t('Interactions')}
  195. <QuestionTooltip
  196. size="sm"
  197. title={t('The total number of times that users performed an INP on this page.')}
  198. />
  199. </SectionHeading>
  200. <ChartValue>
  201. {currentInpCount ? formatAbbreviatedNumber(currentInpCount) : null}
  202. </ChartValue>
  203. {initialInpCount && currentInpCount && inpCountDiff && shouldDoublePeriod ? (
  204. <ChartSubText color={diffToColor(inpCountDiff)}>
  205. {getChartSubText(
  206. inpCountDiff,
  207. formatAbbreviatedNumber(initialInpCount),
  208. formatAbbreviatedNumber(currentInpCount)
  209. )}
  210. </ChartSubText>
  211. ) : null}
  212. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  213. {zoomRenderProps => (
  214. <LineChart
  215. {...zoomRenderProps}
  216. height={CHART_HEIGHTS}
  217. series={inpThroughtputData}
  218. xAxis={{show: false}}
  219. grid={{
  220. left: 0,
  221. right: 15,
  222. top: 10,
  223. bottom: -10,
  224. }}
  225. yAxis={{
  226. axisLabel: {formatter: number => formatAbbreviatedNumber(number)},
  227. }}
  228. tooltip={{valueFormatter: number => formatAbbreviatedNumber(number)}}
  229. />
  230. )}
  231. </ChartZoom>
  232. <SidebarSpacer />
  233. </Fragment>
  234. );
  235. }
  236. const getChartSubText = (
  237. diff?: number,
  238. value?: string | number,
  239. newValue?: string | number
  240. ) => {
  241. if (diff === undefined || value === undefined) {
  242. return null;
  243. }
  244. if (diff > 1) {
  245. const relativeDiff = Math.round((diff - 1) * 1000) / 10;
  246. if (relativeDiff === Infinity) {
  247. return `Up from ${value} to ${newValue}`;
  248. }
  249. return `Up ${relativeDiff}% from ${value}`;
  250. }
  251. if (diff < 1) {
  252. const relativeDiff = Math.round((1 - diff) * 1000) / 10;
  253. return `Down ${relativeDiff}% from ${value}`;
  254. }
  255. return t('No Change');
  256. };
  257. const processSeriesData = (
  258. count: SeriesDataUnit[],
  259. isLoading: boolean,
  260. {period, start, end}: PageFilters['datetime'],
  261. shouldDoublePeriod: boolean
  262. ) => {
  263. let seriesData = !isLoading
  264. ? count.map(({name, value}) => ({
  265. name,
  266. value,
  267. }))
  268. : [];
  269. // Trim off last data point since it's incomplete
  270. if (seriesData.length > 0 && period && !start && !end) {
  271. seriesData = seriesData.slice(0, -1);
  272. }
  273. const dataMiddleIndex = Math.floor(seriesData.length / 2);
  274. const currentSeries = shouldDoublePeriod
  275. ? seriesData.slice(dataMiddleIndex)
  276. : seriesData;
  277. const previousSeries = seriesData.slice(0, dataMiddleIndex);
  278. const initialCount = !isLoading
  279. ? previousSeries.reduce((acc, {value}) => acc + value, 0)
  280. : undefined;
  281. const currentCount = !isLoading
  282. ? currentSeries.reduce((acc, {value}) => acc + value, 0)
  283. : undefined;
  284. const countDiff =
  285. !isLoading && currentCount !== undefined && initialCount !== undefined
  286. ? currentCount / initialCount
  287. : undefined;
  288. return {countDiff, currentSeries, currentCount, initialCount};
  289. };
  290. const SidebarPerformanceScoreRingContainer = styled('div')`
  291. display: flex;
  292. justify-content: center;
  293. align-items: center;
  294. margin-bottom: ${space(1)};
  295. `;
  296. const ChartValue = styled('div')`
  297. font-size: ${p => p.theme.fontSizeExtraLarge};
  298. `;
  299. const ChartSubText = styled('div')<{color?: string}>`
  300. font-size: ${p => p.theme.fontSizeMedium};
  301. color: ${p => p.color ?? p.theme.subText};
  302. `;
  303. const SectionHeading = styled('h4')`
  304. display: inline-grid;
  305. grid-auto-flow: column;
  306. gap: ${space(1)};
  307. align-items: center;
  308. color: ${p => p.theme.subText};
  309. font-size: ${p => p.theme.fontSizeMedium};
  310. margin: 0;
  311. `;
  312. const MiniAggregateWaterfallContainer = styled('div')`
  313. margin-top: ${space(1)};
  314. margin-bottom: ${space(1)};
  315. `;
  316. const ProjectScoreEmptyLoadingElement = styled('div')`
  317. width: 220px;
  318. height: 160px;
  319. `;