webVitalStatusLineChart.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {useTheme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import ChartZoom from 'sentry/components/charts/chartZoom';
  4. import MarkArea from 'sentry/components/charts/components/markArea';
  5. import MarkLine from 'sentry/components/charts/components/markLine';
  6. import {LineChart, LineChartSeries} from 'sentry/components/charts/lineChart';
  7. import {getDuration} from 'sentry/utils/formatters';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import useRouter from 'sentry/utils/useRouter';
  10. import {
  11. PERFORMANCE_SCORE_MEDIANS,
  12. PERFORMANCE_SCORE_P90S,
  13. } from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  14. interface Props {
  15. webVitalSeries: LineChartSeries;
  16. }
  17. export function WebVitalStatusLineChart({webVitalSeries}: Props) {
  18. const theme = useTheme();
  19. const router = useRouter();
  20. const pageFilters = usePageFilters();
  21. const {period, start, end, utc} = pageFilters.selection.datetime;
  22. const webVital = webVitalSeries.seriesName;
  23. const allSeries = [webVitalSeries];
  24. const showPoorMarkLine = webVitalSeries.data?.some(
  25. ({value}) => value > PERFORMANCE_SCORE_MEDIANS[webVital ?? '']
  26. );
  27. const showMehMarkLine = webVitalSeries.data?.some(
  28. ({value}) => value >= PERFORMANCE_SCORE_P90S[webVital ?? '']
  29. );
  30. const showGoodMarkLine = webVitalSeries.data?.every(
  31. ({value}) => value < PERFORMANCE_SCORE_P90S[webVital ?? '']
  32. );
  33. const goodMarkArea = MarkArea({
  34. silent: true,
  35. itemStyle: {
  36. color: theme.green300,
  37. opacity: 0.1,
  38. },
  39. data: [
  40. [
  41. {
  42. yAxis: PERFORMANCE_SCORE_P90S[webVital ?? ''],
  43. },
  44. {
  45. yAxis: 0,
  46. },
  47. ],
  48. ],
  49. });
  50. const mehMarkArea = MarkArea({
  51. silent: true,
  52. itemStyle: {
  53. color: theme.yellow300,
  54. opacity: 0.1,
  55. },
  56. data: [
  57. [
  58. {
  59. yAxis: PERFORMANCE_SCORE_MEDIANS[webVital ?? ''],
  60. },
  61. {
  62. yAxis: PERFORMANCE_SCORE_P90S[webVital ?? ''],
  63. },
  64. ],
  65. ],
  66. });
  67. const poorMarkArea = MarkArea({
  68. silent: true,
  69. itemStyle: {
  70. color: theme.red300,
  71. opacity: 0.1,
  72. },
  73. data: [
  74. [
  75. {
  76. yAxis: PERFORMANCE_SCORE_MEDIANS[webVital ?? ''],
  77. },
  78. {
  79. yAxis: Infinity,
  80. },
  81. ],
  82. ],
  83. });
  84. const goodMarkLine = MarkLine({
  85. silent: true,
  86. lineStyle: {
  87. color: theme.green300,
  88. },
  89. label: {
  90. formatter: () => 'Good',
  91. position: 'insideEndBottom',
  92. color: theme.green300,
  93. },
  94. data: showGoodMarkLine
  95. ? [
  96. [
  97. {xAxis: 'min', y: 10},
  98. {xAxis: 'max', y: 10},
  99. ],
  100. ]
  101. : [
  102. {
  103. yAxis: PERFORMANCE_SCORE_P90S[webVital ?? ''],
  104. },
  105. ],
  106. });
  107. const mehMarkLine = MarkLine({
  108. silent: true,
  109. lineStyle: {
  110. color: theme.yellow300,
  111. },
  112. label: {
  113. formatter: () => 'Meh',
  114. position: 'insideEndBottom',
  115. color: theme.yellow300,
  116. },
  117. data:
  118. showMehMarkLine && !showPoorMarkLine
  119. ? [
  120. [
  121. {xAxis: 'min', y: 10},
  122. {xAxis: 'max', y: 10},
  123. ],
  124. ]
  125. : [
  126. {
  127. yAxis: PERFORMANCE_SCORE_MEDIANS[webVital ?? ''],
  128. },
  129. ],
  130. });
  131. const poorMarkLine = MarkLine({
  132. silent: true,
  133. lineStyle: {
  134. color: theme.red300,
  135. },
  136. label: {
  137. formatter: () => 'Poor',
  138. position: 'insideEndBottom',
  139. color: theme.red300,
  140. },
  141. data: [
  142. [
  143. {xAxis: 'min', y: 10},
  144. {xAxis: 'max', y: 10},
  145. ],
  146. ],
  147. });
  148. allSeries.push({
  149. seriesName: '',
  150. type: 'line',
  151. markArea: goodMarkArea,
  152. data: [],
  153. });
  154. allSeries.push({
  155. seriesName: '',
  156. type: 'line',
  157. markArea: mehMarkArea,
  158. data: [],
  159. });
  160. allSeries.push({
  161. seriesName: '',
  162. type: 'line',
  163. markArea: poorMarkArea,
  164. data: [],
  165. });
  166. allSeries.push({
  167. seriesName: '',
  168. type: 'line',
  169. markLine: goodMarkLine,
  170. data: [],
  171. });
  172. allSeries.push({
  173. seriesName: '',
  174. type: 'line',
  175. markLine: mehMarkLine,
  176. data: [],
  177. });
  178. if (showPoorMarkLine) {
  179. allSeries.push({
  180. seriesName: '',
  181. type: 'line',
  182. markLine: poorMarkLine,
  183. data: [],
  184. });
  185. }
  186. const getFormattedDuration = (value: number) => {
  187. if (value < 1000) {
  188. return getDuration(value / 1000, 0, true);
  189. }
  190. return getDuration(value / 1000, 2, true);
  191. };
  192. return (
  193. <ChartContainer>
  194. {webVital && (
  195. <ChartZoom router={router} period={period} start={start} end={end} utc={utc}>
  196. {zoomRenderProps => (
  197. <LineChart
  198. {...zoomRenderProps}
  199. height={240}
  200. series={allSeries}
  201. xAxis={{show: false}}
  202. grid={{
  203. left: 0,
  204. right: 15,
  205. top: 10,
  206. bottom: 0,
  207. }}
  208. yAxis={
  209. webVital === 'cls' ? {} : {axisLabel: {formatter: getFormattedDuration}}
  210. }
  211. tooltip={webVital === 'cls' ? {} : {valueFormatter: getFormattedDuration}}
  212. />
  213. )}
  214. </ChartZoom>
  215. )}
  216. </ChartContainer>
  217. );
  218. }
  219. const ChartContainer = styled('div')`
  220. position: relative;
  221. flex: 1;
  222. `;