webVitalMeters.tsx 6.8 KB

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