webVitalMeters.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. <VitalContainer
  112. key={webVital}
  113. webVital={webVital}
  114. webVitalExists={webVitalExists}
  115. meterBody={meterBody}
  116. onClick={onClick}
  117. />
  118. );
  119. })}
  120. </Flex>
  121. </Container>
  122. );
  123. }
  124. type VitalContainerProps = {
  125. meterBody: React.ReactNode;
  126. webVital: WebVitals;
  127. webVitalExists: boolean;
  128. onClick?: (webVital: WebVitals) => void;
  129. };
  130. function VitalContainer({
  131. webVital,
  132. webVitalExists,
  133. meterBody,
  134. onClick,
  135. }: VitalContainerProps) {
  136. return (
  137. <MeterBarContainer
  138. key={webVital}
  139. onClick={() => webVitalExists && onClick?.(webVital)}
  140. clickable={webVitalExists}
  141. >
  142. {webVitalExists && <InteractionStateLayer />}
  143. {webVitalExists && meterBody}
  144. {!webVitalExists && (
  145. <StyledTooltip
  146. title={tct('No [webVital] data found in this project.', {
  147. webVital: webVital.toUpperCase(),
  148. })}
  149. >
  150. {meterBody}
  151. </StyledTooltip>
  152. )}
  153. </MeterBarContainer>
  154. );
  155. }
  156. export const getFormattedDuration = (value: number) => {
  157. return getDuration(value, value < 1 ? 0 : 2, true);
  158. };
  159. const Container = styled('div')`
  160. margin-bottom: ${space(1)};
  161. `;
  162. const Flex = styled('div')<{gap?: number}>`
  163. display: flex;
  164. flex-direction: row;
  165. justify-content: center;
  166. width: 100%;
  167. gap: ${p => (p.gap ? `${p.gap}px` : space(1))};
  168. align-items: center;
  169. flex-wrap: wrap;
  170. `;
  171. const MeterBarContainer = styled('div')<{clickable?: boolean}>`
  172. flex: 1;
  173. position: relative;
  174. padding: 0;
  175. cursor: ${p => (p.clickable ? 'pointer' : 'default')};
  176. min-width: 140px;
  177. `;
  178. const MeterBarBody = styled('div')`
  179. border: 1px solid ${p => p.theme.gray200};
  180. border-radius: ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0 0;
  181. border-bottom: none;
  182. padding: ${space(1)} 0 ${space(0.5)} 0;
  183. `;
  184. const MeterHeader = styled('div')`
  185. font-size: ${p => p.theme.fontSizeSmall};
  186. color: ${p => p.theme.textColor};
  187. display: inline-block;
  188. text-align: center;
  189. width: 100%;
  190. `;
  191. const MeterValueText = styled('div')`
  192. display: flex;
  193. justify-content: center;
  194. align-items: center;
  195. font-size: ${p => p.theme.headerFontSize};
  196. color: ${p => p.theme.textColor};
  197. flex: 1;
  198. text-align: center;
  199. `;
  200. function MeterBarFooter({score}: {score: number | undefined}) {
  201. if (score === undefined) {
  202. return (
  203. <MeterBarFooterContainer status="none">{t('No Data')}</MeterBarFooterContainer>
  204. );
  205. }
  206. const status = scoreToStatus(score);
  207. return (
  208. <MeterBarFooterContainer status={status}>
  209. {STATUS_TEXT[status]} {score}
  210. </MeterBarFooterContainer>
  211. );
  212. }
  213. const MeterBarFooterContainer = styled('div')<{status: string}>`
  214. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  215. border-radius: 0 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius};
  216. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  217. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  218. font-size: ${p => p.theme.fontSizeExtraSmall};
  219. padding: ${space(0.5)};
  220. text-align: center;
  221. `;
  222. const NoValueContainer = styled('span')`
  223. color: ${p => p.theme.gray300};
  224. font-size: ${p => p.theme.headerFontSize};
  225. `;
  226. function NoValue() {
  227. return <NoValueContainer>{' \u2014 '}</NoValueContainer>;
  228. }
  229. const StyledTooltip = styled(Tooltip)`
  230. display: block;
  231. width: 100%;
  232. `;
  233. const StyledQuestionTooltip = styled(QuestionTooltip)`
  234. position: absolute;
  235. right: ${space(1)};
  236. `;
  237. export const Dot = styled('span')<{color: string}>`
  238. display: inline-block;
  239. margin-right: ${space(1)};
  240. border-radius: ${p => p.theme.borderRadius};
  241. width: ${space(1)};
  242. height: ${space(1)};
  243. background-color: ${p => p.color};
  244. `;