metricAlert.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import type {LineSeriesOption, YAXisComponentOption} from 'echarts';
  2. import type {AreaChartSeries} from 'sentry/components/charts/areaChart';
  3. import XAxis from 'sentry/components/charts/components/xAxis';
  4. import AreaSeries from 'sentry/components/charts/series/areaSeries';
  5. import type {SessionApiResponse} from 'sentry/types';
  6. import {lightTheme as theme} from 'sentry/utils/theme';
  7. import type {MetricChartData} from 'sentry/views/alerts/rules/metric/details/metricChartOption';
  8. import {
  9. getMetricAlertChartOption,
  10. transformSessionResponseToSeries,
  11. } from 'sentry/views/alerts/rules/metric/details/metricChartOption';
  12. import {DEFAULT_FONT_FAMILY, slackChartDefaults, slackChartSize} from './slack';
  13. import type {RenderDescriptor} from './types';
  14. import {ChartType} from './types';
  15. const metricAlertXaxis = XAxis({
  16. theme,
  17. splitNumber: 3,
  18. isGroupedByDate: true,
  19. axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY},
  20. });
  21. const metricAlertYaxis: YAXisComponentOption = {
  22. axisLabel: {fontSize: 11, fontFamily: DEFAULT_FONT_FAMILY},
  23. splitLine: {
  24. lineStyle: {
  25. color: theme.chartLineColor,
  26. opacity: 0.3,
  27. },
  28. },
  29. };
  30. function transformAreaSeries(series: AreaChartSeries[]): LineSeriesOption[] {
  31. return series.map(({seriesName, data, ...otherSeriesProps}) => {
  32. const areaSeries = AreaSeries({
  33. name: seriesName,
  34. data: data.map(({name, value}) => [name, value]),
  35. lineStyle: {
  36. opacity: 1,
  37. width: 0.4,
  38. },
  39. areaStyle: {
  40. opacity: 1.0,
  41. },
  42. animation: false,
  43. animationThreshold: 1,
  44. animationDuration: 0,
  45. ...otherSeriesProps,
  46. });
  47. // Fix incident label font family, cannot use Rubik
  48. if (areaSeries.markLine?.label) {
  49. areaSeries.markLine.label.fontFamily = DEFAULT_FONT_FAMILY;
  50. }
  51. return areaSeries;
  52. });
  53. }
  54. export const metricAlertCharts: RenderDescriptor<ChartType>[] = [];
  55. metricAlertCharts.push({
  56. key: ChartType.SLACK_METRIC_ALERT_EVENTS,
  57. getOption: (data: MetricChartData) => {
  58. const {chartOption} = getMetricAlertChartOption(data);
  59. return {
  60. ...chartOption,
  61. backgroundColor: theme.background,
  62. series: transformAreaSeries(chartOption.series),
  63. xAxis: metricAlertXaxis,
  64. yAxis: {
  65. ...chartOption.yAxis,
  66. ...metricAlertYaxis,
  67. axisLabel: {
  68. ...chartOption.yAxis!.axisLabel,
  69. ...metricAlertYaxis.axisLabel,
  70. },
  71. },
  72. grid: slackChartDefaults.grid,
  73. };
  74. },
  75. ...slackChartSize,
  76. });
  77. interface MetricAlertSessionData extends Omit<MetricChartData, 'timeseriesData'> {
  78. sessionResponse: SessionApiResponse;
  79. }
  80. metricAlertCharts.push({
  81. key: ChartType.SLACK_METRIC_ALERT_SESSIONS,
  82. getOption: (data: MetricAlertSessionData) => {
  83. const {sessionResponse, rule, ...rest} = data;
  84. const {chartOption} = getMetricAlertChartOption({
  85. ...rest,
  86. rule,
  87. timeseriesData: transformSessionResponseToSeries(sessionResponse, rule),
  88. });
  89. return {
  90. ...chartOption,
  91. backgroundColor: theme.background,
  92. series: transformAreaSeries(chartOption.series),
  93. xAxis: metricAlertXaxis,
  94. yAxis: {
  95. ...chartOption.yAxis,
  96. ...metricAlertYaxis,
  97. axisLabel: {
  98. ...chartOption.yAxis!.axisLabel,
  99. ...metricAlertYaxis.axisLabel,
  100. },
  101. },
  102. grid: slackChartDefaults.grid,
  103. };
  104. },
  105. ...slackChartSize,
  106. });