metricAlert.tsx 3.2 KB

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