File size: 6,691 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

-- +migrate Up

-- disable re-opening alerts
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION fn_prevent_reopen()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF OLD.status = 'closed' THEN
            RAISE EXCEPTION 'cannot change status of closed alert';
        END IF;
        RETURN NEW;
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE;
-- +migrate StatementEnd

CREATE TRIGGER trg_prevent_reopen
  BEFORE UPDATE OF status
  ON alerts
  FOR EACH ROW
  EXECUTE PROCEDURE fn_prevent_reopen();



-- collect EP snapshots when alerts are generated
CREATE TABLE alert_escalation_policy_snapshots (
    alert_id BIGINT NOT NULL REFERENCES alerts (id) ON DELETE CASCADE,
    step_number INT NOT NULL,
    step_max INT NOT NULL,
    step_delay INTERVAL NOT NULL,
    repeat INT NOT NULL,
    user_id UUID REFERENCES users (id) ON DELETE CASCADE,
    schedule_id UUID REFERENCES schedules (id) ON DELETE CASCADE
);

CREATE VIEW alert_escalation_policies AS
    WITH step_max AS (
        SELECT escalation_policy_id, count(step_number) as step_max
        FROM escalation_policy_steps
        GROUP BY escalation_policy_id
    )
    SELECT a.id as alert_id, step.step_number, m.step_max, (step.delay::TEXT||' minutes')::INTERVAL as step_delay, e.repeat, act.user_id, act.schedule_id
    FROM
        alerts a,
        escalation_policies e,
        escalation_policy_steps step,
        step_max m,
        escalation_policy_actions act,
        services svc
    WHERE a.service_id = svc.id
        AND e.id = svc.escalation_policy_id
        AND step.escalation_policy_id = m.escalation_policy_id
        AND step.escalation_policy_id = svc.escalation_policy_id
        AND act.escalation_policy_step_id = step.id;


INSERT INTO alert_escalation_policy_snapshots
    (alert_id, step_number, step_max, step_delay, repeat, user_id, schedule_id)
SELECT alert_id, step_number, step_max, step_delay, repeat, user_id, schedule_id
FROM alert_escalation_policies;

-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION fn_snapshot_escalation_policy()
  RETURNS trigger AS
$BODY$
    BEGIN
        INSERT INTO alert_escalation_policy_snapshots
            (alert_id, step_number, step_max, step_delay, repeat, user_id, schedule_id)
        SELECT alert_id, step_number, step_max, step_delay, repeat, user_id, schedule_id
        FROM alert_escalation_policies pol
        WHERE pol.alert_id = NEW.id;

        RETURN NEW;
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE;
-- +migrate StatementEnd


CREATE TRIGGER trg_snapshot_escalation_policy
    AFTER INSERT
    ON alerts
    FOR EACH ROW
    EXECUTE PROCEDURE fn_snapshot_escalation_policy();



-- Use snapshots when calculating notifications

-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION escalate_alerts() RETURNS VOID AS
    $$
        BEGIN
            UPDATE alerts
            SET escalation_level = escalation_level + 1, last_escalation = now()
            FROM alert_escalation_policy_snapshots e
            WHERE (last_escalation + e.step_delay) < now()
                AND status = 'triggered'
                AND id = e.alert_id
                AND e.step_number = (escalation_level % e.step_max)
                AND (e.repeat = -1 OR (escalation_level+1) / e.step_max <= e.repeat);
        END;
    $$ LANGUAGE 'plpgsql';
-- +migrate StatementEnd


CREATE OR REPLACE VIEW on_call_alert_users AS
    WITH alert_users AS (
        SELECT s.user_id,
            s.schedule_id,
            s.alert_id,
            a.status,
            a.escalation_level
        FROM
            alerts a,
            alert_escalation_policy_snapshots s
        WHERE s.alert_id = a.id
            AND s.step_number = (a.escalation_level % s.step_max)
            AND a.status <> 'closed'
    )
    SELECT DISTINCT au.alert_id,
        au.status,
        CASE
            WHEN au.user_id IS NULL THEN oc.user_id
            ELSE au.user_id
        END AS user_id,
        au.escalation_level
    FROM alert_users au
    LEFT JOIN on_call oc ON au.schedule_id = oc.schedule_id;

DROP VIEW alert_escalation_levels;

-- +migrate Down


CREATE VIEW alert_escalation_levels AS
    SELECT
        alerts.id AS alert_id,
        count(step.id) AS levels,
        alerts.escalation_level::bigint % count(step.id) AS relative_level
    FROM
        alerts,
        escalation_policy_steps step,
        services s
    WHERE step.escalation_policy_id = s.escalation_policy_id
        AND s.id = alerts.service_id
    GROUP BY alerts.id;

-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION escalate_alerts() RETURNS VOID AS
    $$
        BEGIN
            UPDATE alerts a
                SET escalation_level = escalation_level + 1, last_escalation = now()
                FROM services s, escalation_policy_steps step, alert_escalation_levels lvl, escalation_policies e
                WHERE (last_escalation + (step.delay::TEXT||' minutes')::interval) < now()
                    AND a.status = 'triggered'::enum_alert_status
                    AND s.id = a.service_id
                    AND step.escalation_policy_id = s.escalation_policy_id
                    AND lvl.alert_id = a.id
                    AND step.step_number = lvl.relative_level
                    AND e.id = s.escalation_policy_id
                    AND (e.repeat = -1 OR (escalation_level+1) / lvl.levels <= e.repeat);
        END;
    $$ LANGUAGE 'plpgsql';
-- +migrate StatementEnd


CREATE OR REPLACE VIEW on_call_alert_users AS
    WITH alert_users AS (
        SELECT act.user_id,
            act.schedule_id,
            a.id AS alert_id,
            a.status,
            a.escalation_level
        FROM alerts a,
            services s,
            alert_escalation_levels lvl,
            escalation_policy_steps step,
            escalation_policy_actions act
        WHERE s.id = a.service_id
            AND lvl.alert_id = a.id
            AND step.escalation_policy_id = s.escalation_policy_id
            AND step.step_number = lvl.relative_level
            AND a.status <> 'closed'::enum_alert_status
            AND act.escalation_policy_step_id = step.id
        GROUP BY act.user_id, act.schedule_id, a.id
    )
    SELECT DISTINCT au.alert_id,
        au.status,
        CASE
            WHEN au.user_id IS NULL THEN oc.user_id
            ELSE au.user_id
        END AS user_id,
        au.escalation_level
    FROM alert_users au
    LEFT JOIN on_call oc ON au.schedule_id = oc.schedule_id;

DROP TRIGGER trg_snapshot_escalation_policy ON alerts;
DROP TRIGGER trg_prevent_reopen ON alerts;
DROP FUNCTION fn_snapshot_escalation_policy();
DROP FUNCTION fn_prevent_reopen();
DROP TABLE alert_escalation_policy_snapshots;
DROP VIEW alert_escalation_policies;