chart.tsx 7.5 KB

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