1import { config } from 'app/core/config';
2import { PanelModel } from 'app/features/dashboard/state';
3
4export const hiddenReducerTypes = ['percent_diff', 'percent_diff_abs'];
5export class ThresholdMapper {
6  static alertToGraphThresholds(panel: PanelModel) {
7    if (!panel.alert || config.unifiedAlertingEnabled) {
8      return false; // no update when no alerts
9    }
10
11    for (let i = 0; i < panel.alert.conditions.length; i++) {
12      const condition = panel.alert.conditions[i];
13      if (condition.type !== 'query') {
14        continue;
15      }
16
17      const evaluator = condition.evaluator;
18      const thresholds: any[] = (panel.thresholds = []);
19      const visible = hiddenReducerTypes.indexOf(condition.reducer?.type) === -1;
20
21      switch (evaluator.type) {
22        case 'gt': {
23          const value = evaluator.params[0];
24          thresholds.push({ value: value, op: 'gt', visible });
25          break;
26        }
27        case 'lt': {
28          const value = evaluator.params[0];
29          thresholds.push({ value: value, op: 'lt', visible });
30          break;
31        }
32        case 'outside_range': {
33          const value1 = evaluator.params[0];
34          const value2 = evaluator.params[1];
35
36          if (value1 > value2) {
37            thresholds.push({ value: value1, op: 'gt', visible });
38            thresholds.push({ value: value2, op: 'lt', visible });
39          } else {
40            thresholds.push({ value: value1, op: 'lt', visible });
41            thresholds.push({ value: value2, op: 'gt', visible });
42          }
43
44          break;
45        }
46        case 'within_range': {
47          const value1 = evaluator.params[0];
48          const value2 = evaluator.params[1];
49
50          if (value1 > value2) {
51            thresholds.push({ value: value1, op: 'lt', visible });
52            thresholds.push({ value: value2, op: 'gt', visible });
53          } else {
54            thresholds.push({ value: value1, op: 'gt', visible });
55            thresholds.push({ value: value2, op: 'lt', visible });
56          }
57          break;
58        }
59      }
60      break;
61    }
62
63    for (const t of panel.thresholds) {
64      t.fill = panel.options.alertThreshold;
65      t.line = panel.options.alertThreshold;
66      t.colorMode = 'critical';
67    }
68
69    const updated = true;
70    return updated;
71  }
72}
73