/
schedule.go
328 lines (290 loc) · 9.52 KB
/
schedule.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
package graphqlapp
import (
context "context"
"database/sql"
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/target/goalert/assignment"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/notificationchannel"
"github.com/target/goalert/oncall"
"github.com/target/goalert/permission"
"github.com/target/goalert/schedule"
"github.com/target/goalert/schedule/rule"
"github.com/target/goalert/search"
"github.com/target/goalert/util"
"github.com/target/goalert/validation"
"github.com/target/goalert/validation/validate"
)
type Schedule App
type TemporarySchedule App
type OnCallNotificationRule App
func (a *App) Schedule() graphql2.ScheduleResolver { return (*Schedule)(a) }
func (a *App) TemporarySchedule() graphql2.TemporaryScheduleResolver { return (*TemporarySchedule)(a) }
func (a *App) OnCallNotificationRule() graphql2.OnCallNotificationRuleResolver {
return (*OnCallNotificationRule)(a)
}
func (a *OnCallNotificationRule) Target(ctx context.Context, raw *schedule.OnCallNotificationRule) (*assignment.RawTarget, error) {
ch, err := a.NCStore.FindOne(ctx, raw.ChannelID)
if err != nil {
return nil, err
}
if ch.Type == notificationchannel.TypeSlack {
return &assignment.RawTarget{
Type: assignment.TargetTypeSlackChannel,
ID: ch.Value,
Name: ch.Name,
}, nil
}
return &assignment.RawTarget{Type: assignment.TargetTypeNotificationChannel, ID: ch.ID}, nil
}
func (a *TemporarySchedule) Shifts(ctx context.Context, temp *schedule.TemporarySchedule) ([]oncall.Shift, error) {
result := make([]oncall.Shift, 0, len(temp.Shifts))
for _, s := range temp.Shifts {
result = append(result, oncall.Shift{
UserID: s.UserID,
Start: s.Start,
End: s.End,
})
}
return result, nil
}
func (q *Query) Schedule(ctx context.Context, id string) (*schedule.Schedule, error) {
return (*App)(q).FindOneSchedule(ctx, id)
}
func (s *Schedule) Shifts(ctx context.Context, raw *schedule.Schedule, start, end time.Time) ([]oncall.Shift, error) {
if end.Before(start) {
return nil, validation.NewFieldError("EndTime", "must be after StartTime")
}
if end.After(start.AddDate(0, 0, 50)) {
return nil, validation.NewFieldError("EndTime", "cannot be more than 50 days past StartTime")
}
return s.OnCallStore.HistoryBySchedule(ctx, raw.ID, start, end)
}
func (s *Schedule) TemporarySchedules(ctx context.Context, raw *schedule.Schedule) ([]schedule.TemporarySchedule, error) {
id, err := parseUUID("ScheduleID", raw.ID)
if err != nil {
return nil, err
}
return s.ScheduleStore.TemporarySchedules(ctx, nil, id)
}
func (s *Schedule) OnCallNotificationRules(ctx context.Context, raw *schedule.Schedule) ([]schedule.OnCallNotificationRule, error) {
id, err := parseUUID("ScheduleID", raw.ID)
if err != nil {
return nil, err
}
return s.ScheduleStore.OnCallNotificationRules(ctx, nil, id)
}
func (s *Schedule) Target(ctx context.Context, raw *schedule.Schedule, input assignment.RawTarget) (*graphql2.ScheduleTarget, error) {
rules, err := s.RuleStore.FindByTargetTx(ctx, nil, raw.ID, input)
if err != nil {
return nil, err
}
return &graphql2.ScheduleTarget{
ScheduleID: raw.ID,
Target: &input,
Rules: rules,
}, nil
}
func (s *Schedule) Targets(ctx context.Context, raw *schedule.Schedule) ([]graphql2.ScheduleTarget, error) {
rules, err := s.RuleStore.FindAll(ctx, raw.ID)
if err != nil {
return nil, err
}
m := make(map[assignment.RawTarget][]rule.Rule)
for _, r := range rules {
tgt := assignment.RawTarget{ID: r.Target.TargetID(), Type: r.Target.TargetType()}
m[tgt] = append(m[tgt], r)
}
result := make([]graphql2.ScheduleTarget, 0, len(m))
for tgt, rules := range m {
t := tgt // need to make a copy so we can take a pointer
result = append(result, graphql2.ScheduleTarget{
Target: &t,
ScheduleID: raw.ID,
Rules: rules,
})
}
return result, nil
}
func (s *Schedule) AssignedTo(ctx context.Context, raw *schedule.Schedule) ([]assignment.RawTarget, error) {
pols, err := s.PolicyStore.FindAllPoliciesBySchedule(ctx, raw.ID)
if err != nil {
return nil, err
}
sort.Slice(pols, func(i, j int) bool { return strings.ToLower(pols[i].Name) < strings.ToLower(pols[j].Name) })
tgt := make([]assignment.RawTarget, len(pols))
for i, p := range pols {
tgt[i] = assignment.RawTarget{
ID: p.ID,
Name: p.Name,
Type: assignment.TargetTypeEscalationPolicy,
}
}
return tgt, nil
}
func (m *Mutation) UpdateSchedule(ctx context.Context, input graphql2.UpdateScheduleInput) (ok bool, err error) {
var loc *time.Location
if input.TimeZone != nil {
loc, err = util.LoadLocation(*input.TimeZone)
if err != nil {
return false, validation.NewFieldError("timeZone", err.Error())
}
}
err = withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
sched, err := m.ScheduleStore.FindOneForUpdate(ctx, tx, input.ID)
if err != nil {
return err
}
if input.Name != nil {
sched.Name = *input.Name
}
if input.Description != nil {
sched.Description = *input.Description
}
if loc != nil {
sched.TimeZone = loc
}
return m.ScheduleStore.UpdateTx(ctx, tx, sched)
})
return err == nil, err
}
func (m *Mutation) CreateSchedule(ctx context.Context, input graphql2.CreateScheduleInput) (sched *schedule.Schedule, err error) {
usedTargets := make(map[assignment.RawTarget]int, len(input.Targets))
for i, tgt := range input.Targets {
fieldPrefix := fmt.Sprintf("targets[%d].", i)
// validating both are not nil
if tgt.NewRotation == nil && tgt.Target == nil {
return nil, validate.Many(
validation.NewFieldError(fieldPrefix+"target", "one of `target` or `newRotation` is required"),
validation.NewFieldError(fieldPrefix+"newRotation", "one of `target` or `newRotation` is required"),
)
}
// validating only one is present
if tgt.NewRotation != nil && tgt.Target != nil {
return nil, validate.Many(
validation.NewFieldError(fieldPrefix+"target", "cannot be used with `newRotation`"),
validation.NewFieldError(fieldPrefix+"newRotation", "cannot be used with `target`"),
)
}
// checking for duplicate targets
if tgt.Target != nil {
raw := assignment.NewRawTarget(tgt.Target)
if oldIndex, ok := usedTargets[raw]; ok {
return nil, validation.NewFieldError(fieldPrefix+"target", fmt.Sprintf("must be unique. Conflicts with existing `targets[%d].target`.", oldIndex))
}
usedTargets[raw] = i
}
}
loc, err := util.LoadLocation(input.TimeZone)
if err != nil {
return nil, validation.NewFieldError("timeZone", err.Error())
}
err = withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
s := &schedule.Schedule{
Name: input.Name,
TimeZone: loc,
}
if input.Description != nil {
s.Description = *input.Description
}
sched, err = m.ScheduleStore.CreateScheduleTx(ctx, tx, s)
if err != nil {
return err
}
if input.Favorite != nil && *input.Favorite {
err = m.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.ScheduleTarget(sched.ID))
if err != nil {
return err
}
}
for i := range input.Targets {
if input.Targets[i].NewRotation == nil {
continue
}
rot, err := m.CreateRotation(ctx, *input.Targets[i].NewRotation)
if err != nil {
return validation.AddPrefix("targets["+strconv.Itoa(i)+"].newRotation.", err)
}
// Inserting newly created rotation as 'target' with it's corresponding rules
input.Targets[i].Target = &assignment.RawTarget{Type: assignment.TargetTypeRotation, ID: rot.ID, Name: rot.Name}
}
for i, r := range input.Targets {
r.ScheduleID = &sched.ID
_, err = m.UpdateScheduleTarget(ctx, r)
if err != nil {
return validation.AddPrefix("targets["+strconv.Itoa(i)+"].", err)
}
}
for i, override := range input.NewUserOverrides {
override.ScheduleID = &sched.ID
_, err = m.CreateUserOverride(ctx, override)
if err != nil {
return validation.AddPrefix("newUserOverride["+strconv.Itoa(i)+"].", err)
}
}
return nil
})
return sched, err
}
func (r *Schedule) TimeZone(ctx context.Context, data *schedule.Schedule) (string, error) {
return data.TimeZone.String(), nil
}
func (q *Query) Schedules(ctx context.Context, opts *graphql2.ScheduleSearchOptions) (conn *graphql2.ScheduleConnection, err error) {
if opts == nil {
opts = &graphql2.ScheduleSearchOptions{}
}
var searchOpts schedule.SearchOptions
searchOpts.FavoritesUserID = permission.UserID(ctx)
if opts.Search != nil {
searchOpts.Search = *opts.Search
}
if opts.FavoritesOnly != nil {
searchOpts.FavoritesOnly = *opts.FavoritesOnly
}
if opts.FavoritesFirst != nil {
searchOpts.FavoritesFirst = *opts.FavoritesFirst
}
searchOpts.Omit = opts.Omit
if opts.After != nil && *opts.After != "" {
err = search.ParseCursor(*opts.After, &searchOpts)
if err != nil {
return nil, err
}
}
if opts.First != nil {
searchOpts.Limit = *opts.First
}
if searchOpts.Limit == 0 {
searchOpts.Limit = 15
}
searchOpts.Limit++
scheds, err := q.ScheduleStore.Search(ctx, &searchOpts)
if err != nil {
return nil, err
}
conn = new(graphql2.ScheduleConnection)
conn.PageInfo = &graphql2.PageInfo{}
if len(scheds) == searchOpts.Limit {
scheds = scheds[:len(scheds)-1]
conn.PageInfo.HasNextPage = true
}
if len(scheds) > 0 {
last := scheds[len(scheds)-1]
searchOpts.After.IsFavorite = last.IsUserFavorite()
searchOpts.After.Name = last.Name
cur, err := search.Cursor(searchOpts)
if err != nil {
return conn, err
}
conn.PageInfo.EndCursor = &cur
}
conn.Nodes = scheds
return conn, err
}
func (s *Schedule) IsFavorite(ctx context.Context, raw *schedule.Schedule) (bool, error) {
return raw.IsUserFavorite(), nil
}