chart.tsx 7.5 KB

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