vitalsComparison.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import React, {useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import {
  5. INDUSTRY_STANDARDS,
  6. MIN_VITAL_COUNT_FOR_DISPLAY,
  7. SENTRY_CUSTOMERS,
  8. } from 'sentry/components/performance/vitalsAlert/constants';
  9. import QuestionTooltip from 'sentry/components/questionTooltip';
  10. import Tag from 'sentry/components/tag';
  11. import {t, tct} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Organization} from 'sentry/types';
  14. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  15. import EventView from 'sentry/utils/discover/eventView';
  16. import {WebVital} from 'sentry/utils/fields';
  17. import VitalsCardDiscoverQuery from 'sentry/utils/performance/vitals/vitalsCardsDiscoverQuery';
  18. import {webVitalMeh, webVitalPoor} from 'sentry/views/performance/vitalDetail/utils';
  19. type Score = 'poor' | 'meh' | 'good';
  20. type ViewProps = Pick<
  21. EventView,
  22. 'environment' | 'project' | 'start' | 'end' | 'statsPeriod'
  23. >;
  24. type Props = ViewProps & {
  25. location: Location;
  26. organization: Organization;
  27. vital: WebVital | WebVital[];
  28. };
  29. const SUPPORTED_VITALS = ['measurements.fcp', 'measurements.lcp'];
  30. function getScore(vital: WebVital, value: number): Score {
  31. const poorScore = webVitalPoor[vital];
  32. const mehScore = webVitalMeh[vital];
  33. if (value > poorScore) {
  34. return 'poor';
  35. }
  36. if (value > mehScore) {
  37. return 'meh';
  38. }
  39. return 'good';
  40. }
  41. function getIndicatorString(score: Score) {
  42. switch (score) {
  43. case 'poor':
  44. return t('Poor');
  45. case 'meh':
  46. return t('Meh');
  47. default:
  48. return t('Good');
  49. }
  50. }
  51. function getTagLevel(score: Score) {
  52. switch (score) {
  53. case 'poor':
  54. return 'error';
  55. case 'meh':
  56. return 'warning';
  57. default:
  58. return 'success';
  59. }
  60. }
  61. function MetricsCard({
  62. title,
  63. vital,
  64. value,
  65. tooltip,
  66. }: {
  67. title: string;
  68. tooltip: string;
  69. value: number;
  70. vital: WebVital;
  71. }) {
  72. // round to 2 decimals if <10s, otherwise use just 1 decimal
  73. const score = getScore(vital, value);
  74. const numDecimals = value >= 10_000 ? 1 : 2;
  75. const timeInSeconds = value / 1000.0;
  76. return (
  77. <MetricsCardWrapper>
  78. <MetricsTitle>
  79. {title} (p75) <StyledQuestionTooltip title={tooltip} size="xs" />
  80. </MetricsTitle>
  81. <ScoreWrapper>
  82. <ScoreContent>{timeInSeconds.toFixed(numDecimals)}s</ScoreContent>
  83. <TagWrapper>
  84. <StyledTag type={getTagLevel(score)}>{getIndicatorString(score)}</StyledTag>
  85. </TagWrapper>
  86. </ScoreWrapper>
  87. </MetricsCardWrapper>
  88. );
  89. }
  90. function ContentWrapper({
  91. organization,
  92. vital,
  93. children,
  94. count,
  95. p75,
  96. }: {
  97. children: React.ReactNode;
  98. count: number;
  99. organization: Organization;
  100. p75: number;
  101. vital: WebVital;
  102. }) {
  103. useEffect(() => {
  104. trackAdvancedAnalyticsEvent('performance_views.vital_detail.comparison_viewed', {
  105. organization,
  106. vital,
  107. count,
  108. p75,
  109. });
  110. });
  111. return <Container>{children}</Container>;
  112. }
  113. function VitalsComparison(props: Props) {
  114. const {location, vital: _vital, organization} = props;
  115. const vitals = Array.isArray(_vital) ? _vital : [_vital];
  116. const vital = vitals[0];
  117. if (!SUPPORTED_VITALS.includes(vital)) {
  118. return null;
  119. }
  120. return (
  121. <VitalsCardDiscoverQuery location={location} vitals={vitals}>
  122. {({isLoading, vitalsData}) => {
  123. if (isLoading || !vitalsData) {
  124. return null;
  125. }
  126. const {p75} = vitalsData[vital];
  127. if (!p75) {
  128. return null;
  129. }
  130. const lookupName = vital === 'measurements.fcp' ? 'FCP' : 'LCP';
  131. const sentryStandard = SENTRY_CUSTOMERS[lookupName];
  132. const industryStandard = INDUSTRY_STANDARDS[lookupName];
  133. const count = vitalsData[vital].total;
  134. // only show it if we hit the min number
  135. if (count < MIN_VITAL_COUNT_FOR_DISPLAY) {
  136. return null;
  137. }
  138. return (
  139. <ContentWrapper {...{organization, vital, count, p75}}>
  140. <MetricsCard
  141. title={t('Selected Projects')}
  142. vital={vital}
  143. value={p75}
  144. tooltip={tct(
  145. "25% of your project's transactions have an [lookupName] greater than this number. Good, Bad, Meh segmentation is based on Google industry standards.",
  146. {lookupName}
  147. )}
  148. />
  149. <MetricsCard
  150. title={t('Sentry Peers')}
  151. vital={vital}
  152. value={sentryStandard}
  153. tooltip={tct(
  154. '20% of Sentry customers have a p75 [lookupName] lower than this.',
  155. {lookupName}
  156. )}
  157. />
  158. <MetricsCard
  159. title={t('Industry Standard')}
  160. vital={vital}
  161. value={industryStandard}
  162. tooltip={tct(
  163. "Calculated as a Good [lookupName] based on Google's industry standards.",
  164. {lookupName}
  165. )}
  166. />
  167. </ContentWrapper>
  168. );
  169. }}
  170. </VitalsCardDiscoverQuery>
  171. );
  172. }
  173. export default VitalsComparison;
  174. const Container = styled('div')`
  175. display: grid;
  176. grid-template-columns: 1fr 1fr 1fr;
  177. gap: ${space(2)};
  178. `;
  179. const ScoreContent = styled('h6')`
  180. margin: auto;
  181. `;
  182. const ScoreWrapper = styled('div')`
  183. display: flex;
  184. align-items: center;
  185. `;
  186. const MetricsCardWrapper = styled('div')`
  187. display: flex;
  188. flex-direction: row;
  189. justify-content: space-between;
  190. border: 1px ${p => p.theme.gray200};
  191. border-radius: 4px;
  192. border-style: solid;
  193. align-items: center;
  194. height: 57px;
  195. padding: ${space(2)};
  196. margin-bottom: ${space(2)};
  197. `;
  198. const StyledTag = styled(Tag)`
  199. margin-left: ${space(1)};
  200. `;
  201. const MetricsTitle = styled('span')`
  202. font-size: 14px;
  203. `;
  204. const TagWrapper = styled('span')`
  205. margin: auto;
  206. `;
  207. const StyledQuestionTooltip = styled(QuestionTooltip)`
  208. position: relative;
  209. top: 1px;
  210. `;