pageOverviewSidebar.tsx 10 KB

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