forked from uber/grafana-dash-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalert.ts
More file actions
62 lines (50 loc) · 1.4 KB
/
alert.ts
File metadata and controls
62 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { GrafanaAlert } from '../grafana';
import type Condition from './condition';
type AlertOptions = Partial<
Omit<GrafanaAlert, 'conditions'> & {
conditions: Condition[];
}
>;
class Alert {
private conditions: Condition[];
private state: GrafanaAlert;
constructor(opts: AlertOptions = {}) {
this.conditions = [];
this.state = {
name: 'Panel Title alert',
for: '15m',
frequency: '5m',
conditions: [],
message: '',
notifications: [],
executionErrorState: 'keep_state',
noDataState: 'keep_state',
alertRuleTags: {},
handler: 1,
};
this._init(opts);
this._initConditions(opts);
}
_init(opts: AlertOptions) {
Object.keys(opts).forEach((opt) => {
this.state[opt] = opts[opt];
});
}
_initConditions(opts: AlertOptions) {
this.state.conditions = this.state.conditions || [];
if (opts.conditions) {
this.conditions = this.conditions.concat(opts.conditions);
}
}
addCondition(condition: Condition) {
this.conditions.push(condition);
return this;
}
generate() {
this.state.conditions = this.conditions.map((condition) =>
condition.generate()
);
return this.state;
}
}
export = Alert;