pageOverviewSidebar.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. }
  115. : undefined;
  116. return (
  117. <Fragment>
  118. <SectionHeading>
  119. {t('Performance Score')}
  120. <QuestionTooltip
  121. isHoverable
  122. size="sm"
  123. title={
  124. <span>
  125. {t('The overall performance rating of this page.')}
  126. <br />
  127. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/#performance-score">
  128. {t('How is this calculated?')}
  129. </ExternalLink>
  130. </span>
  131. }
  132. />
  133. </SectionHeading>
  134. <SidebarPerformanceScoreRingContainer>
  135. {!projectScoreIsLoading && projectScore && (
  136. <PerformanceScoreRingWithTooltips
  137. projectScore={projectScore}
  138. text={projectScore.totalScore}
  139. width={220}
  140. height={180}
  141. ringBackgroundColors={ringBackgroundColors}
  142. ringSegmentColors={ringSegmentColors}
  143. weights={weights}
  144. />
  145. )}
  146. {projectScoreIsLoading && <ProjectScoreEmptyLoadingElement />}
  147. </SidebarPerformanceScoreRingContainer>
  148. <SidebarSpacer />
  149. <SectionHeading>
  150. {t('Page Loads')}
  151. <QuestionTooltip
  152. size="sm"
  153. title={t(
  154. 'The total number of times that users have loaded this page. This number does not include any page navigations beyond initial page loads.'
  155. )}
  156. />
  157. </SectionHeading>
  158. <ChartValue>
  159. {currentCount ? formatAbbreviatedNumber(currentCount) : null}
  160. </ChartValue>
  161. {initialCount && currentCount && countDiff && shouldDoublePeriod ? (
  162. <ChartSubText color={diffToColor(countDiff)}>
  163. {getChartSubText(
  164. countDiff,
  165. formatAbbreviatedNumber(initialCount),
  166. formatAbbreviatedNumber(currentCount)
  167. )}
  168. </ChartSubText>
  169. ) : null}
  170. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  171. {zoomRenderProps => (
  172. <LineChart
  173. {...zoomRenderProps}
  174. height={CHART_HEIGHTS}
  175. series={throughtputData}
  176. xAxis={{show: false}}
  177. grid={{
  178. left: 0,
  179. right: 15,
  180. top: 10,
  181. bottom: -10,
  182. }}
  183. yAxis={{axisLabel: {formatter: number => formatAbbreviatedNumber(number)}}}
  184. tooltip={{valueFormatter: number => formatAbbreviatedNumber(number)}}
  185. />
  186. )}
  187. </ChartZoom>
  188. <SidebarSpacer />
  189. <SectionHeading>
  190. {t('Aggregate Spans')}
  191. <QuestionTooltip
  192. size="sm"
  193. title={t('A synthesized span waterfall for this page.')}
  194. />
  195. </SectionHeading>
  196. <MiniAggregateWaterfallContainer>
  197. <MiniAggregateWaterfall transaction={transaction} />
  198. </MiniAggregateWaterfallContainer>
  199. <SidebarSpacer />
  200. </Fragment>
  201. );
  202. }
  203. const getChartSubText = (
  204. diff?: number,
  205. value?: string | number,
  206. newValue?: string | number
  207. ) => {
  208. if (diff === undefined || value === undefined) {
  209. return null;
  210. }
  211. if (diff > 1) {
  212. const relativeDiff = Math.round((diff - 1) * 1000) / 10;
  213. if (relativeDiff === Infinity) {
  214. return `Up from ${value} to ${newValue}`;
  215. }
  216. return `Up ${relativeDiff}% from ${value}`;
  217. }
  218. if (diff < 1) {
  219. const relativeDiff = Math.round((1 - diff) * 1000) / 10;
  220. return `Down ${relativeDiff}% from ${value}`;
  221. }
  222. return t('No Change');
  223. };
  224. const SidebarPerformanceScoreRingContainer = styled('div')`
  225. display: flex;
  226. justify-content: center;
  227. align-items: center;
  228. margin-bottom: ${space(1)};
  229. `;
  230. const ChartValue = styled('div')`
  231. font-size: ${p => p.theme.fontSizeExtraLarge};
  232. `;
  233. const ChartSubText = styled('div')<{color?: string}>`
  234. font-size: ${p => p.theme.fontSizeMedium};
  235. color: ${p => p.color ?? p.theme.subText};
  236. `;
  237. const SectionHeading = styled('h4')`
  238. display: inline-grid;
  239. grid-auto-flow: column;
  240. gap: ${space(1)};
  241. align-items: center;
  242. color: ${p => p.theme.subText};
  243. font-size: ${p => p.theme.fontSizeMedium};
  244. margin: 0;
  245. `;
  246. const MiniAggregateWaterfallContainer = styled('div')`
  247. margin-top: ${space(1)};
  248. margin-bottom: ${space(1)};
  249. `;
  250. const ProjectScoreEmptyLoadingElement = styled('div')`
  251. width: 220px;
  252. height: 160px;
  253. `;