chart.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import {forwardRef, useEffect, useMemo, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import Color from 'color';
  4. import * as echarts from 'echarts/core';
  5. import {CanvasRenderer} from 'echarts/renderers';
  6. import isNil from 'lodash/isNil';
  7. import omitBy from 'lodash/omitBy';
  8. import {transformToAreaSeries} from 'sentry/components/charts/areaChart';
  9. import {transformToBarSeries} from 'sentry/components/charts/barChart';
  10. import BaseChart from 'sentry/components/charts/baseChart';
  11. import {
  12. defaultFormatAxisLabel,
  13. getFormatter,
  14. } from 'sentry/components/charts/components/tooltip';
  15. import {transformToLineSeries} from 'sentry/components/charts/lineChart';
  16. import ScatterSeries from 'sentry/components/charts/series/scatterSeries';
  17. import {isChartHovered} from 'sentry/components/charts/utils';
  18. import {t} from 'sentry/locale';
  19. import type {ReactEchartsRef} from 'sentry/types/echarts';
  20. import mergeRefs from 'sentry/utils/mergeRefs';
  21. import {formatMetricUsingUnit} from 'sentry/utils/metrics/formatters';
  22. import {MetricDisplayType} from 'sentry/utils/metrics/types';
  23. import usePageFilters from 'sentry/utils/usePageFilters';
  24. import type {CombinedMetricChartProps, Series} from 'sentry/views/ddm/chart/types';
  25. import type {UseFocusAreaResult} from 'sentry/views/ddm/chart/useFocusArea';
  26. import type {UseMetricSamplesResult} from 'sentry/views/ddm/chart/useMetricChartSamples';
  27. const MAIN_X_AXIS_ID = 'xAxis';
  28. type ChartProps = {
  29. displayType: MetricDisplayType;
  30. series: Series[];
  31. focusArea?: UseFocusAreaResult;
  32. group?: string;
  33. height?: number;
  34. samples?: UseMetricSamplesResult;
  35. };
  36. // We need to enable canvas renderer for echarts before we use it here.
  37. // Once we use it in more places, this should probably move to a more global place
  38. // But for now we keep it here to not invluence the bundle size of the main chunks.
  39. echarts.use(CanvasRenderer);
  40. function isNonZeroValue(value: number | null) {
  41. return value !== null && value !== 0;
  42. }
  43. function addSeriesPadding(data: Series['data']) {
  44. const hasNonZeroSibling = (index: number) => {
  45. return (
  46. isNonZeroValue(data[index - 1]?.value) || isNonZeroValue(data[index + 1]?.value)
  47. );
  48. };
  49. const paddingIndices = new Set<number>();
  50. return {
  51. data: data.map(({name, value}, index) => {
  52. const shouldAddPadding = value === null && hasNonZeroSibling(index);
  53. if (shouldAddPadding) {
  54. paddingIndices.add(index);
  55. }
  56. return {
  57. name,
  58. value: shouldAddPadding ? 0 : value,
  59. };
  60. }),
  61. paddingIndices,
  62. };
  63. }
  64. export const MetricChart = forwardRef<ReactEchartsRef, ChartProps>(
  65. ({series, displayType, height, group, samples, focusArea}, forwardedRef) => {
  66. const chartRef = useRef<ReactEchartsRef>(null);
  67. const filteredSeries = useMemo(() => series.filter(s => !s.hidden), [series]);
  68. const firstUnit = filteredSeries[0]?.unit || 'none';
  69. const uniqueUnits = useMemo(
  70. () => [...new Set(filteredSeries.map(s => s.unit || 'none'))],
  71. [filteredSeries]
  72. );
  73. useEffect(() => {
  74. if (!group) {
  75. return;
  76. }
  77. const echartsInstance = chartRef?.current?.getEchartsInstance();
  78. if (echartsInstance && !echartsInstance.group) {
  79. echartsInstance.group = group;
  80. }
  81. });
  82. // TODO(ddm): This assumes that all series have the same bucket size
  83. const bucketSize = series[0]?.data[1]?.name - series[0]?.data[0]?.name;
  84. const isSubMinuteBucket = bucketSize < 60_000;
  85. const lastBucketTimestamp = series[0]?.data?.[series[0]?.data?.length - 1]?.name;
  86. const ingestionBuckets = useMemo(
  87. () => getIngestionDelayBucketCount(bucketSize, lastBucketTimestamp),
  88. [bucketSize, lastBucketTimestamp]
  89. );
  90. const seriesToShow = useMemo(
  91. () =>
  92. filteredSeries
  93. .map(s => {
  94. const mappedSeries = {
  95. ...s,
  96. silent: true,
  97. yAxisIndex: uniqueUnits.indexOf(s.unit),
  98. xAxisIndex: 0,
  99. ...(displayType !== MetricDisplayType.BAR
  100. ? addSeriesPadding(s.data)
  101. : {data: s.data}),
  102. };
  103. if (displayType === MetricDisplayType.BAR) {
  104. mappedSeries.stack = s.unit;
  105. }
  106. return mappedSeries;
  107. })
  108. // Split series in two parts, one for the main chart and one for the fog of war
  109. // The order is important as the tooltip will show the first series first (for overlaps)
  110. .flatMap(s => createIngestionSeries(s, ingestionBuckets, displayType)),
  111. [filteredSeries, uniqueUnits, displayType, ingestionBuckets]
  112. );
  113. const {selection} = usePageFilters();
  114. const dateTimeOptions = useMemo(() => {
  115. return omitBy(selection.datetime, isNil);
  116. }, [selection.datetime]);
  117. const chartProps = useMemo(() => {
  118. const seriesUnits = seriesToShow.reduce(
  119. (acc, s) => {
  120. acc[s.seriesName] = s.unit;
  121. return acc;
  122. },
  123. {} as Record<string, string>
  124. );
  125. const timeseriesFormatters = {
  126. valueFormatter: (value: number, seriesName?: string) => {
  127. const unit = (seriesName && seriesUnits[seriesName]) ?? 'none';
  128. return formatMetricUsingUnit(value, unit);
  129. },
  130. isGroupedByDate: true,
  131. bucketSize,
  132. showTimeInTooltip: true,
  133. addSecondsToTimeFormat: isSubMinuteBucket,
  134. limit: 10,
  135. filter: (_, seriesParam) => {
  136. return seriesParam?.axisId === MAIN_X_AXIS_ID;
  137. },
  138. };
  139. const heightOptions = height ? {height} : {autoHeightResize: true};
  140. let baseChartProps: CombinedMetricChartProps = {
  141. ...heightOptions,
  142. ...dateTimeOptions,
  143. displayType,
  144. forwardedRef: mergeRefs([forwardedRef, chartRef]),
  145. series: seriesToShow,
  146. devicePixelRatio: 2,
  147. renderer: 'canvas' as const,
  148. isGroupedByDate: true,
  149. colors: seriesToShow.map(s => s.color),
  150. grid: {
  151. top: 5,
  152. bottom: 0,
  153. left: 0,
  154. right: 0,
  155. },
  156. tooltip: {
  157. formatter: (params, asyncTicket) => {
  158. // Only show the tooltip if the current chart is hovered
  159. // as chart groups trigger the tooltip for all charts in the group when one is hoverered
  160. if (!isChartHovered(chartRef?.current)) {
  161. return '';
  162. }
  163. // The mechanism by which we display ingestion delay the chart, duplicates the series in the chart data
  164. // so we need to de-duplicate the series before showing the tooltip
  165. // this assumes that the first series is the main series and the second is the ingestion delay series
  166. if (Array.isArray(params)) {
  167. const uniqueSeries = new Set<string>();
  168. const deDupedParams = params.filter(param => {
  169. // Filter null values from tooltip
  170. if (param.value[1] === null) {
  171. return false;
  172. }
  173. // scatter series (samples) have their own tooltip
  174. if (param.seriesType === 'scatter') {
  175. return false;
  176. }
  177. // Filter padding datapoints from tooltip
  178. if (param.value[1] === 0) {
  179. const currentSeries = seriesToShow[param.seriesIndex];
  180. const paddingIndices =
  181. 'paddingIndices' in currentSeries
  182. ? currentSeries.paddingIndices
  183. : undefined;
  184. if (paddingIndices?.has(param.dataIndex)) {
  185. return false;
  186. }
  187. }
  188. if (uniqueSeries.has(param.seriesName)) {
  189. return false;
  190. }
  191. uniqueSeries.add(param.seriesName);
  192. return true;
  193. });
  194. const date = defaultFormatAxisLabel(
  195. params[0].value[0] as number,
  196. timeseriesFormatters.isGroupedByDate,
  197. false,
  198. timeseriesFormatters.showTimeInTooltip,
  199. timeseriesFormatters.addSecondsToTimeFormat,
  200. timeseriesFormatters.bucketSize
  201. );
  202. if (deDupedParams.length === 0) {
  203. return [
  204. '<div class="tooltip-series">',
  205. `<center>${t('No data available')}</center>`,
  206. '</div>',
  207. `<div class="tooltip-footer">${date}</div>`,
  208. ].join('');
  209. }
  210. return getFormatter(timeseriesFormatters)(deDupedParams, asyncTicket);
  211. }
  212. return getFormatter(timeseriesFormatters)(params, asyncTicket);
  213. },
  214. },
  215. yAxes:
  216. uniqueUnits.length === 0
  217. ? // fallback axis for when there are no series as echarts requires at least one axis
  218. [
  219. {
  220. id: 'none',
  221. axisLabel: {
  222. formatter: (value: number) => {
  223. return formatMetricUsingUnit(value, 'none');
  224. },
  225. },
  226. },
  227. ]
  228. : [
  229. ...uniqueUnits.map((unit, index) =>
  230. unit === firstUnit
  231. ? {
  232. id: unit,
  233. axisLabel: {
  234. formatter: (value: number) => {
  235. return formatMetricUsingUnit(value, unit);
  236. },
  237. },
  238. }
  239. : {
  240. id: unit,
  241. show: index === 1,
  242. axisLabel: {
  243. show: index === 1,
  244. formatter: (value: number) => {
  245. return formatMetricUsingUnit(value, unit);
  246. },
  247. },
  248. splitLine: {
  249. show: false,
  250. },
  251. position: 'right' as const,
  252. axisPointer: {
  253. type: 'none' as const,
  254. },
  255. }
  256. ),
  257. ],
  258. xAxes: [
  259. {
  260. id: MAIN_X_AXIS_ID,
  261. axisPointer: {
  262. snap: true,
  263. },
  264. },
  265. ],
  266. };
  267. if (samples?.applyChartProps) {
  268. baseChartProps = samples.applyChartProps(baseChartProps);
  269. }
  270. // Apply focus area props as last so it can disable tooltips
  271. if (focusArea?.applyChartProps) {
  272. baseChartProps = focusArea.applyChartProps(baseChartProps);
  273. }
  274. return baseChartProps;
  275. }, [
  276. seriesToShow,
  277. dateTimeOptions,
  278. bucketSize,
  279. isSubMinuteBucket,
  280. height,
  281. displayType,
  282. forwardedRef,
  283. uniqueUnits,
  284. samples,
  285. focusArea,
  286. firstUnit,
  287. ]);
  288. return (
  289. <ChartWrapper>
  290. {focusArea?.overlay}
  291. <CombinedChart {...chartProps} />
  292. </ChartWrapper>
  293. );
  294. }
  295. );
  296. function CombinedChart({
  297. displayType,
  298. series,
  299. scatterSeries = [],
  300. ...chartProps
  301. }: CombinedMetricChartProps) {
  302. const combinedSeries = useMemo(() => {
  303. if (displayType === MetricDisplayType.LINE) {
  304. return [
  305. ...transformToLineSeries({series}),
  306. ...transformToScatterSeries({series: scatterSeries, displayType}),
  307. ];
  308. }
  309. if (displayType === MetricDisplayType.BAR) {
  310. return [
  311. ...transformToBarSeries({series, stacked: true, animation: false}),
  312. ...transformToScatterSeries({series: scatterSeries, displayType}),
  313. ];
  314. }
  315. if (displayType === MetricDisplayType.AREA) {
  316. return [
  317. ...transformToAreaSeries({series, stacked: true, colors: chartProps.colors}),
  318. ...transformToScatterSeries({series: scatterSeries, displayType}),
  319. ];
  320. }
  321. return [];
  322. }, [displayType, scatterSeries, series, chartProps.colors]);
  323. return <BaseChart {...chartProps} series={combinedSeries} />;
  324. }
  325. function transformToScatterSeries({
  326. series,
  327. displayType,
  328. }: {
  329. displayType: MetricDisplayType;
  330. series: Series[];
  331. }) {
  332. return series.map(({seriesName, data: seriesData, ...options}) => {
  333. if (displayType === MetricDisplayType.BAR) {
  334. return ScatterSeries({
  335. ...options,
  336. name: seriesName,
  337. data: seriesData?.map(({value, name}) => ({value: [name, value]})),
  338. });
  339. }
  340. return ScatterSeries({
  341. ...options,
  342. name: seriesName,
  343. data: seriesData?.map(({value, name}) => [name, value]),
  344. animation: false,
  345. });
  346. });
  347. }
  348. function createIngestionSeries(
  349. orignalSeries: Series,
  350. ingestionBuckets: number,
  351. displayType: MetricDisplayType
  352. ) {
  353. if (ingestionBuckets < 1) {
  354. return [orignalSeries];
  355. }
  356. const series = [
  357. {
  358. ...orignalSeries,
  359. data: orignalSeries.data.slice(0, -ingestionBuckets),
  360. },
  361. ];
  362. if (displayType === MetricDisplayType.BAR) {
  363. series.push(createIngestionBarSeries(orignalSeries, ingestionBuckets));
  364. } else if (displayType === MetricDisplayType.AREA) {
  365. series.push(createIngestionAreaSeries(orignalSeries, ingestionBuckets));
  366. } else {
  367. series.push(createIngestionLineSeries(orignalSeries, ingestionBuckets));
  368. }
  369. return series;
  370. }
  371. const EXTRAPOLATED_AREA_STRIPE_IMG =
  372. 'image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAABkCAYAAAC/zKGXAAAAMUlEQVR4Ae3KoREAIAwEsMKgrMeYj8BzyIpEZyTZda16mPVJFEVRFEVRFEVRFMWO8QB4uATKpuU51gAAAABJRU5ErkJggg==';
  373. export const getIngestionSeriesId = (seriesId: string) => `${seriesId}-ingestion`;
  374. function createIngestionBarSeries(series: Series, fogBucketCnt = 0) {
  375. return {
  376. ...series,
  377. id: getIngestionSeriesId(series.id),
  378. silent: true,
  379. data: series.data.map((data, index) => ({
  380. ...data,
  381. // W need to set a value for the non-fog of war buckets so that the stacking still works in echarts
  382. value: index < series.data.length - fogBucketCnt ? 0 : data.value,
  383. })),
  384. itemStyle: {
  385. opacity: 1,
  386. decal: {
  387. symbol: EXTRAPOLATED_AREA_STRIPE_IMG,
  388. dashArrayX: [6, 0],
  389. dashArrayY: [6, 0],
  390. rotation: Math.PI / 4,
  391. },
  392. },
  393. };
  394. }
  395. function createIngestionLineSeries(series: Series, fogBucketCnt = 0) {
  396. return {
  397. ...series,
  398. id: getIngestionSeriesId(series.id),
  399. silent: true,
  400. // We include the last non-fog of war bucket so that the line is connected
  401. data: series.data.slice(-fogBucketCnt - 1),
  402. lineStyle: {
  403. type: 'dotted',
  404. },
  405. };
  406. }
  407. function createIngestionAreaSeries(series: Series, fogBucketCnt = 0) {
  408. return {
  409. ...series,
  410. id: getIngestionSeriesId(series.id),
  411. silent: true,
  412. stack: 'fogOfWar',
  413. // We include the last non-fog of war bucket so that the line is connected
  414. data: series.data.slice(-fogBucketCnt - 1),
  415. lineStyle: {
  416. type: 'dotted',
  417. color: Color(series.color).lighten(0.3).string(),
  418. },
  419. };
  420. }
  421. const AVERAGE_INGESTION_DELAY_MS = 90_000;
  422. /**
  423. * Calculates the number of buckets, affected by ingestion delay.
  424. * Based on the AVERAGE_INGESTION_DELAY_MS
  425. * @param bucketSize in ms
  426. * @param lastBucketTimestamp starting time of the last bucket in ms
  427. */
  428. function getIngestionDelayBucketCount(bucketSize: number, lastBucketTimestamp: number) {
  429. const timeSinceLastBucket = Date.now() - (lastBucketTimestamp + bucketSize);
  430. const ingestionAffectedTime = Math.max(
  431. 0,
  432. AVERAGE_INGESTION_DELAY_MS - timeSinceLastBucket
  433. );
  434. return Math.ceil(ingestionAffectedTime / bucketSize);
  435. }
  436. const ChartWrapper = styled('div')`
  437. position: relative;
  438. height: 100%;
  439. `;