pageOverviewSidebar.tsx 10 KB

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