chart.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {browserHistory} from 'react-router';
  2. import {useTheme} from '@emotion/react';
  3. import type {LegendComponentOption, LineSeriesOption} from 'echarts';
  4. import ChartZoom from 'sentry/components/charts/chartZoom';
  5. import {LineChart, LineChartProps} from 'sentry/components/charts/lineChart';
  6. import TransitionChart from 'sentry/components/charts/transitionChart';
  7. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  8. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  9. import {EventsStatsData, OrganizationSummary, Project} from 'sentry/types';
  10. import {Series} from 'sentry/types/echarts';
  11. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  12. import {
  13. axisLabelFormatter,
  14. getDurationUnit,
  15. tooltipFormatter,
  16. } from 'sentry/utils/discover/charts';
  17. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  18. import getDynamicText from 'sentry/utils/getDynamicText';
  19. import {decodeList} from 'sentry/utils/queryString';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useRouter from 'sentry/utils/useRouter';
  22. import {getIntervalLine} from 'sentry/views/performance/utils';
  23. import {ViewProps} from '../types';
  24. import {
  25. NormalizedTrendsTransaction,
  26. TrendChangeType,
  27. TrendFunctionField,
  28. TrendsStats,
  29. } from './types';
  30. import {
  31. generateTrendFunctionAsString,
  32. getCurrentTrendFunction,
  33. getCurrentTrendParameter,
  34. getUnselectedSeries,
  35. transformEventStatsSmoothed,
  36. trendToColor,
  37. } from './utils';
  38. type Props = ViewProps & {
  39. isLoading: boolean;
  40. organization: OrganizationSummary;
  41. projects: Project[];
  42. statsData: TrendsStats;
  43. trendChangeType: TrendChangeType;
  44. additionalSeries?: LineSeriesOption[];
  45. applyRegressionFormatToInterval?: boolean;
  46. disableLegend?: boolean;
  47. disableXAxis?: boolean;
  48. grid?: LineChartProps['grid'];
  49. height?: number;
  50. neutralColor?: boolean;
  51. transaction?: NormalizedTrendsTransaction;
  52. trendFunctionField?: TrendFunctionField;
  53. };
  54. export function transformEventStats(
  55. data: EventsStatsData,
  56. seriesName?: string
  57. ): Series[] {
  58. return [
  59. {
  60. seriesName: seriesName || 'Current',
  61. data: data.map(([timestamp, countsForTimestamp]) => ({
  62. name: timestamp * 1000,
  63. value: countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  64. })),
  65. },
  66. ];
  67. }
  68. function getLegend(trendFunction: string): LegendComponentOption {
  69. return {
  70. right: 10,
  71. top: 0,
  72. itemGap: 12,
  73. align: 'left',
  74. data: [
  75. {
  76. name: 'Baseline',
  77. icon: 'path://M180 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z, M810 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40zm, M1440 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z',
  78. },
  79. {
  80. name: 'Releases',
  81. },
  82. {
  83. name: trendFunction,
  84. },
  85. ],
  86. };
  87. }
  88. export function Chart({
  89. trendChangeType,
  90. statsPeriod,
  91. transaction,
  92. statsData,
  93. isLoading,
  94. start: propsStart,
  95. end: propsEnd,
  96. trendFunctionField,
  97. disableXAxis,
  98. disableLegend,
  99. neutralColor,
  100. grid,
  101. height,
  102. projects,
  103. project,
  104. organization,
  105. additionalSeries,
  106. applyRegressionFormatToInterval = false,
  107. }: Props) {
  108. const location = useLocation();
  109. const router = useRouter();
  110. const theme = useTheme();
  111. const handleLegendSelectChanged = legendChange => {
  112. const {selected} = legendChange;
  113. const unselected = Object.keys(selected).filter(key => !selected[key]);
  114. const query = {
  115. ...location.query,
  116. };
  117. const queryKey = getUnselectedSeries(trendChangeType);
  118. query[queryKey] = unselected;
  119. const to = {
  120. ...location,
  121. query,
  122. };
  123. browserHistory.push(to);
  124. };
  125. const derivedTrendChangeType = organization.features.includes('performance-new-trends')
  126. ? transaction?.change
  127. : trendChangeType;
  128. const lineColor =
  129. trendToColor[neutralColor ? 'neutral' : derivedTrendChangeType || trendChangeType];
  130. const events =
  131. statsData && transaction?.project && transaction?.transaction
  132. ? statsData[[transaction.project, transaction.transaction].join(',')]
  133. : undefined;
  134. const data = events?.data ?? [];
  135. const trendFunction = getCurrentTrendFunction(location, trendFunctionField);
  136. const trendParameter = getCurrentTrendParameter(location, projects, project);
  137. const chartLabel = generateTrendFunctionAsString(
  138. trendFunction.field,
  139. trendParameter.column
  140. );
  141. const results = transformEventStats(data, chartLabel);
  142. const {smoothedResults, minValue, maxValue} = transformEventStatsSmoothed(
  143. results,
  144. chartLabel
  145. );
  146. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  147. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  148. const {utc} = normalizeDateTimeParams(location.query);
  149. const seriesSelection = decodeList(
  150. location.query[getUnselectedSeries(trendChangeType)]
  151. ).reduce((selection, metric) => {
  152. selection[metric] = false;
  153. return selection;
  154. }, {});
  155. const legend: LegendComponentOption = disableLegend
  156. ? {show: false}
  157. : {
  158. ...getLegend(chartLabel),
  159. selected: seriesSelection,
  160. };
  161. const loading = isLoading;
  162. const reloading = isLoading;
  163. const yMax = Math.max(
  164. maxValue,
  165. transaction?.aggregate_range_2 || 0,
  166. transaction?.aggregate_range_1 || 0
  167. );
  168. const yMin = Math.min(
  169. minValue,
  170. transaction?.aggregate_range_1 || Number.MAX_SAFE_INTEGER,
  171. transaction?.aggregate_range_2 || Number.MAX_SAFE_INTEGER
  172. );
  173. const smoothedSeries = smoothedResults
  174. ? smoothedResults.map(values => {
  175. return {
  176. ...values,
  177. color: lineColor.default,
  178. lineStyle: {
  179. opacity: 1,
  180. },
  181. };
  182. })
  183. : [];
  184. const needsLabel = true;
  185. const intervalSeries = getIntervalLine(
  186. theme,
  187. smoothedResults || [],
  188. 0.5,
  189. needsLabel,
  190. transaction,
  191. applyRegressionFormatToInterval
  192. );
  193. const yDiff = yMax - yMin;
  194. const yMargin = yDiff * 0.1;
  195. const series = [...smoothedSeries, ...intervalSeries];
  196. const durationUnit = getDurationUnit(series);
  197. const chartOptions: Omit<LineChartProps, 'series'> = {
  198. tooltip: {
  199. valueFormatter: (value, seriesName) => {
  200. return tooltipFormatter(value, aggregateOutputType(seriesName));
  201. },
  202. },
  203. yAxis: {
  204. min: Math.max(0, yMin - yMargin),
  205. max: yMax + yMargin,
  206. minInterval: durationUnit,
  207. axisLabel: {
  208. color: theme.chartLabel,
  209. formatter: (value: number) =>
  210. axisLabelFormatter(value, 'duration', undefined, durationUnit),
  211. },
  212. },
  213. };
  214. return (
  215. <ChartZoom
  216. router={router}
  217. period={statsPeriod}
  218. start={start}
  219. end={end}
  220. utc={utc === 'true'}
  221. >
  222. {zoomRenderProps => {
  223. return (
  224. <TransitionChart loading={loading} reloading={reloading}>
  225. <TransparentLoadingMask visible={reloading} />
  226. {getDynamicText({
  227. value: (
  228. <LineChart
  229. height={height}
  230. {...zoomRenderProps}
  231. {...chartOptions}
  232. additionalSeries={additionalSeries}
  233. onLegendSelectChanged={handleLegendSelectChanged}
  234. series={series}
  235. seriesOptions={{
  236. showSymbol: false,
  237. }}
  238. legend={legend}
  239. toolBox={{
  240. show: false,
  241. }}
  242. grid={
  243. grid ?? {
  244. left: '10px',
  245. right: '10px',
  246. top: '40px',
  247. bottom: '0px',
  248. }
  249. }
  250. xAxis={disableXAxis ? {show: false} : undefined}
  251. />
  252. ),
  253. fixed: 'Duration Chart',
  254. })}
  255. </TransitionChart>
  256. );
  257. }}
  258. </ChartZoom>
  259. );
  260. }
  261. export default Chart;