vitalChartMetrics.tsx 5.3 KB

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