webVitalMeters.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 {MODULE_DOC_LINK} from 'sentry/views/performance/browser/webVitals/settings';
  13. import {PERFORMANCE_SCORE_COLORS} from 'sentry/views/performance/browser/webVitals/utils/performanceScoreColors';
  14. import {
  15. scoreToStatus,
  16. STATUS_TEXT,
  17. } from 'sentry/views/performance/browser/webVitals/utils/scoreToStatus';
  18. import type {
  19. ProjectScore,
  20. WebVitals,
  21. } from 'sentry/views/performance/browser/webVitals/utils/types';
  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. if (!projectScore) {
  69. return null;
  70. }
  71. const webVitalsConfig = WEB_VITALS_METERS_CONFIG_WITH_INP;
  72. const webVitals = Object.keys(webVitalsConfig) as WebVitals[];
  73. const colors = theme.charts.getColorPalette(3);
  74. return (
  75. <Container>
  76. <Flex>
  77. {webVitals.map((webVital, index) => {
  78. const webVitalExists = projectScore[`${webVital}Score`] !== undefined;
  79. const formattedMeterValueText = webVitalExists ? (
  80. webVitalsConfig[webVital].formatter(
  81. projectData?.data?.[0]?.[`p75(measurements.${webVital})`] as number
  82. )
  83. ) : (
  84. <NoValue />
  85. );
  86. const headerText = webVitalsConfig[webVital].name;
  87. const meterBody = (
  88. <Fragment>
  89. <MeterBarBody>
  90. {showTooltip && (
  91. <StyledQuestionTooltip
  92. isHoverable
  93. size="xs"
  94. title={
  95. <span>
  96. {tct(
  97. `The p75 [webVital] value and aggregate [webVital] score of your selected project(s).
  98. Scores and values may share some (but not perfect) correlation.`,
  99. {
  100. webVital: webVital.toUpperCase(),
  101. }
  102. )}
  103. <br />
  104. <ExternalLink href={`${MODULE_DOC_LINK}#performance-score`}>
  105. {t('Find out how performance scores are calculated here.')}
  106. </ExternalLink>
  107. </span>
  108. }
  109. />
  110. )}
  111. <MeterHeader>{headerText}</MeterHeader>
  112. <MeterValueText>
  113. <Dot color={colors[index]} />
  114. {formattedMeterValueText}
  115. </MeterValueText>
  116. </MeterBarBody>
  117. <MeterBarFooter score={projectScore[`${webVital}Score`]} />
  118. </Fragment>
  119. );
  120. return (
  121. <MeterBarContainer
  122. key={webVital}
  123. onClick={() => webVitalExists && onClick?.(webVital)}
  124. clickable={webVitalExists}
  125. >
  126. {webVitalExists && <InteractionStateLayer />}
  127. {webVitalExists && meterBody}
  128. {!webVitalExists && (
  129. <StyledTooltip
  130. title={tct('No [webVital] data found in this project.', {
  131. webVital: webVital.toUpperCase(),
  132. })}
  133. >
  134. {meterBody}
  135. </StyledTooltip>
  136. )}
  137. </MeterBarContainer>
  138. );
  139. })}
  140. </Flex>
  141. </Container>
  142. );
  143. }
  144. export const getFormattedDuration = (value: number) => {
  145. return getDuration(value, value < 1 ? 0 : 2, true);
  146. };
  147. const Container = styled('div')`
  148. margin-bottom: ${space(1)};
  149. `;
  150. const Flex = styled('div')<{gap?: number}>`
  151. display: flex;
  152. flex-direction: row;
  153. justify-content: center;
  154. width: 100%;
  155. gap: ${p => (p.gap ? `${p.gap}px` : space(1))};
  156. align-items: center;
  157. flex-wrap: wrap;
  158. `;
  159. const MeterBarContainer = styled('div')<{clickable?: boolean}>`
  160. flex: 1;
  161. position: relative;
  162. padding: 0;
  163. cursor: ${p => (p.clickable ? 'pointer' : 'default')};
  164. min-width: 140px;
  165. `;
  166. const MeterBarBody = styled('div')`
  167. border: 1px solid ${p => p.theme.gray200};
  168. border-radius: ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0 0;
  169. border-bottom: none;
  170. padding: ${space(1)} 0 ${space(0.5)} 0;
  171. `;
  172. const MeterHeader = styled('div')`
  173. font-size: ${p => p.theme.fontSizeSmall};
  174. color: ${p => p.theme.textColor};
  175. display: inline-block;
  176. text-align: center;
  177. width: 100%;
  178. `;
  179. const MeterValueText = styled('div')`
  180. display: flex;
  181. justify-content: center;
  182. align-items: center;
  183. font-size: ${p => p.theme.headerFontSize};
  184. color: ${p => p.theme.textColor};
  185. flex: 1;
  186. text-align: center;
  187. `;
  188. function MeterBarFooter({score}: {score: number | undefined}) {
  189. if (score === undefined) {
  190. return (
  191. <MeterBarFooterContainer status="none">{t('No Data')}</MeterBarFooterContainer>
  192. );
  193. }
  194. const status = scoreToStatus(score);
  195. return (
  196. <MeterBarFooterContainer status={status}>
  197. {STATUS_TEXT[status]} {score}
  198. </MeterBarFooterContainer>
  199. );
  200. }
  201. const MeterBarFooterContainer = styled('div')<{status: string}>`
  202. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  203. border-radius: 0 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius};
  204. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  205. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  206. font-size: ${p => p.theme.fontSizeExtraSmall};
  207. padding: ${space(0.5)};
  208. text-align: center;
  209. `;
  210. const NoValueContainer = styled('span')`
  211. color: ${p => p.theme.gray300};
  212. font-size: ${p => p.theme.headerFontSize};
  213. `;
  214. function NoValue() {
  215. return <NoValueContainer>{' \u2014 '}</NoValueContainer>;
  216. }
  217. const StyledTooltip = styled(Tooltip)`
  218. display: block;
  219. width: 100%;
  220. `;
  221. const StyledQuestionTooltip = styled(QuestionTooltip)`
  222. position: absolute;
  223. right: ${space(1)};
  224. `;
  225. export const Dot = styled('span')<{color: string}>`
  226. display: inline-block;
  227. margin-right: ${space(1)};
  228. border-radius: ${p => p.theme.borderRadius};
  229. width: ${space(1)};
  230. height: ${space(1)};
  231. background-color: ${p => p.color};
  232. `;