/
resolver.go
402 lines (367 loc) · 10.2 KB
/
resolver.go
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package resolver
import (
"context"
"database/sql"
"github.com/pkg/errors"
"github.com/target/goalert/permission"
"github.com/target/goalert/schedule"
"github.com/target/goalert/schedule/rule"
"github.com/target/goalert/util"
"github.com/target/goalert/validation/validate"
)
type Resolver interface {
AlertEPID(context.Context, int) (string, error)
IsUserOnCall(ctx context.Context, userID string) (bool, error)
OnCallByUser(ctx context.Context, userID string) ([]OnCallAssignment, error)
}
type OnCallAssignment struct {
ServiceID string `json:"service_id"`
ServiceName string `json:"service_name"`
EPID string `json:"escalation_policy_id"`
EPName string `json:"escalation_policy_name"`
Level int `json:"escalation_policy_step_number"`
RotationID string `json:"rotation_id"`
RotationName string `json:"rotation_name"`
ScheduleID string `json:"schedule_id"`
ScheduleName string `json:"schedule_name"`
UserID string `json:"user_id"`
IsActive bool `json:"is_active"`
}
type DB struct {
db *sql.DB
epID *sql.Stmt
isOnCall *sql.Stmt
onCallEPDirect *sql.Stmt
onCallEPRot *sql.Stmt
onCallRemoveOverrides *sql.Stmt
onCallAddOverrideAssignments *sql.Stmt
onCallReplaceOverrideAssignments *sql.Stmt
onCallDirectAssignments *sql.Stmt
rules rule.Store
sched *schedule.Store
}
func NewDB(ctx context.Context, db *sql.DB, rules rule.Store, sched *schedule.Store) (*DB, error) {
p := &util.Prepare{DB: db, Ctx: ctx}
return &DB{
db: db,
rules: rules,
sched: sched,
isOnCall: p.P(`
select 1
from services svc
join escalation_policy_steps step on step.escalation_policy_id = svc.escalation_policy_id
join escalation_policy_actions act on act.escalation_policy_step_id = step.id
left join schedule_on_call_users sUser on
sUser.schedule_id = act.schedule_id and
sUser.user_id = $1 and
sUser.end_time isnull
left join rotation_state rState on rState.rotation_id = act.rotation_id
left join rotation_participants part on
part.id = rState.rotation_participant_id and
part.user_id = $1
where coalesce(act.user_id, sUser.user_id, part.user_id) = $1
limit 1
`),
onCallEPDirect: p.P(`
select distinct
svc.id,
svc.name,
ep.id,
ep.name,
step.step_number,
null,
null,
null,
null,
true
from services svc
join escalation_policies ep on ep.id = svc.escalation_policy_id
join escalation_policy_steps step on step.escalation_policy_id = svc.escalation_policy_id
join escalation_policy_actions act on
act.escalation_policy_step_id = step.id and
act.user_id = $1
`),
onCallEPRot: p.P(`
select distinct
svc.id,
svc.name,
ep.id,
ep.name,
step.step_number,
null,
null,
act.rotation_id,
rot.name,
part.id = state.rotation_participant_id
from services svc
join escalation_policies ep on ep.id = svc.escalation_policy_id
join escalation_policy_steps step on step.escalation_policy_id = svc.escalation_policy_id
join escalation_policy_actions act on
act.escalation_policy_step_id = step.id and
act.rotation_id notnull
join rotations rot on rot.id = act.rotation_id
join rotation_participants part on
part.user_id = $1 and
part.rotation_id = act.rotation_id
join rotation_state state on state.rotation_id = act.rotation_id
`),
onCallRemoveOverrides: p.P(`
select
tgt_schedule_id
from user_overrides
where
now() between start_time and end_time and
remove_user_id = $1
`),
onCallAddOverrideAssignments: p.P(`
select
svc.id,
svc.name,
ep.id,
ep.name,
step.step_number,
sched.id,
sched.name,
null,
null,
o.start_time <= now()
from user_overrides o
join schedules sched on sched.id = o.tgt_schedule_id
join escalation_policy_actions act on act.schedule_id = o.tgt_schedule_id
join escalation_policy_steps step on step.id = act.escalation_policy_step_id
join escalation_policies ep on ep.id = step.escalation_policy_id
join services svc on svc.escalation_policy_id = step.escalation_policy_id
where
o.end_time > now() and
o.add_user_id = $1 and
o.remove_user_id isnull
`),
onCallReplaceOverrideAssignments: p.P(`
select distinct
svc.id,
svc.name,
ep.id,
ep.name,
step.step_number,
sched.id,
sched.name,
null,
null,
onCall.user_id notnull
from user_overrides o
join escalation_policy_actions act on act.schedule_id = o.tgt_schedule_id
join escalation_policy_steps step on step.id = act.escalation_policy_step_id
join escalation_policies ep on ep.id = step.escalation_policy_id
join services svc on svc.escalation_policy_id = step.escalation_policy_id
join schedules sched on sched.id = o.tgt_schedule_id
join schedule_rules rule on
rule.schedule_id = o.tgt_schedule_id and
(rule.tgt_user_id isnull or rule.tgt_user_id = o.remove_user_id)
left join schedule_on_call_users onCall on
onCall.user_id = $1 and
onCall.schedule_id = act.schedule_id and
onCall.end_time isnull
left join rotation_participants part on
part.rotation_id = rule.tgt_rotation_id and
part.user_id = o.remove_user_id
where
o.end_time > now() and
o.add_user_id = $1 and
o.remove_user_id notnull and
(rule.tgt_user_id notnull or part notnull)
`),
onCallDirectAssignments: p.P(`
select distinct
svc.id,
svc.name,
ep.id,
ep.name,
step.step_number,
sched.id,
sched.name,
null,
null,
onCall.user_id notnull
from services svc
join escalation_policies ep on ep.id = svc.escalation_policy_id
join escalation_policy_steps step on step.escalation_policy_id = svc.escalation_policy_id
join escalation_policy_actions act on
act.escalation_policy_step_id = step.id and
act.schedule_id notnull
join schedule_rules rule on
rule.schedule_id = act.schedule_id and
(rule.tgt_user_id isnull or rule.tgt_user_id = $1)
join schedules sched on sched.id = act.schedule_id
left join schedule_on_call_users onCall on
onCall.user_id = $1 and
onCall.schedule_id = act.schedule_id and
onCall.end_time isnull
left join rotation_participants part on
part.user_id = $1 and
part.rotation_id = rule.tgt_rotation_id
where
rule.tgt_user_id notnull or
part notnull
`),
epID: p.P(`
select escalation_policy_id
from alerts a
join services s
on s.id = a.service_id
where a.id = $1
`),
}, p.Err
}
func (db *DB) AlertEPID(ctx context.Context, alertID int) (string, error) {
err := permission.LimitCheckAny(ctx, permission.All)
if err != nil {
return "", err
}
row := db.epID.QueryRowContext(ctx, alertID)
var epID string
return epID, errors.Wrap(row.Scan(&epID), "query alert EP ID")
}
func (db *DB) IsUserOnCall(ctx context.Context, userID string) (bool, error) {
err := permission.LimitCheckAny(ctx, permission.All)
if err != nil {
return false, err
}
err = validate.UUID("UserID", userID)
if err != nil {
return false, err
}
var result int
err = db.isOnCall.QueryRowContext(ctx, userID).Scan(&result)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
if err != nil {
return false, err
}
return result == 1, nil
}
type assignment struct {
ServiceID string
ServiceName string
EPID string
EPName string
StepNumber int
SchedID sql.NullString
SchedName sql.NullString
RotID sql.NullString
RotName sql.NullString
Active bool
}
func (a assignment) ID() assignmentID {
return assignmentID{
ServiceID: a.ServiceID,
EPID: a.EPID,
StepNumber: a.StepNumber,
SchedID: a.SchedID.String,
RotID: a.RotID.String,
}
}
type assignmentID struct {
ServiceID string
EPID string
StepNumber int
SchedID string
RotID string
}
func (db *DB) OnCallByUser(ctx context.Context, userID string) ([]OnCallAssignment, error) {
err := permission.LimitCheckAny(ctx, permission.All)
if err != nil {
return nil, err
}
err = validate.UUID("UserID", userID)
if err != nil {
return nil, err
}
tx, err := db.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
fetchAll := func(s *sql.Stmt) ([]assignment, error) {
var result []assignment
rows, err := tx.Stmt(s).QueryContext(ctx, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var f assignment
for rows.Next() {
err = rows.Scan(&f.ServiceID, &f.ServiceName, &f.EPID, &f.EPName, &f.StepNumber, &f.SchedID, &f.SchedName, &f.RotID, &f.RotName, &f.Active)
if err != nil {
return nil, err
}
result = append(result, f)
}
return result, nil
}
direct, err := fetchAll(db.onCallDirectAssignments)
if err != nil {
return nil, errors.Wrap(err, "fetch direct")
}
replace, err := fetchAll(db.onCallReplaceOverrideAssignments)
if err != nil {
return nil, errors.Wrap(err, "fetch replace")
}
add, err := fetchAll(db.onCallAddOverrideAssignments)
if err != nil {
return nil, errors.Wrap(err, "fetch add")
}
userTgt, err := fetchAll(db.onCallEPDirect)
if err != nil {
return nil, errors.Wrap(err, "fetch ep direct")
}
rotTgt, err := fetchAll(db.onCallEPRot)
if err != nil {
return nil, errors.Wrap(err, "fetch ep rotations")
}
remove := make(map[string]bool)
rows, err := tx.Stmt(db.onCallRemoveOverrides).QueryContext(ctx, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var id string
for rows.Next() {
err = rows.Scan(&id)
if err != nil {
return nil, err
}
remove[id] = true
}
// done with DB stuff
rows.Close()
tx.Rollback()
m := make(map[assignmentID]assignment)
all := append(direct, replace...)
all = append(all, add...)
all = append(all, userTgt...)
all = append(all, rotTgt...)
for _, a := range all {
id := a.ID()
e := m[id]
a.Active = (e.Active || a.Active) && !remove[a.SchedID.String]
m[id] = a
}
result := make([]OnCallAssignment, 0, len(m))
for _, a := range m {
result = append(result, OnCallAssignment{
ServiceID: a.ServiceID,
ServiceName: a.ServiceName,
EPID: a.EPID,
EPName: a.EPName,
Level: a.StepNumber,
RotationID: a.RotID.String,
RotationName: a.RotName.String,
ScheduleID: a.SchedID.String,
ScheduleName: a.SchedName.String,
UserID: userID,
IsActive: a.Active,
})
}
return result, nil
}