vitalChartMetrics.tsx 5.5 KB

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