pageOverviewSidebar.tsx 10 KB

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