pageOverviewSidebar.tsx 11 KB

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