webVitalMeters.tsx 7.2 KB

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