webVitalMeters.tsx 7.5 KB

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