webVitalMeters.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 {VITAL_DESCRIPTIONS} from 'sentry/views/insights/browser/webVitals/components/webVitalDescription';
  13. import {MODULE_DOC_LINK} from 'sentry/views/insights/browser/webVitals/settings';
  14. import type {
  15. ProjectScore,
  16. WebVitals,
  17. } from 'sentry/views/insights/browser/webVitals/types';
  18. import {PERFORMANCE_SCORE_COLORS} from 'sentry/views/insights/browser/webVitals/utils/performanceScoreColors';
  19. import {
  20. scoreToStatus,
  21. STATUS_TEXT,
  22. } from 'sentry/views/insights/browser/webVitals/utils/scoreToStatus';
  23. type Props = {
  24. onClick?: (webVital: WebVitals) => void;
  25. projectData?: TableData;
  26. projectScore?: ProjectScore;
  27. showTooltip?: boolean;
  28. transaction?: string;
  29. };
  30. const WEB_VITALS_METERS_CONFIG = {
  31. lcp: {
  32. name: t('Largest Contentful Paint'),
  33. formatter: (value: number) => getFormattedDuration(value / 1000),
  34. },
  35. fcp: {
  36. name: t('First Contentful Paint'),
  37. formatter: (value: number) => getFormattedDuration(value / 1000),
  38. },
  39. inp: {
  40. name: t('Interaction to Next Paint'),
  41. formatter: (value: number) => getFormattedDuration(value / 1000),
  42. },
  43. cls: {
  44. name: t('Cumulative Layout Shift'),
  45. formatter: (value: number) => Math.round(value * 100) / 100,
  46. },
  47. ttfb: {
  48. name: t('Time To First Byte'),
  49. formatter: (value: number) => getFormattedDuration(value / 1000),
  50. },
  51. };
  52. export default function WebVitalMeters({
  53. onClick,
  54. projectData,
  55. projectScore,
  56. showTooltip = true,
  57. }: Props) {
  58. const theme = useTheme();
  59. if (!projectScore) {
  60. return null;
  61. }
  62. const webVitalsConfig = WEB_VITALS_METERS_CONFIG;
  63. const webVitals = Object.keys(webVitalsConfig) as WebVitals[];
  64. const colors = theme.charts.getColorPalette(3) ?? [];
  65. const renderVitals = () => {
  66. return webVitals.map((webVital, index) => {
  67. const webVitalKey = `p75(measurements.${webVital})`;
  68. const score = projectScore[`${webVital}Score`];
  69. const meterValue = projectData?.data?.[0]?.[webVitalKey] as number;
  70. if (!score) {
  71. return null;
  72. }
  73. return (
  74. <VitalMeter
  75. key={webVital}
  76. webVital={webVital}
  77. showTooltip={showTooltip}
  78. score={score}
  79. meterValue={meterValue}
  80. color={colors[index]!}
  81. onClick={onClick}
  82. />
  83. );
  84. });
  85. };
  86. return (
  87. <Container>
  88. <Flex>{renderVitals()}</Flex>
  89. </Container>
  90. );
  91. }
  92. type VitalMeterProps = {
  93. color: string;
  94. meterValue: number | undefined;
  95. score: number | undefined;
  96. showTooltip: boolean;
  97. webVital: WebVitals;
  98. isAggregateMode?: boolean;
  99. onClick?: (webVital: WebVitals) => void;
  100. };
  101. export function VitalMeter({
  102. webVital,
  103. showTooltip,
  104. score,
  105. meterValue,
  106. color,
  107. onClick,
  108. isAggregateMode = true,
  109. }: VitalMeterProps) {
  110. const webVitalsConfig = WEB_VITALS_METERS_CONFIG;
  111. const webVitalExists = score !== undefined;
  112. const formattedMeterValueText =
  113. webVitalExists && meterValue ? (
  114. webVitalsConfig[webVital].formatter(meterValue)
  115. ) : (
  116. <NoValue />
  117. );
  118. const webVitalKey = `measurements.${webVital}`;
  119. const {shortDescription} = VITAL_DESCRIPTIONS[webVitalKey];
  120. const headerText = webVitalsConfig[webVital].name;
  121. const meterBody = (
  122. <Fragment>
  123. <MeterBarBody>
  124. {showTooltip && (
  125. <StyledQuestionTooltip
  126. isHoverable
  127. size="xs"
  128. title={
  129. <span>
  130. {shortDescription}
  131. <br />
  132. <ExternalLink href={`${MODULE_DOC_LINK}#performance-score`}>
  133. {t('Find out how performance scores are calculated here.')}
  134. </ExternalLink>
  135. </span>
  136. }
  137. />
  138. )}
  139. <MeterHeader>{headerText}</MeterHeader>
  140. <MeterValueText>
  141. <Dot color={color} />
  142. {formattedMeterValueText}
  143. </MeterValueText>
  144. </MeterBarBody>
  145. <MeterBarFooter score={score} />
  146. </Fragment>
  147. );
  148. return (
  149. <VitalContainer
  150. key={webVital}
  151. webVital={webVital}
  152. webVitalExists={webVitalExists}
  153. meterBody={meterBody}
  154. onClick={onClick}
  155. isAggregateMode={isAggregateMode}
  156. />
  157. );
  158. }
  159. type VitalContainerProps = {
  160. meterBody: React.ReactNode;
  161. webVital: WebVitals;
  162. webVitalExists: boolean;
  163. isAggregateMode?: boolean;
  164. onClick?: (webVital: WebVitals) => void;
  165. };
  166. function VitalContainer({
  167. webVital,
  168. webVitalExists,
  169. meterBody,
  170. onClick,
  171. isAggregateMode = true,
  172. }: VitalContainerProps) {
  173. return (
  174. <MeterBarContainer
  175. key={webVital}
  176. onClick={() => webVitalExists && onClick?.(webVital)}
  177. clickable={webVitalExists}
  178. >
  179. {webVitalExists && <InteractionStateLayer />}
  180. {webVitalExists && meterBody}
  181. {!webVitalExists && (
  182. <StyledTooltip
  183. title={tct('No [webVital] data found in this [selection].', {
  184. webVital: webVital.toUpperCase(),
  185. selection: isAggregateMode ? 'project' : 'trace',
  186. })}
  187. >
  188. {meterBody}
  189. </StyledTooltip>
  190. )}
  191. </MeterBarContainer>
  192. );
  193. }
  194. export const getFormattedDuration = (value: number) => {
  195. return getDuration(value, value < 1 ? 0 : 2, true);
  196. };
  197. const Container = styled('div')`
  198. margin-bottom: ${space(1)};
  199. `;
  200. const Flex = styled('div')<{gap?: number}>`
  201. display: flex;
  202. flex-direction: row;
  203. justify-content: center;
  204. width: 100%;
  205. gap: ${p => (p.gap ? `${p.gap}px` : space(1))};
  206. align-items: center;
  207. flex-wrap: wrap;
  208. `;
  209. const MeterBarContainer = styled('div')<{clickable?: boolean}>`
  210. background-color: ${p => p.theme.background};
  211. flex: 1;
  212. position: relative;
  213. padding: 0;
  214. cursor: ${p => (p.clickable ? 'pointer' : 'default')};
  215. min-width: 140px;
  216. `;
  217. const MeterBarBody = styled('div')`
  218. border: 1px solid ${p => p.theme.gray200};
  219. border-radius: ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0 0;
  220. border-bottom: none;
  221. padding: ${space(1)} 0 ${space(0.5)} 0;
  222. `;
  223. const MeterHeader = styled('div')`
  224. font-size: ${p => p.theme.fontSizeSmall};
  225. font-weight: ${p => p.theme.fontWeightBold};
  226. color: ${p => p.theme.textColor};
  227. display: inline-block;
  228. text-align: center;
  229. width: 100%;
  230. `;
  231. const MeterValueText = styled('div')`
  232. display: flex;
  233. justify-content: center;
  234. align-items: center;
  235. font-size: ${p => p.theme.headerFontSize};
  236. color: ${p => p.theme.textColor};
  237. flex: 1;
  238. text-align: center;
  239. `;
  240. function MeterBarFooter({score}: {score: number | undefined}) {
  241. if (score === undefined) {
  242. return (
  243. <MeterBarFooterContainer status="none">{t('No Data')}</MeterBarFooterContainer>
  244. );
  245. }
  246. const status = scoreToStatus(score);
  247. return (
  248. <MeterBarFooterContainer status={status}>
  249. {STATUS_TEXT[status]} {score}
  250. </MeterBarFooterContainer>
  251. );
  252. }
  253. const MeterBarFooterContainer = styled('div')<{status: string}>`
  254. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  255. border-radius: 0 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius};
  256. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  257. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].border]};
  258. font-size: ${p => p.theme.fontSizeExtraSmall};
  259. padding: ${space(0.5)};
  260. text-align: center;
  261. `;
  262. const NoValueContainer = styled('span')`
  263. color: ${p => p.theme.gray300};
  264. font-size: ${p => p.theme.headerFontSize};
  265. `;
  266. function NoValue() {
  267. return <NoValueContainer>{' \u2014 '}</NoValueContainer>;
  268. }
  269. const StyledTooltip = styled(Tooltip)`
  270. display: block;
  271. width: 100%;
  272. `;
  273. const StyledQuestionTooltip = styled(QuestionTooltip)`
  274. position: absolute;
  275. right: ${space(1)};
  276. `;
  277. export const Dot = styled('span')<{color: string}>`
  278. display: inline-block;
  279. margin-right: ${space(1)};
  280. border-radius: ${p => p.theme.borderRadius};
  281. width: ${space(1)};
  282. height: ${space(1)};
  283. background-color: ${p => p.color};
  284. `;
  285. // A compressed version of the VitalMeter component used in the trace context panel
  286. type VitalPillProps = Omit<
  287. VitalMeterProps,
  288. 'showTooltip' | 'isAggregateMode' | 'onClick' | 'color'
  289. >;
  290. export function VitalPill({webVital, score, meterValue}: VitalPillProps) {
  291. const status = score !== undefined ? scoreToStatus(score) : 'none';
  292. const webVitalExists = score !== undefined;
  293. const webVitalsConfig = WEB_VITALS_METERS_CONFIG;
  294. const formattedMeterValueText =
  295. webVitalExists && meterValue ? (
  296. webVitalsConfig[webVital].formatter(meterValue)
  297. ) : (
  298. <NoValue />
  299. );
  300. const tooltipText = VITAL_DESCRIPTIONS[`measurements.${webVital}`];
  301. return (
  302. <VitalPillContainer>
  303. <Tooltip title={tooltipText?.shortDescription}>
  304. <VitalPillName status={status}>
  305. {`${webVital ? webVital.toUpperCase() : ''} (${STATUS_TEXT[status] ?? 'N/A'})`}
  306. </VitalPillName>
  307. </Tooltip>
  308. <VitalPillValue>{formattedMeterValueText}</VitalPillValue>
  309. </VitalPillContainer>
  310. );
  311. }
  312. const VitalPillContainer = styled('div')`
  313. display: flex;
  314. flex-direction: row;
  315. width: 100%;
  316. height: 30px;
  317. `;
  318. const VitalPillName = styled('div')<{status: string}>`
  319. display: flex;
  320. align-items: center;
  321. position: relative;
  322. height: 100%;
  323. padding: 0 ${space(1)};
  324. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].border]};
  325. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  326. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  327. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  328. font-size: ${p => p.theme.fontSizeSmall};
  329. font-weight: ${p => p.theme.fontWeightBold};
  330. text-decoration: underline;
  331. text-decoration-style: dotted;
  332. text-underline-offset: ${space(0.25)};
  333. text-decoration-thickness: 1px;
  334. cursor: pointer;
  335. `;
  336. const VitalPillValue = styled('div')`
  337. display: flex;
  338. flex: 1;
  339. align-items: center;
  340. justify-content: flex-end;
  341. height: 100%;
  342. padding: 0 ${space(0.5)};
  343. border: 1px solid ${p => p.theme.gray200};
  344. border-left: none;
  345. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  346. background: ${p => p.theme.background};
  347. color: ${p => p.theme.textColor};
  348. font-size: ${p => p.theme.fontSizeLarge};
  349. `;