chart.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import {forwardRef, useCallback, useEffect, useMemo, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import * as echarts from 'echarts/core';
  5. import {CanvasRenderer} from 'echarts/renderers';
  6. import {updateDateTime} from 'sentry/actionCreators/pageFilters';
  7. import {transformToAreaSeries} from 'sentry/components/charts/areaChart';
  8. import {transformToBarSeries} from 'sentry/components/charts/barChart';
  9. import BaseChart, {BaseChartProps} from 'sentry/components/charts/baseChart';
  10. import {transformToLineSeries} from 'sentry/components/charts/lineChart';
  11. import ScatterSeries from 'sentry/components/charts/series/scatterSeries';
  12. import {DateTimeObject} from 'sentry/components/charts/utils';
  13. import {ReactEchartsRef} from 'sentry/types/echarts';
  14. import mergeRefs from 'sentry/utils/mergeRefs';
  15. import {
  16. formatMetricsUsingUnitAndOp,
  17. isCumulativeOp,
  18. MetricCorrelation,
  19. MetricDisplayType,
  20. } from 'sentry/utils/metrics';
  21. import useRouter from 'sentry/utils/useRouter';
  22. import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
  23. import {FocusArea, useFocusArea} from 'sentry/views/ddm/focusArea';
  24. import {getFormatter} from '../../components/charts/components/tooltip';
  25. import {useMetricSamples} from './useMetricSamples';
  26. import {Sample, ScatterSeries as ScatterSeriesType, Series} from './widget';
  27. type ChartProps = {
  28. displayType: MetricDisplayType;
  29. focusArea: FocusArea | null;
  30. series: Series[];
  31. widgetIndex: number;
  32. addFocusArea?: (area: FocusArea) => void;
  33. correlations?: MetricCorrelation[];
  34. drawFocusArea?: () => void;
  35. height?: number;
  36. highlightedSampleId?: string;
  37. onSampleClick?: (sample: Sample) => void;
  38. operation?: string;
  39. removeFocusArea?: () => void;
  40. };
  41. // We need to enable canvas renderer for echarts before we use it here.
  42. // Once we use it in more places, this should probably move to a more global place
  43. // But for now we keep it here to not invluence the bundle size of the main chunks.
  44. echarts.use(CanvasRenderer);
  45. export const MetricChart = forwardRef<ReactEchartsRef, ChartProps>(
  46. (
  47. {
  48. series,
  49. displayType,
  50. operation,
  51. widgetIndex,
  52. drawFocusArea,
  53. addFocusArea,
  54. focusArea,
  55. removeFocusArea,
  56. height,
  57. correlations,
  58. onSampleClick,
  59. highlightedSampleId,
  60. },
  61. forwardedRef
  62. ) => {
  63. const router = useRouter();
  64. const chartRef = useRef<ReactEchartsRef>(null);
  65. const handleZoom = useCallback(
  66. (range: DateTimeObject) => {
  67. Sentry.metrics.increment('ddm.enhance.zoom');
  68. updateDateTime(range, router, {save: true});
  69. },
  70. [router]
  71. );
  72. const focusAreaBrush = useFocusArea({
  73. chartRef,
  74. focusArea,
  75. opts: {
  76. widgetIndex,
  77. isDisabled: !addFocusArea || !removeFocusArea || !handleZoom,
  78. useFullYAxis: isCumulativeOp(operation),
  79. },
  80. onDraw: drawFocusArea,
  81. onAdd: addFocusArea,
  82. onRemove: removeFocusArea,
  83. onZoom: handleZoom,
  84. });
  85. useEffect(() => {
  86. const echartsInstance = chartRef?.current?.getEchartsInstance();
  87. if (echartsInstance && !echartsInstance.group) {
  88. echartsInstance.group = DDM_CHART_GROUP;
  89. }
  90. });
  91. const unit = series[0]?.unit;
  92. const seriesToShow = useMemo(
  93. () =>
  94. series
  95. .filter(s => !s.hidden)
  96. .map(s => ({
  97. ...s,
  98. silent: true,
  99. })),
  100. [series]
  101. );
  102. const valueFormatter = useCallback(
  103. (value: number) => {
  104. return formatMetricsUsingUnitAndOp(value, unit, operation);
  105. },
  106. [unit, operation]
  107. );
  108. const samples = useMetricSamples({
  109. chartRef,
  110. correlations,
  111. onClick: onSampleClick,
  112. highlightedSampleId,
  113. operation,
  114. timeseries: series,
  115. });
  116. // TODO(ddm): This assumes that all series have the same bucket size
  117. const bucketSize = seriesToShow[0]?.data[1]?.name - seriesToShow[0]?.data[0]?.name;
  118. const isSubMinuteBucket = bucketSize < 60_000;
  119. const seriesLength = seriesToShow[0]?.data.length;
  120. const displayFogOfWar = isCumulativeOp(operation);
  121. const chartProps = useMemo(() => {
  122. const timeseriesFormatters = {
  123. valueFormatter,
  124. isGroupedByDate: true,
  125. bucketSize,
  126. showTimeInTooltip: true,
  127. addSecondsToTimeFormat: isSubMinuteBucket,
  128. limit: 10,
  129. filter: (_, seriesParam) => {
  130. return seriesParam?.axisId === 'xAxis';
  131. },
  132. };
  133. const heightOptions = height ? {height} : {autoHeightResize: true};
  134. return {
  135. ...heightOptions,
  136. ...focusAreaBrush.options,
  137. forwardedRef: mergeRefs([forwardedRef, chartRef]),
  138. series: seriesToShow,
  139. renderer: seriesToShow.length > 20 ? ('canvas' as const) : ('svg' as const),
  140. isGroupedByDate: true,
  141. colors: seriesToShow.map(s => s.color),
  142. grid: {top: 5, bottom: 0, left: 0, right: 0},
  143. onClick: samples.handleClick,
  144. tooltip: {
  145. formatter: (params, asyncTicket) => {
  146. if (focusAreaBrush.isDrawingRef.current) {
  147. return '';
  148. }
  149. const hoveredEchartElement = Array.from(
  150. document.querySelectorAll(':hover')
  151. ).find(element => {
  152. return element.classList.contains('echarts-for-react');
  153. });
  154. if (params.seriesType === 'scatter') {
  155. return getFormatter(samples.formatters)(params, asyncTicket);
  156. }
  157. if (hoveredEchartElement === chartRef?.current?.ele) {
  158. return getFormatter(timeseriesFormatters)(params, asyncTicket);
  159. }
  160. return '';
  161. },
  162. },
  163. yAxes: [
  164. {
  165. // used to find and convert datapoint to pixel position
  166. id: 'yAxis',
  167. axisLabel: {
  168. formatter: (value: number) => {
  169. return valueFormatter(value);
  170. },
  171. },
  172. },
  173. samples.yAxis,
  174. ],
  175. xAxes: [
  176. {
  177. // used to find and convert datapoint to pixel position
  178. id: 'xAxis',
  179. axisPointer: {
  180. snap: true,
  181. },
  182. },
  183. samples.xAxis,
  184. ],
  185. };
  186. }, [
  187. bucketSize,
  188. focusAreaBrush.options,
  189. focusAreaBrush.isDrawingRef,
  190. forwardedRef,
  191. isSubMinuteBucket,
  192. seriesToShow,
  193. height,
  194. samples.handleClick,
  195. samples.xAxis,
  196. samples.yAxis,
  197. samples.formatters,
  198. valueFormatter,
  199. ]);
  200. return (
  201. <ChartWrapper>
  202. {focusAreaBrush.overlay}
  203. <CombinedChart
  204. {...chartProps}
  205. displayType={displayType}
  206. scatterSeries={samples.series}
  207. />
  208. {displayFogOfWar && (
  209. <FogOfWar bucketSize={bucketSize} seriesLength={seriesLength} />
  210. )}
  211. </ChartWrapper>
  212. );
  213. }
  214. );
  215. type CombinedChartProps = BaseChartProps & {
  216. displayType: MetricDisplayType;
  217. series: Series[];
  218. scatterSeries?: ScatterSeriesType[];
  219. };
  220. function CombinedChart({
  221. displayType,
  222. series,
  223. scatterSeries = [],
  224. ...chartProps
  225. }: CombinedChartProps) {
  226. const combinedSeries = useMemo(() => {
  227. if (displayType === MetricDisplayType.LINE) {
  228. return [
  229. ...transformToLineSeries({series}),
  230. ...transformToScatterSeries({series: scatterSeries, displayType}),
  231. ];
  232. }
  233. if (displayType === MetricDisplayType.BAR) {
  234. return [
  235. ...transformToBarSeries({series, stacked: true, animation: false}),
  236. ...transformToScatterSeries({series: scatterSeries, displayType}),
  237. ];
  238. }
  239. if (displayType === MetricDisplayType.AREA) {
  240. return [
  241. ...transformToAreaSeries({series, stacked: true, colors: chartProps.colors}),
  242. ...transformToScatterSeries({series: scatterSeries, displayType}),
  243. ];
  244. }
  245. return [];
  246. }, [displayType, scatterSeries, series, chartProps.colors]);
  247. return <BaseChart {...chartProps} series={combinedSeries} />;
  248. }
  249. function transformToScatterSeries({
  250. series,
  251. displayType,
  252. }: {
  253. displayType: MetricDisplayType;
  254. series: Series[];
  255. }) {
  256. return series.map(({seriesName, data: seriesData, ...options}) => {
  257. if (displayType === MetricDisplayType.BAR) {
  258. return ScatterSeries({
  259. ...options,
  260. name: seriesName,
  261. data: seriesData?.map(({value, name}) => ({value: [name, value]})),
  262. });
  263. }
  264. return ScatterSeries({
  265. ...options,
  266. name: seriesName,
  267. data: seriesData?.map(({value, name}) => [name, value]),
  268. animation: false,
  269. });
  270. });
  271. }
  272. function FogOfWar({
  273. bucketSize,
  274. seriesLength,
  275. }: {
  276. bucketSize?: number;
  277. seriesLength?: number;
  278. }) {
  279. if (!bucketSize || !seriesLength) {
  280. return null;
  281. }
  282. const widthFactor = getWidthFactor(bucketSize);
  283. const fogOfWarWidth = widthFactor * bucketSize + 30_000;
  284. const seriesWidth = bucketSize * seriesLength;
  285. // If either of these are undefiend, NaN or 0 the result will be invalid
  286. if (!fogOfWarWidth || !seriesWidth) {
  287. return null;
  288. }
  289. const width = (fogOfWarWidth / seriesWidth) * 100;
  290. return <FogOfWarOverlay width={width ?? 0} />;
  291. }
  292. function getWidthFactor(bucketSize: number) {
  293. // In general, fog of war should cover the last bucket
  294. if (bucketSize > 30 * 60_000) {
  295. return 1;
  296. }
  297. // for 10s timeframe we want to show a fog of war that spans last 10 buckets
  298. // because on average, we are missing last 90 seconds of data
  299. if (bucketSize <= 10_000) {
  300. return 10;
  301. }
  302. // For smaller time frames we want to show a wider fog of war
  303. return 2;
  304. }
  305. const ChartWrapper = styled('div')`
  306. position: relative;
  307. height: 100%;
  308. `;
  309. const FogOfWarOverlay = styled('div')<{width?: number}>`
  310. height: calc(100% - 29px);
  311. width: ${p => p.width}%;
  312. position: absolute;
  313. right: 0px;
  314. top: 5px;
  315. pointer-events: none;
  316. background: linear-gradient(
  317. 90deg,
  318. ${p => p.theme.background}00 0%,
  319. ${p => p.theme.background}FF 70%,
  320. ${p => p.theme.background}FF 100%
  321. );
  322. `;