pageOverviewSidebar.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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';
  13. import {formatAbbreviatedNumber} from 'sentry/utils/formatters';
  14. import {getPeriod} from 'sentry/utils/getPeriod';
  15. import usePageFilters from 'sentry/utils/usePageFilters';
  16. import useRouter from 'sentry/utils/useRouter';
  17. import {MiniAggregateWaterfall} from 'sentry/views/performance/browser/webVitals/components/miniAggregateWaterfall';
  18. import PerformanceScoreRingWithTooltips from 'sentry/views/performance/browser/webVitals/components/performanceScoreRingWithTooltips';
  19. import {useProjectRawWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsValuesTimeseriesQuery';
  20. import type {ProjectScore} from 'sentry/views/performance/browser/webVitals/utils/types';
  21. import {SidebarSpacer} from 'sentry/views/performance/transactionSummary/utils';
  22. const CHART_HEIGHTS = 100;
  23. type Props = {
  24. transaction: string;
  25. projectScore?: ProjectScore;
  26. projectScoreIsLoading?: boolean;
  27. search?: string;
  28. };
  29. export function PageOverviewSidebar({
  30. projectScore,
  31. transaction,
  32. projectScoreIsLoading,
  33. }: Props) {
  34. const theme = useTheme();
  35. const router = useRouter();
  36. const pageFilters = usePageFilters();
  37. const {period, start, end, utc} = pageFilters.selection.datetime;
  38. const shouldDoublePeriod = shouldFetchPreviousPeriod({
  39. includePrevious: true,
  40. period,
  41. start,
  42. end,
  43. });
  44. const doubledPeriod = getPeriod({period, start, end}, {shouldDoublePeriod});
  45. const doubledDatetime: PageFilters['datetime'] = {
  46. period: doubledPeriod.statsPeriod ?? null,
  47. start: doubledPeriod.start ?? null,
  48. end: doubledPeriod.end ?? null,
  49. utc,
  50. };
  51. const {data, isLoading: isLoading} = useProjectRawWebVitalsValuesTimeseriesQuery({
  52. transaction,
  53. datetime: doubledDatetime,
  54. });
  55. let seriesData = !isLoading
  56. ? data?.count.map(({name, value}) => ({
  57. name,
  58. value,
  59. }))
  60. : [];
  61. // Trim off last data point since it's incomplete
  62. if (seriesData.length > 0 && period && !start && !end) {
  63. seriesData = seriesData.slice(0, -1);
  64. }
  65. const dataMiddleIndex = Math.floor(seriesData.length / 2);
  66. const currentSeries = shouldDoublePeriod
  67. ? seriesData.slice(dataMiddleIndex)
  68. : seriesData;
  69. const previousSeries = seriesData.slice(0, dataMiddleIndex);
  70. const initialCount = !isLoading
  71. ? previousSeries.reduce((acc, {value}) => acc + value, 0)
  72. : undefined;
  73. const currentCount = !isLoading
  74. ? currentSeries.reduce((acc, {value}) => acc + value, 0)
  75. : undefined;
  76. const countDiff =
  77. !isLoading && currentCount !== undefined && initialCount !== undefined
  78. ? currentCount / initialCount
  79. : undefined;
  80. const throughtputData: LineChartSeries[] = [
  81. {
  82. data: currentSeries,
  83. seriesName: t('Page Loads'),
  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. // Gets weights to dynamically size the performance score ring segments
  107. const weights = projectScore
  108. ? {
  109. cls: projectScore.clsWeight,
  110. fcp: projectScore.fcpWeight,
  111. fid: projectScore.fidWeight,
  112. lcp: projectScore.lcpWeight,
  113. ttfb: projectScore.ttfbWeight,
  114. inp: projectScore.inpWeight,
  115. }
  116. : undefined;
  117. return (
  118. <Fragment>
  119. <SectionHeading>
  120. {t('Performance Score')}
  121. <QuestionTooltip
  122. isHoverable
  123. size="sm"
  124. title={
  125. <span>
  126. {t('The overall performance rating of this page.')}
  127. <br />
  128. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/#performance-score">
  129. {t('How is this calculated?')}
  130. </ExternalLink>
  131. </span>
  132. }
  133. />
  134. </SectionHeading>
  135. <SidebarPerformanceScoreRingContainer>
  136. {!projectScoreIsLoading && projectScore && (
  137. <PerformanceScoreRingWithTooltips
  138. projectScore={projectScore}
  139. text={projectScore.totalScore}
  140. width={220}
  141. height={180}
  142. ringBackgroundColors={ringBackgroundColors}
  143. ringSegmentColors={ringSegmentColors}
  144. weights={weights}
  145. />
  146. )}
  147. {projectScoreIsLoading && <ProjectScoreEmptyLoadingElement />}
  148. </SidebarPerformanceScoreRingContainer>
  149. <SidebarSpacer />
  150. <SectionHeading>
  151. {t('Page Loads')}
  152. <QuestionTooltip
  153. size="sm"
  154. title={t(
  155. 'The total number of times that users have loaded this page. This number does not include any page navigations beyond initial page loads.'
  156. )}
  157. />
  158. </SectionHeading>
  159. <ChartValue>
  160. {currentCount ? formatAbbreviatedNumber(currentCount) : null}
  161. </ChartValue>
  162. {initialCount && currentCount && countDiff && shouldDoublePeriod ? (
  163. <ChartSubText color={diffToColor(countDiff)}>
  164. {getChartSubText(
  165. countDiff,
  166. formatAbbreviatedNumber(initialCount),
  167. formatAbbreviatedNumber(currentCount)
  168. )}
  169. </ChartSubText>
  170. ) : null}
  171. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  172. {zoomRenderProps => (
  173. <LineChart
  174. {...zoomRenderProps}
  175. height={CHART_HEIGHTS}
  176. series={throughtputData}
  177. xAxis={{show: false}}
  178. grid={{
  179. left: 0,
  180. right: 15,
  181. top: 10,
  182. bottom: -10,
  183. }}
  184. yAxis={{axisLabel: {formatter: number => formatAbbreviatedNumber(number)}}}
  185. tooltip={{valueFormatter: number => formatAbbreviatedNumber(number)}}
  186. />
  187. )}
  188. </ChartZoom>
  189. <SidebarSpacer />
  190. <SectionHeading>
  191. {t('Aggregate Spans')}
  192. <QuestionTooltip
  193. size="sm"
  194. title={t('A synthesized span waterfall for this page.')}
  195. />
  196. </SectionHeading>
  197. <MiniAggregateWaterfallContainer>
  198. <MiniAggregateWaterfall transaction={transaction} />
  199. </MiniAggregateWaterfallContainer>
  200. <SidebarSpacer />
  201. </Fragment>
  202. );
  203. }
  204. const getChartSubText = (
  205. diff?: number,
  206. value?: string | number,
  207. newValue?: string | number
  208. ) => {
  209. if (diff === undefined || value === undefined) {
  210. return null;
  211. }
  212. if (diff > 1) {
  213. const relativeDiff = Math.round((diff - 1) * 1000) / 10;
  214. if (relativeDiff === Infinity) {
  215. return `Up from ${value} to ${newValue}`;
  216. }
  217. return `Up ${relativeDiff}% from ${value}`;
  218. }
  219. if (diff < 1) {
  220. const relativeDiff = Math.round((1 - diff) * 1000) / 10;
  221. return `Down ${relativeDiff}% from ${value}`;
  222. }
  223. return t('No Change');
  224. };
  225. const SidebarPerformanceScoreRingContainer = styled('div')`
  226. display: flex;
  227. justify-content: center;
  228. align-items: center;
  229. margin-bottom: ${space(1)};
  230. `;
  231. const ChartValue = styled('div')`
  232. font-size: ${p => p.theme.fontSizeExtraLarge};
  233. `;
  234. const ChartSubText = styled('div')<{color?: string}>`
  235. font-size: ${p => p.theme.fontSizeMedium};
  236. color: ${p => p.color ?? p.theme.subText};
  237. `;
  238. const SectionHeading = styled('h4')`
  239. display: inline-grid;
  240. grid-auto-flow: column;
  241. gap: ${space(1)};
  242. align-items: center;
  243. color: ${p => p.theme.subText};
  244. font-size: ${p => p.theme.fontSizeMedium};
  245. margin: 0;
  246. `;
  247. const MiniAggregateWaterfallContainer = styled('div')`
  248. margin-top: ${space(1)};
  249. margin-bottom: ${space(1)};
  250. `;
  251. const ProjectScoreEmptyLoadingElement = styled('div')`
  252. width: 220px;
  253. height: 160px;
  254. `;