chart.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. }: Props) {
  98. const location = useLocation();
  99. const router = useRouter();
  100. const theme = useTheme();
  101. const handleLegendSelectChanged = legendChange => {
  102. const {selected} = legendChange;
  103. const unselected = Object.keys(selected).filter(key => !selected[key]);
  104. const query = {
  105. ...location.query,
  106. };
  107. const queryKey = getUnselectedSeries(trendChangeType);
  108. query[queryKey] = unselected;
  109. const to = {
  110. ...location,
  111. query,
  112. };
  113. browserHistory.push(to);
  114. };
  115. const lineColor = trendToColor[trendChangeType || ''];
  116. const events =
  117. statsData && transaction?.project && transaction?.transaction
  118. ? statsData[[transaction.project, transaction.transaction].join(',')]
  119. : undefined;
  120. const data = events?.data ?? [];
  121. const trendFunction = getCurrentTrendFunction(location, trendFunctionField);
  122. const trendParameter = getCurrentTrendParameter(location, projects, project);
  123. const chartLabel = generateTrendFunctionAsString(
  124. trendFunction.field,
  125. trendParameter.column
  126. );
  127. const results = transformEventStats(data, chartLabel);
  128. const {smoothedResults, minValue, maxValue} = transformEventStatsSmoothed(
  129. results,
  130. chartLabel
  131. );
  132. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  133. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  134. const {utc} = normalizeDateTimeParams(location.query);
  135. const seriesSelection = decodeList(
  136. location.query[getUnselectedSeries(trendChangeType)]
  137. ).reduce((selection, metric) => {
  138. selection[metric] = false;
  139. return selection;
  140. }, {});
  141. const legend: LegendComponentOption = disableLegend
  142. ? {show: false}
  143. : {
  144. ...getLegend(chartLabel),
  145. selected: seriesSelection,
  146. };
  147. const loading = isLoading;
  148. const reloading = isLoading;
  149. const yMax = Math.max(
  150. maxValue,
  151. transaction?.aggregate_range_2 || 0,
  152. transaction?.aggregate_range_1 || 0
  153. );
  154. const yMin = Math.min(
  155. minValue,
  156. transaction?.aggregate_range_1 || Number.MAX_SAFE_INTEGER,
  157. transaction?.aggregate_range_2 || Number.MAX_SAFE_INTEGER
  158. );
  159. const smoothedSeries = smoothedResults
  160. ? smoothedResults.map(values => {
  161. return {
  162. ...values,
  163. color: lineColor.default,
  164. lineStyle: {
  165. opacity: 1,
  166. },
  167. };
  168. })
  169. : [];
  170. const needsLabel = true;
  171. const intervalSeries = getIntervalLine(
  172. theme,
  173. smoothedResults || [],
  174. 0.5,
  175. needsLabel,
  176. transaction
  177. );
  178. const yDiff = yMax - yMin;
  179. const yMargin = yDiff * 0.1;
  180. const series = [...smoothedSeries, ...intervalSeries];
  181. const durationUnit = getDurationUnit(series);
  182. const chartOptions: Omit<LineChartProps, 'series'> = {
  183. tooltip: {
  184. valueFormatter: (value, seriesName) => {
  185. return tooltipFormatter(value, aggregateOutputType(seriesName));
  186. },
  187. },
  188. yAxis: {
  189. min: Math.max(0, yMin - yMargin),
  190. max: yMax + yMargin,
  191. minInterval: durationUnit,
  192. axisLabel: {
  193. color: theme.chartLabel,
  194. formatter: (value: number) =>
  195. axisLabelFormatter(value, 'duration', undefined, durationUnit),
  196. },
  197. },
  198. };
  199. return (
  200. <ChartZoom
  201. router={router}
  202. period={statsPeriod}
  203. start={start}
  204. end={end}
  205. utc={utc === 'true'}
  206. >
  207. {zoomRenderProps => {
  208. return (
  209. <TransitionChart loading={loading} reloading={reloading}>
  210. <TransparentLoadingMask visible={reloading} />
  211. {getDynamicText({
  212. value: (
  213. <LineChart
  214. height={height}
  215. {...zoomRenderProps}
  216. {...chartOptions}
  217. onLegendSelectChanged={handleLegendSelectChanged}
  218. series={series}
  219. seriesOptions={{
  220. showSymbol: false,
  221. }}
  222. legend={legend}
  223. toolBox={{
  224. show: false,
  225. }}
  226. grid={
  227. grid ?? {
  228. left: '10px',
  229. right: '10px',
  230. top: '40px',
  231. bottom: '0px',
  232. }
  233. }
  234. xAxis={disableXAxis ? {show: false} : undefined}
  235. />
  236. ),
  237. fixed: 'Duration Chart',
  238. })}
  239. </TransitionChart>
  240. );
  241. }}
  242. </ChartZoom>
  243. );
  244. }
  245. export default Chart;