vitalsComparison.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 data = vitalsData[vital];
  127. if (!data || !data.p75) {
  128. return null;
  129. }
  130. const {p75} = data;
  131. const lookupName = vital === 'measurements.fcp' ? 'FCP' : 'LCP';
  132. const sentryStandard = SENTRY_CUSTOMERS[lookupName];
  133. const industryStandard = INDUSTRY_STANDARDS[lookupName];
  134. const count = vitalsData[vital].total;
  135. // only show it if we hit the min number
  136. if (count < MIN_VITAL_COUNT_FOR_DISPLAY) {
  137. return null;
  138. }
  139. return (
  140. <ContentWrapper {...{organization, vital, count, p75}}>
  141. <MetricsCard
  142. title={t('Selected Projects')}
  143. vital={vital}
  144. value={p75}
  145. tooltip={tct(
  146. "25% of your project's transactions have an [lookupName] greater than this number. Good, Bad, Meh segmentation is based on Google industry standards.",
  147. {lookupName}
  148. )}
  149. />
  150. <MetricsCard
  151. title={t('Sentry Peers')}
  152. vital={vital}
  153. value={sentryStandard}
  154. tooltip={tct(
  155. '20% of Sentry customers have a p75 [lookupName] lower than this.',
  156. {lookupName}
  157. )}
  158. />
  159. <MetricsCard
  160. title={t('Industry Standard')}
  161. vital={vital}
  162. value={industryStandard}
  163. tooltip={tct(
  164. "Calculated as a Good [lookupName] based on Google's industry standards.",
  165. {lookupName}
  166. )}
  167. />
  168. </ContentWrapper>
  169. );
  170. }}
  171. </VitalsCardDiscoverQuery>
  172. );
  173. }
  174. export default VitalsComparison;
  175. const Container = styled('div')`
  176. display: grid;
  177. grid-template-columns: 1fr 1fr 1fr;
  178. gap: ${space(2)};
  179. `;
  180. const ScoreContent = styled('h6')`
  181. margin: auto;
  182. `;
  183. const ScoreWrapper = styled('div')`
  184. display: flex;
  185. align-items: center;
  186. `;
  187. const MetricsCardWrapper = styled('div')`
  188. display: flex;
  189. flex-direction: row;
  190. justify-content: space-between;
  191. border: 1px ${p => p.theme.gray200};
  192. border-radius: 4px;
  193. border-style: solid;
  194. align-items: center;
  195. height: 57px;
  196. padding: ${space(2)};
  197. margin-bottom: ${space(2)};
  198. `;
  199. const StyledTag = styled(Tag)`
  200. margin-left: ${space(1)};
  201. `;
  202. const MetricsTitle = styled('span')`
  203. font-size: 14px;
  204. `;
  205. const TagWrapper = styled('span')`
  206. margin: auto;
  207. `;
  208. const StyledQuestionTooltip = styled(QuestionTooltip)`
  209. position: relative;
  210. top: 1px;
  211. `;