metricChartOption.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import color from 'color';
  2. import type {YAXisComponentOption} from 'echarts';
  3. import moment from 'moment-timezone';
  4. import type {AreaChartProps, AreaChartSeries} from 'sentry/components/charts/areaChart';
  5. import MarkArea from 'sentry/components/charts/components/markArea';
  6. import MarkLine from 'sentry/components/charts/components/markLine';
  7. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  8. import {t} from 'sentry/locale';
  9. import ConfigStore from 'sentry/stores/configStore';
  10. import {space} from 'sentry/styles/space';
  11. import type {Series} from 'sentry/types/echarts';
  12. import type {SessionApiResponse} from 'sentry/types/organization';
  13. import {formatMRIField} from 'sentry/utils/metrics/mri';
  14. import {getCrashFreeRateSeries} from 'sentry/utils/sessions';
  15. import {lightTheme as theme} from 'sentry/utils/theme';
  16. import type {MetricRule, Trigger} from 'sentry/views/alerts/rules/metric/types';
  17. import {AlertRuleTriggerType, Dataset} from 'sentry/views/alerts/rules/metric/types';
  18. import {getAnomalyMarkerSeries} from 'sentry/views/alerts/rules/metric/utils/anomalyChart';
  19. import type {Anomaly, Incident} from 'sentry/views/alerts/types';
  20. import {IncidentActivityType, IncidentStatus} from 'sentry/views/alerts/types';
  21. import {
  22. ALERT_CHART_MIN_MAX_BUFFER,
  23. alertAxisFormatter,
  24. alertTooltipValueFormatter,
  25. SESSION_AGGREGATE_TO_FIELD,
  26. shouldScaleAlertChart,
  27. } from 'sentry/views/alerts/utils';
  28. import {AlertWizardAlertNames} from 'sentry/views/alerts/wizard/options';
  29. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  30. import {isCrashFreeAlert} from '../utils/isCrashFreeAlert';
  31. function formatTooltipDate(date: moment.MomentInput, format: string): string {
  32. const {
  33. options: {timezone},
  34. } = ConfigStore.get('user');
  35. return moment.tz(date, timezone).format(format);
  36. }
  37. function createStatusAreaSeries(
  38. lineColor: string,
  39. startTime: number,
  40. endTime: number,
  41. yPosition: number
  42. ): AreaChartSeries {
  43. return {
  44. seriesName: '',
  45. type: 'line',
  46. markLine: MarkLine({
  47. silent: true,
  48. lineStyle: {color: lineColor, type: 'solid', width: 4},
  49. data: [[{coord: [startTime, yPosition]}, {coord: [endTime, yPosition]}]],
  50. }),
  51. data: [],
  52. };
  53. }
  54. function createThresholdSeries(lineColor: string, threshold: number): AreaChartSeries {
  55. return {
  56. seriesName: 'Threshold Line',
  57. type: 'line',
  58. markLine: MarkLine({
  59. silent: true,
  60. lineStyle: {color: lineColor, type: 'dashed', width: 1},
  61. data: [{yAxis: threshold}],
  62. label: {
  63. show: false,
  64. },
  65. }),
  66. data: [],
  67. };
  68. }
  69. function createIncidentSeries(
  70. incident: Incident,
  71. lineColor: string,
  72. incidentTimestamp: number,
  73. dataPoint?: AreaChartSeries['data'][0],
  74. seriesName?: string,
  75. aggregate?: string,
  76. handleIncidentClick?: (incident: Incident) => void
  77. ): AreaChartSeries {
  78. const formatter = ({value, marker}: any) => {
  79. const time = formatTooltipDate(moment(value), 'MMM D, YYYY LT');
  80. return [
  81. `<div class="tooltip-series"><div>`,
  82. `<span class="tooltip-label">${marker} <strong>${t('Alert')} #${
  83. incident.identifier
  84. }</strong></span>${
  85. dataPoint?.value
  86. ? `${seriesName} ${alertTooltipValueFormatter(
  87. dataPoint.value,
  88. seriesName ?? '',
  89. aggregate ?? ''
  90. )}`
  91. : ''
  92. }`,
  93. `</div></div>`,
  94. `<div class="tooltip-footer">${time}</div>`,
  95. '<div class="tooltip-arrow"></div>',
  96. ].join('');
  97. };
  98. return {
  99. seriesName: 'Incident Line',
  100. type: 'line',
  101. markLine: MarkLine({
  102. silent: false,
  103. lineStyle: {color: lineColor, type: 'solid'},
  104. data: [
  105. {
  106. xAxis: incidentTimestamp,
  107. // @ts-expect-error onClick not in echart types
  108. onClick: () => handleIncidentClick?.(incident),
  109. },
  110. ],
  111. label: {
  112. silent: true,
  113. show: !!incident.identifier,
  114. position: 'insideEndBottom',
  115. formatter: incident.identifier,
  116. color: lineColor,
  117. fontSize: 10,
  118. fontFamily: 'Rubik',
  119. },
  120. tooltip: {
  121. formatter,
  122. },
  123. }),
  124. data: [],
  125. tooltip: {
  126. trigger: 'item',
  127. alwaysShowContent: true,
  128. formatter,
  129. },
  130. };
  131. }
  132. export type MetricChartData = {
  133. rule: MetricRule;
  134. timeseriesData: Series[];
  135. anomalies?: Anomaly[];
  136. handleIncidentClick?: (incident: Incident) => void;
  137. incidents?: Incident[];
  138. selectedIncident?: Incident | null;
  139. seriesName?: string;
  140. showWaitingForData?: boolean;
  141. };
  142. type MetricChartOption = {
  143. chartOption: AreaChartProps;
  144. criticalDuration: number;
  145. totalDuration: number;
  146. waitingForDataDuration: number;
  147. warningDuration: number;
  148. };
  149. export function getMetricAlertChartOption({
  150. timeseriesData,
  151. rule,
  152. seriesName,
  153. incidents,
  154. selectedIncident,
  155. handleIncidentClick,
  156. showWaitingForData,
  157. anomalies,
  158. }: MetricChartData): MetricChartOption {
  159. let criticalTrigger: Trigger | undefined;
  160. let warningTrigger: Trigger | undefined;
  161. for (const trigger of rule.triggers) {
  162. if (trigger.label === AlertRuleTriggerType.CRITICAL) {
  163. criticalTrigger ??= trigger;
  164. }
  165. if (trigger.label === AlertRuleTriggerType.WARNING) {
  166. warningTrigger ??= trigger;
  167. }
  168. if (criticalTrigger && warningTrigger) {
  169. break;
  170. }
  171. }
  172. const series: AreaChartSeries[] = timeseriesData.map(s => ({
  173. ...s,
  174. seriesName: s.seriesName && formatMRIField(s.seriesName),
  175. }));
  176. const areaSeries: AreaChartSeries[] = [];
  177. // Ensure series data appears below incident/mark lines
  178. series[0].z = 1;
  179. series[0].color = CHART_PALETTE[0][0];
  180. const dataArr = timeseriesData[0].data;
  181. let maxSeriesValue = Number.NEGATIVE_INFINITY;
  182. let minSeriesValue = Number.POSITIVE_INFINITY;
  183. for (const coord of dataArr) {
  184. if (coord.value > maxSeriesValue) {
  185. maxSeriesValue = coord.value;
  186. }
  187. if (coord.value < minSeriesValue) {
  188. minSeriesValue = coord.value;
  189. }
  190. }
  191. // find the lowest value between chart data points, warning threshold,
  192. // critical threshold and then apply some breathing space
  193. const minChartValue = shouldScaleAlertChart(rule.aggregate)
  194. ? Math.floor(
  195. Math.min(
  196. minSeriesValue,
  197. typeof warningTrigger?.alertThreshold === 'number'
  198. ? warningTrigger.alertThreshold
  199. : Infinity,
  200. typeof criticalTrigger?.alertThreshold === 'number'
  201. ? criticalTrigger.alertThreshold
  202. : Infinity
  203. ) / ALERT_CHART_MIN_MAX_BUFFER
  204. )
  205. : 0;
  206. const startDate = new Date(dataArr[0]?.name);
  207. const endDate =
  208. dataArr.length > 1 ? new Date(dataArr[dataArr.length - 1]?.name) : new Date();
  209. const firstPoint = startDate.getTime();
  210. const lastPoint = endDate.getTime();
  211. const totalDuration = lastPoint - firstPoint;
  212. let waitingForDataDuration = 0;
  213. let criticalDuration = 0;
  214. let warningDuration = 0;
  215. series.push(
  216. createStatusAreaSeries(theme.green300, firstPoint, lastPoint, minChartValue)
  217. );
  218. if (showWaitingForData) {
  219. const {startIndex, endIndex} = getWaitingForDataRange(dataArr);
  220. const startTime = new Date(dataArr[startIndex]?.name).getTime();
  221. const endTime = new Date(dataArr[endIndex]?.name).getTime();
  222. waitingForDataDuration = Math.abs(endTime - startTime);
  223. series.push(createStatusAreaSeries(theme.gray200, startTime, endTime, minChartValue));
  224. }
  225. if (incidents) {
  226. incidents
  227. .filter(
  228. incident =>
  229. !incident.dateClosed || new Date(incident.dateClosed).getTime() > firstPoint
  230. )
  231. .forEach(incident => {
  232. const activities = incident.activities ?? [];
  233. const statusChanges = activities
  234. .filter(
  235. ({type, value}) =>
  236. type === IncidentActivityType.STATUS_CHANGE &&
  237. [IncidentStatus.WARNING, IncidentStatus.CRITICAL].includes(Number(value))
  238. )
  239. .sort(
  240. (a, b) =>
  241. new Date(a.dateCreated).getTime() - new Date(b.dateCreated).getTime()
  242. );
  243. const incidentEnd = incident.dateClosed ?? new Date().getTime();
  244. const timeWindowMs = rule.timeWindow * 60 * 1000;
  245. const incidentColor =
  246. warningTrigger &&
  247. !statusChanges.find(({value}) => Number(value) === IncidentStatus.CRITICAL)
  248. ? theme.yellow300
  249. : theme.red300;
  250. const incidentStartDate = new Date(incident.dateStarted).getTime();
  251. const incidentCloseDate = incident.dateClosed
  252. ? new Date(incident.dateClosed).getTime()
  253. : lastPoint;
  254. const incidentStartValue = dataArr.find(
  255. point => new Date(point.name).getTime() >= incidentStartDate
  256. );
  257. series.push(
  258. createIncidentSeries(
  259. incident,
  260. incidentColor,
  261. incidentStartDate,
  262. incidentStartValue,
  263. seriesName ?? series[0].seriesName,
  264. rule.aggregate,
  265. handleIncidentClick
  266. )
  267. );
  268. const areaStart = Math.max(new Date(incident.dateStarted).getTime(), firstPoint);
  269. const areaEnd = Math.min(
  270. statusChanges.length && statusChanges[0].dateCreated
  271. ? new Date(statusChanges[0].dateCreated).getTime() - timeWindowMs
  272. : new Date(incidentEnd).getTime(),
  273. lastPoint
  274. );
  275. const areaColor = warningTrigger ? theme.yellow300 : theme.red300;
  276. if (areaEnd > areaStart) {
  277. series.push(
  278. createStatusAreaSeries(areaColor, areaStart, areaEnd, minChartValue)
  279. );
  280. if (areaColor === theme.yellow300) {
  281. warningDuration += Math.abs(areaEnd - areaStart);
  282. } else {
  283. criticalDuration += Math.abs(areaEnd - areaStart);
  284. }
  285. }
  286. statusChanges.forEach((activity, idx) => {
  287. const statusAreaStart = Math.max(
  288. new Date(activity.dateCreated).getTime() - timeWindowMs,
  289. firstPoint
  290. );
  291. const statusAreaEnd = Math.min(
  292. idx === statusChanges.length - 1
  293. ? new Date(incidentEnd).getTime()
  294. : new Date(statusChanges[idx + 1].dateCreated).getTime() - timeWindowMs,
  295. lastPoint
  296. );
  297. const statusAreaColor =
  298. activity.value === `${IncidentStatus.CRITICAL}`
  299. ? theme.red300
  300. : theme.yellow300;
  301. if (statusAreaEnd > statusAreaStart) {
  302. series.push(
  303. createStatusAreaSeries(
  304. statusAreaColor,
  305. statusAreaStart,
  306. statusAreaEnd,
  307. minChartValue
  308. )
  309. );
  310. if (statusAreaColor === theme.yellow300) {
  311. warningDuration += Math.abs(statusAreaEnd - statusAreaStart);
  312. } else {
  313. criticalDuration += Math.abs(statusAreaEnd - statusAreaStart);
  314. }
  315. }
  316. });
  317. if (selectedIncident && incident.id === selectedIncident.id) {
  318. const selectedIncidentColor =
  319. incidentColor === theme.yellow300 ? theme.yellow100 : theme.red100;
  320. // Is areaSeries used anywhere?
  321. areaSeries.push({
  322. seriesName: '',
  323. type: 'line',
  324. markArea: MarkArea({
  325. silent: true,
  326. itemStyle: {
  327. color: color(selectedIncidentColor).alpha(0.42).rgb().string(),
  328. },
  329. data: [[{xAxis: incidentStartDate}, {xAxis: incidentCloseDate}]],
  330. }),
  331. data: [],
  332. });
  333. }
  334. });
  335. }
  336. if (anomalies) {
  337. series.push(...getAnomalyMarkerSeries(anomalies, {startDate, endDate}));
  338. }
  339. let maxThresholdValue = 0;
  340. if (!rule.comparisonDelta && warningTrigger?.alertThreshold) {
  341. const {alertThreshold} = warningTrigger;
  342. const warningThresholdLine = createThresholdSeries(theme.yellow300, alertThreshold);
  343. series.push(warningThresholdLine);
  344. maxThresholdValue = Math.max(maxThresholdValue, alertThreshold);
  345. }
  346. if (!rule.comparisonDelta && criticalTrigger?.alertThreshold) {
  347. const {alertThreshold} = criticalTrigger;
  348. const criticalThresholdLine = createThresholdSeries(theme.red300, alertThreshold);
  349. series.push(criticalThresholdLine);
  350. maxThresholdValue = Math.max(maxThresholdValue, alertThreshold);
  351. }
  352. if (!rule.comparisonDelta && rule.resolveThreshold) {
  353. const resolveThresholdLine = createThresholdSeries(
  354. theme.green300,
  355. rule.resolveThreshold
  356. );
  357. series.push(resolveThresholdLine);
  358. maxThresholdValue = Math.max(maxThresholdValue, rule.resolveThreshold);
  359. }
  360. const yAxis: YAXisComponentOption = {
  361. axisLabel: {
  362. formatter: (value: number) =>
  363. alertAxisFormatter(value, timeseriesData[0].seriesName, rule.aggregate),
  364. },
  365. max: isCrashFreeAlert(rule.dataset)
  366. ? 100
  367. : maxThresholdValue > maxSeriesValue
  368. ? maxThresholdValue
  369. : undefined,
  370. min: minChartValue || undefined,
  371. };
  372. return {
  373. criticalDuration,
  374. warningDuration,
  375. waitingForDataDuration,
  376. totalDuration,
  377. chartOption: {
  378. isGroupedByDate: true,
  379. yAxis,
  380. series,
  381. grid: {
  382. left: space(0.25),
  383. right: space(2),
  384. top: space(3),
  385. bottom: 0,
  386. },
  387. },
  388. };
  389. }
  390. function getWaitingForDataRange(dataArr) {
  391. if (dataArr[0].value > 0) {
  392. return {startIndex: 0, endIndex: 0};
  393. }
  394. for (let i = 0; i < dataArr.length; i++) {
  395. const dataPoint = dataArr[i];
  396. if (dataPoint.value > 0) {
  397. return {startIndex: 0, endIndex: i - 1};
  398. }
  399. }
  400. return {startIndex: 0, endIndex: dataArr.length - 1};
  401. }
  402. export function transformSessionResponseToSeries(
  403. response: SessionApiResponse | null,
  404. rule: MetricRule
  405. ): MetricChartData['timeseriesData'] {
  406. const {aggregate} = rule;
  407. return [
  408. {
  409. seriesName:
  410. AlertWizardAlertNames[
  411. getAlertTypeFromAggregateDataset({
  412. aggregate,
  413. dataset: Dataset.SESSIONS,
  414. })
  415. ],
  416. data: getCrashFreeRateSeries(
  417. response?.groups,
  418. response?.intervals,
  419. SESSION_AGGREGATE_TO_FIELD[aggregate]
  420. ),
  421. },
  422. ];
  423. }