pageOverviewSidebar.tsx 10 KB

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