webVitalMeters.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import InteractionStateLayer from 'sentry/components/interactionStateLayer';
  5. import ExternalLink from 'sentry/components/links/externalLink';
  6. import QuestionTooltip from 'sentry/components/questionTooltip';
  7. import {Tooltip} from 'sentry/components/tooltip';
  8. import {t, tct} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {TableData} from 'sentry/utils/discover/discoverQuery';
  11. import {getDuration} from 'sentry/utils/formatters';
  12. import {PERFORMANCE_SCORE_COLORS} from 'sentry/views/performance/browser/webVitals/utils/performanceScoreColors';
  13. import {
  14. scoreToStatus,
  15. STATUS_TEXT,
  16. } from 'sentry/views/performance/browser/webVitals/utils/scoreToStatus';
  17. import type {
  18. ProjectScore,
  19. WebVitals,
  20. } from 'sentry/views/performance/browser/webVitals/utils/types';
  21. import {useReplaceFidWithInpSetting} from 'sentry/views/performance/browser/webVitals/utils/useReplaceFidWithInpSetting';
  22. type Props = {
  23. onClick?: (webVital: WebVitals) => void;
  24. projectData?: TableData;
  25. projectScore?: ProjectScore;
  26. showTooltip?: boolean;
  27. transaction?: string;
  28. };
  29. const WEB_VITALS_METERS_CONFIG = {
  30. lcp: {
  31. name: t('Largest Contentful Paint'),
  32. formatter: (value: number) => getFormattedDuration(value / 1000),
  33. },
  34. fcp: {
  35. name: t('First Contentful Paint'),
  36. formatter: (value: number) => getFormattedDuration(value / 1000),
  37. },
  38. fid: {
  39. name: t('First Input Delay'),
  40. formatter: (value: number) => getFormattedDuration(value / 1000),
  41. },
  42. cls: {
  43. name: t('Cumulative Layout Shift'),
  44. formatter: (value: number) => Math.round(value * 100) / 100,
  45. },
  46. ttfb: {
  47. name: t('Time To First Byte'),
  48. formatter: (value: number) => getFormattedDuration(value / 1000),
  49. },
  50. };
  51. const WEB_VITALS_METERS_CONFIG_WITH_INP = {
  52. lcp: WEB_VITALS_METERS_CONFIG.lcp,
  53. fcp: WEB_VITALS_METERS_CONFIG.fcp,
  54. inp: {
  55. name: t('Interaction to Next Paint'),
  56. formatter: (value: number) => getFormattedDuration(value / 1000),
  57. },
  58. cls: WEB_VITALS_METERS_CONFIG.cls,
  59. ttfb: WEB_VITALS_METERS_CONFIG.ttfb,
  60. };
  61. export default function WebVitalMeters({
  62. onClick,
  63. projectData,
  64. projectScore,
  65. showTooltip = true,
  66. }: Props) {
  67. const theme = useTheme();
  68. const shouldReplaceFidWithInp = useReplaceFidWithInpSetting();
  69. if (!projectScore) {
  70. return null;
  71. }
  72. const webVitalsConfig = shouldReplaceFidWithInp
  73. ? WEB_VITALS_METERS_CONFIG_WITH_INP
  74. : WEB_VITALS_METERS_CONFIG;
  75. const webVitals = Object.keys(webVitalsConfig) as WebVitals[];
  76. const colors = theme.charts.getColorPalette(3);
  77. return (
  78. <Container>
  79. <Flex>
  80. {webVitals.map((webVital, index) => {
  81. const webVitalExists = projectScore[`${webVital}Score`] !== undefined;
  82. const formattedMeterValueText = webVitalExists ? (
  83. webVitalsConfig[webVital].formatter(
  84. projectData?.data?.[0]?.[`p75(measurements.${webVital})`] as number
  85. )
  86. ) : (
  87. <NoValue />
  88. );
  89. const headerText = webVitalsConfig[webVital].name;
  90. const meterBody = (
  91. <Fragment>
  92. <MeterBarBody>
  93. {showTooltip && (
  94. <StyledQuestionTooltip
  95. isHoverable
  96. size="xs"
  97. title={
  98. <span>
  99. {tct(
  100. `The p75 [webVital] value and aggregate [webVital] score of your selected project(s).
  101. Scores and values may share some (but not perfect) correlation.`,
  102. {
  103. webVital: webVital.toUpperCase(),
  104. }
  105. )}
  106. <br />
  107. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/#performance-score">
  108. {t('Find out how performance scores are calculated here.')}
  109. </ExternalLink>
  110. </span>
  111. }
  112. />
  113. )}
  114. <MeterHeader>{headerText}</MeterHeader>
  115. <MeterValueText>
  116. <Dot color={colors[index]} />
  117. {formattedMeterValueText}
  118. </MeterValueText>
  119. </MeterBarBody>
  120. <MeterBarFooter score={projectScore[`${webVital}Score`]} />
  121. </Fragment>
  122. );
  123. return (
  124. <MeterBarContainer
  125. key={webVital}
  126. onClick={() => webVitalExists && onClick?.(webVital)}
  127. clickable={webVitalExists}
  128. >
  129. {webVitalExists && <InteractionStateLayer />}
  130. {webVitalExists && meterBody}
  131. {!webVitalExists && (
  132. <StyledTooltip
  133. title={tct('No [webVital] data found in this project.', {
  134. webVital: webVital.toUpperCase(),
  135. })}
  136. >
  137. {meterBody}
  138. </StyledTooltip>
  139. )}
  140. </MeterBarContainer>
  141. );
  142. })}
  143. </Flex>
  144. </Container>
  145. );
  146. }
  147. export const getFormattedDuration = (value: number) => {
  148. return getDuration(value, value < 1 ? 0 : 2, true);
  149. };
  150. const Container = styled('div')`
  151. margin-bottom: ${space(1)};
  152. `;
  153. const Flex = styled('div')<{gap?: number}>`
  154. display: flex;
  155. flex-direction: row;
  156. justify-content: center;
  157. width: 100%;
  158. gap: ${p => (p.gap ? `${p.gap}px` : space(1))};
  159. align-items: center;
  160. flex-wrap: wrap;
  161. `;
  162. const MeterBarContainer = styled('div')<{clickable?: boolean}>`
  163. flex: 1;
  164. position: relative;
  165. padding: 0;
  166. cursor: ${p => (p.clickable ? 'pointer' : 'default')};
  167. min-width: 140px;
  168. `;
  169. const MeterBarBody = styled('div')`
  170. border: 1px solid ${p => p.theme.gray200};
  171. border-radius: ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0 0;
  172. border-bottom: none;
  173. padding: ${space(1)} 0 ${space(0.5)} 0;
  174. `;
  175. const MeterHeader = styled('div')`
  176. font-size: ${p => p.theme.fontSizeSmall};
  177. color: ${p => p.theme.textColor};
  178. display: inline-block;
  179. text-align: center;
  180. width: 100%;
  181. `;
  182. const MeterValueText = styled('div')`
  183. display: flex;
  184. justify-content: center;
  185. align-items: center;
  186. font-size: ${p => p.theme.headerFontSize};
  187. color: ${p => p.theme.textColor};
  188. flex: 1;
  189. text-align: center;
  190. `;
  191. function MeterBarFooter({score}: {score: number | undefined}) {
  192. if (score === undefined) {
  193. return (
  194. <MeterBarFooterContainer status="none">{t('No Data')}</MeterBarFooterContainer>
  195. );
  196. }
  197. const status = scoreToStatus(score);
  198. return (
  199. <MeterBarFooterContainer status={status}>
  200. {STATUS_TEXT[status]} {score}
  201. </MeterBarFooterContainer>
  202. );
  203. }
  204. const MeterBarFooterContainer = styled('div')<{status: string}>`
  205. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  206. border-radius: 0 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius};
  207. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  208. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  209. font-size: ${p => p.theme.fontSizeExtraSmall};
  210. padding: ${space(0.5)};
  211. text-align: center;
  212. `;
  213. const NoValueContainer = styled('span')`
  214. color: ${p => p.theme.gray300};
  215. font-size: ${p => p.theme.headerFontSize};
  216. `;
  217. function NoValue() {
  218. return <NoValueContainer>{' \u2014 '}</NoValueContainer>;
  219. }
  220. const StyledTooltip = styled(Tooltip)`
  221. display: block;
  222. width: 100%;
  223. `;
  224. const StyledQuestionTooltip = styled(QuestionTooltip)`
  225. position: absolute;
  226. right: ${space(1)};
  227. `;
  228. export const Dot = styled('span')<{color: string}>`
  229. display: inline-block;
  230. margin-right: ${space(1)};
  231. border-radius: ${p => p.theme.borderRadius};
  232. width: ${space(1)};
  233. height: ${space(1)};
  234. background-color: ${p => p.color};
  235. `;