webVitalMeters.tsx 7.2 KB

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