vitalChartMetrics.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {useTheme} from '@emotion/react';
  2. import moment from 'moment-timezone';
  3. import ChartZoom from 'sentry/components/charts/chartZoom';
  4. import ErrorPanel from 'sentry/components/charts/errorPanel';
  5. import {LineChart} from 'sentry/components/charts/lineChart';
  6. import ReleaseSeries from 'sentry/components/charts/releaseSeries';
  7. import {ChartContainer, HeaderTitleLegend} from 'sentry/components/charts/styles';
  8. import TransitionChart from 'sentry/components/charts/transitionChart';
  9. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  10. import Panel from 'sentry/components/panels/panel';
  11. import QuestionTooltip from 'sentry/components/questionTooltip';
  12. import {IconWarning} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import type {DateString} from 'sentry/types/core';
  15. import type {Series} from 'sentry/types/echarts';
  16. import type {MetricsApiResponse} from 'sentry/types/metrics';
  17. import {browserHistory} from 'sentry/utils/browserHistory';
  18. import type {WebVital} from 'sentry/utils/fields';
  19. import getDynamicText from 'sentry/utils/getDynamicText';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useRouter from 'sentry/utils/useRouter';
  22. import {replaceSeriesName, transformEventStatsSmoothed} from '../trends/utils';
  23. import type {ViewProps} from '../types';
  24. import {getMaxOfSeries, getVitalChartDefinitions, getVitalChartTitle} from './utils';
  25. type Props = Omit<ViewProps, 'query' | 'start' | 'end'> & {
  26. end: DateString | null;
  27. errored: boolean;
  28. field: string;
  29. loading: boolean;
  30. reloading: boolean;
  31. response: MetricsApiResponse | null;
  32. start: DateString | null;
  33. vital: WebVital;
  34. };
  35. function VitalChartMetrics({
  36. reloading,
  37. loading,
  38. response,
  39. errored,
  40. statsPeriod,
  41. start,
  42. end,
  43. project,
  44. environment,
  45. field,
  46. vital,
  47. }: Props) {
  48. const location = useLocation();
  49. const router = useRouter();
  50. const theme = useTheme();
  51. const {utc, legend, vitalPoor, markLines, chartOptions} = getVitalChartDefinitions({
  52. theme,
  53. location,
  54. vital,
  55. yAxis: field,
  56. });
  57. function handleLegendSelectChanged(legendChange: {
  58. name: string;
  59. selected: Record<string, boolean>;
  60. type: string;
  61. }) {
  62. const {selected} = legendChange;
  63. const unselected = Object.keys(selected).filter(key => !selected[key]);
  64. const to = {
  65. ...location,
  66. query: {
  67. ...location.query,
  68. unselectedSeries: unselected,
  69. },
  70. };
  71. browserHistory.push(to);
  72. }
  73. return (
  74. <Panel>
  75. <ChartContainer>
  76. <HeaderTitleLegend>
  77. {getVitalChartTitle(vital)}
  78. <QuestionTooltip
  79. size="sm"
  80. position="top"
  81. title={t('The durations shown should fall under the vital threshold.')}
  82. />
  83. </HeaderTitleLegend>
  84. <ChartZoom router={router} period={statsPeriod} start={start} end={end} utc={utc}>
  85. {zoomRenderProps => {
  86. if (errored) {
  87. return (
  88. <ErrorPanel>
  89. <IconWarning color="gray500" size="lg" />
  90. </ErrorPanel>
  91. );
  92. }
  93. const data = response?.groups.map(group => ({
  94. seriesName: field,
  95. data: response.intervals.map((intervalValue, intervalIndex) => ({
  96. name: moment(intervalValue).valueOf(),
  97. value: group.series ? group.series[field][intervalIndex] : 0,
  98. })),
  99. })) as Series[] | undefined;
  100. const colors = (data && theme.charts.getColorPalette(data.length - 2)) || [];
  101. const {smoothedResults} = transformEventStatsSmoothed(data);
  102. const smoothedSeries = smoothedResults
  103. ? smoothedResults.map(({seriesName, ...rest}, i: number) => {
  104. return {
  105. seriesName: replaceSeriesName(seriesName) || 'p75',
  106. ...rest,
  107. color: colors[i],
  108. lineStyle: {
  109. opacity: 1,
  110. width: 2,
  111. },
  112. };
  113. })
  114. : [];
  115. const seriesMax = getMaxOfSeries(smoothedSeries);
  116. const yAxisMax = Math.max(seriesMax, vitalPoor);
  117. chartOptions.yAxis!.max = yAxisMax * 1.1;
  118. return (
  119. <ReleaseSeries
  120. start={start}
  121. end={end}
  122. period={statsPeriod}
  123. utc={utc}
  124. projects={project}
  125. environments={environment}
  126. >
  127. {({releaseSeries}) => (
  128. <TransitionChart loading={loading} reloading={reloading}>
  129. <TransparentLoadingMask visible={reloading} />
  130. {getDynamicText({
  131. value: (
  132. <LineChart
  133. {...zoomRenderProps}
  134. {...chartOptions}
  135. legend={legend}
  136. onLegendSelectChanged={handleLegendSelectChanged}
  137. series={[...markLines, ...releaseSeries, ...smoothedSeries]}
  138. />
  139. ),
  140. fixed: 'Web Vitals Chart',
  141. })}
  142. </TransitionChart>
  143. )}
  144. </ReleaseSeries>
  145. );
  146. }}
  147. </ChartZoom>
  148. </ChartContainer>
  149. </Panel>
  150. );
  151. }
  152. export default VitalChartMetrics;