pageOverviewSidebar.tsx 10 KB

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