pageOverviewSidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 {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. return (
  107. <Fragment>
  108. <SectionHeading>
  109. {t('Performance Score')}
  110. <QuestionTooltip
  111. isHoverable
  112. size="sm"
  113. title={
  114. <span>
  115. {t('The overall performance rating of this page.')}
  116. <br />
  117. <ExternalLink href={`${MODULE_DOC_LINK}#performance-score`}>
  118. {t('How is this calculated?')}
  119. </ExternalLink>
  120. </span>
  121. }
  122. />
  123. </SectionHeading>
  124. <SidebarPerformanceScoreRingContainer>
  125. {!projectScoreIsLoading && projectScore && (
  126. <PerformanceScoreRingWithTooltips
  127. projectScore={projectScore}
  128. text={projectScore.totalScore}
  129. width={220}
  130. height={200}
  131. ringBackgroundColors={ringBackgroundColors}
  132. ringSegmentColors={ringSegmentColors}
  133. />
  134. )}
  135. {projectScoreIsLoading && <ProjectScoreEmptyLoadingElement />}
  136. </SidebarPerformanceScoreRingContainer>
  137. <SidebarSpacer />
  138. <SectionHeading>
  139. {t('Page Loads')}
  140. <QuestionTooltip
  141. size="sm"
  142. title={t(
  143. 'The total number of times that users have loaded this page. This number does not include any page navigations beyond initial page loads.'
  144. )}
  145. />
  146. </SectionHeading>
  147. <ChartValue>
  148. {currentCount ? formatAbbreviatedNumber(currentCount) : null}
  149. </ChartValue>
  150. {initialCount && currentCount && countDiff && shouldDoublePeriod ? (
  151. <ChartSubText color={diffToColor(countDiff)}>
  152. {getChartSubText(
  153. countDiff,
  154. formatAbbreviatedNumber(initialCount),
  155. formatAbbreviatedNumber(currentCount)
  156. )}
  157. </ChartSubText>
  158. ) : null}
  159. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  160. {zoomRenderProps => (
  161. <LineChart
  162. {...zoomRenderProps}
  163. height={CHART_HEIGHTS}
  164. series={throughtputData}
  165. xAxis={{show: false}}
  166. grid={{
  167. left: 0,
  168. right: 15,
  169. top: 10,
  170. bottom: -10,
  171. }}
  172. yAxis={{axisLabel: {formatter: number => formatAbbreviatedNumber(number)}}}
  173. tooltip={{valueFormatter: number => formatAbbreviatedNumber(number)}}
  174. />
  175. )}
  176. </ChartZoom>
  177. <MiniAggregateWaterfallContainer>
  178. <MiniAggregateWaterfall transaction={transaction} />
  179. </MiniAggregateWaterfallContainer>
  180. <SidebarSpacer />
  181. <SidebarSpacer />
  182. <SectionHeading>
  183. {t('Interactions')}
  184. <QuestionTooltip
  185. size="sm"
  186. title={t('The total number of times that users performed an INP on this page.')}
  187. />
  188. </SectionHeading>
  189. <ChartValue>
  190. {currentInpCount ? formatAbbreviatedNumber(currentInpCount) : null}
  191. </ChartValue>
  192. {initialInpCount && currentInpCount && inpCountDiff && shouldDoublePeriod ? (
  193. <ChartSubText color={diffToColor(inpCountDiff)}>
  194. {getChartSubText(
  195. inpCountDiff,
  196. formatAbbreviatedNumber(initialInpCount),
  197. formatAbbreviatedNumber(currentInpCount)
  198. )}
  199. </ChartSubText>
  200. ) : null}
  201. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  202. {zoomRenderProps => (
  203. <LineChart
  204. {...zoomRenderProps}
  205. height={CHART_HEIGHTS}
  206. series={inpThroughtputData}
  207. xAxis={{show: false}}
  208. grid={{
  209. left: 0,
  210. right: 15,
  211. top: 10,
  212. bottom: -10,
  213. }}
  214. yAxis={{
  215. axisLabel: {formatter: number => formatAbbreviatedNumber(number)},
  216. }}
  217. tooltip={{valueFormatter: number => formatAbbreviatedNumber(number)}}
  218. />
  219. )}
  220. </ChartZoom>
  221. <SidebarSpacer />
  222. </Fragment>
  223. );
  224. }
  225. const getChartSubText = (
  226. diff?: number,
  227. value?: string | number,
  228. newValue?: string | number
  229. ) => {
  230. if (diff === undefined || value === undefined) {
  231. return null;
  232. }
  233. if (diff > 1) {
  234. const relativeDiff = Math.round((diff - 1) * 1000) / 10;
  235. if (relativeDiff === Infinity) {
  236. return `Up from ${value} to ${newValue}`;
  237. }
  238. return `Up ${relativeDiff}% from ${value}`;
  239. }
  240. if (diff < 1) {
  241. const relativeDiff = Math.round((1 - diff) * 1000) / 10;
  242. return `Down ${relativeDiff}% from ${value}`;
  243. }
  244. return t('No Change');
  245. };
  246. const processSeriesData = (
  247. count: SeriesDataUnit[],
  248. isLoading: boolean,
  249. {period, start, end}: PageFilters['datetime'],
  250. shouldDoublePeriod: boolean
  251. ) => {
  252. let seriesData = !isLoading
  253. ? count.map(({name, value}) => ({
  254. name,
  255. value,
  256. }))
  257. : [];
  258. // Trim off last data point since it's incomplete
  259. if (seriesData.length > 0 && period && !start && !end) {
  260. seriesData = seriesData.slice(0, -1);
  261. }
  262. const dataMiddleIndex = Math.floor(seriesData.length / 2);
  263. const currentSeries = shouldDoublePeriod
  264. ? seriesData.slice(dataMiddleIndex)
  265. : seriesData;
  266. const previousSeries = seriesData.slice(0, dataMiddleIndex);
  267. const initialCount = !isLoading
  268. ? previousSeries.reduce((acc, {value}) => acc + value, 0)
  269. : undefined;
  270. const currentCount = !isLoading
  271. ? currentSeries.reduce((acc, {value}) => acc + value, 0)
  272. : undefined;
  273. const countDiff =
  274. !isLoading && currentCount !== undefined && initialCount !== undefined
  275. ? currentCount / initialCount
  276. : undefined;
  277. return {countDiff, currentSeries, currentCount, initialCount};
  278. };
  279. const SidebarPerformanceScoreRingContainer = styled('div')`
  280. display: flex;
  281. justify-content: center;
  282. align-items: center;
  283. margin-bottom: ${space(1)};
  284. `;
  285. const ChartValue = styled('div')`
  286. font-size: ${p => p.theme.fontSizeExtraLarge};
  287. `;
  288. const ChartSubText = styled('div')<{color?: string}>`
  289. font-size: ${p => p.theme.fontSizeMedium};
  290. color: ${p => p.color ?? p.theme.subText};
  291. `;
  292. const SectionHeading = styled('h4')`
  293. display: inline-grid;
  294. grid-auto-flow: column;
  295. gap: ${space(1)};
  296. align-items: center;
  297. color: ${p => p.theme.subText};
  298. font-size: ${p => p.theme.fontSizeMedium};
  299. margin: 0;
  300. `;
  301. const MiniAggregateWaterfallContainer = styled('div')`
  302. margin-top: ${space(1)};
  303. margin-bottom: ${space(1)};
  304. `;
  305. const ProjectScoreEmptyLoadingElement = styled('div')`
  306. width: 220px;
  307. height: 160px;
  308. `;