File size: 3,801 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
package schedulemanager

import (
	"encoding/json"
	"fmt"
	"time"

	mapset "github.com/deckarep/golang-set/v2"
	"github.com/google/uuid"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/schedule"
	"github.com/target/goalert/util/jsonutil"
)

type updateInfo struct {
	ScheduleID      uuid.UUID
	TimeZone        *time.Location
	RawScheduleData json.RawMessage
	ScheduleData    schedule.Data
	CurrentOnCall   mapset.Set[uuid.UUID]
	Rules           []gadb.SchedMgrRulesRow
	ActiveOverrides []gadb.SchedMgrOverridesRow
}

type updateResult struct {
	ScheduleID           uuid.UUID
	UsersToStart         mapset.Set[uuid.UUID]
	UsersToStop          mapset.Set[uuid.UUID]
	NewRawScheduleData   json.RawMessage       // no update necessary if nil
	NotificationChannels mapset.Set[uuid.UUID] // channels to notify, empty if no notifications
}

func (info updateInfo) calcLatestOnCall(now time.Time) mapset.Set[uuid.UUID] {
	if isActive, users := info.ScheduleData.TempOnCall(now); isActive {
		// temporary schedule config takes precedence over anything else and makes this easy
		// we use a thread-unsafe set here to avoid the cost of locking since this is only called in a single thread
		return mapset.NewThreadUnsafeSet(users...)
	}
	now = now.In(info.TimeZone)
	newOnCall := mapset.NewThreadUnsafeSet[uuid.UUID]()
	for _, r := range info.Rules {
		if ruleRowIsActive(r, now) {
			newOnCall.Add(r.ResolvedUserID)
		}
	}

	for _, o := range info.ActiveOverrides {
		if o.RemoveUserID.Valid {
			if !newOnCall.Contains(o.RemoveUserID.UUID) {
				// if the user to be removed is not currently on-call, skip
				// so we don't add a user for replace overrides that don't match
				continue
			}
			newOnCall.Remove(o.RemoveUserID.UUID)
		}

		if o.AddUserID.Valid {
			newOnCall.Add(o.AddUserID.UUID)
		}
	}

	return newOnCall
}

func (info updateInfo) calcUpdates(now time.Time) (*updateResult, error) {
	if info.CurrentOnCall == nil {
		info.CurrentOnCall = mapset.NewThreadUnsafeSet[uuid.UUID]()
	}

	result := updateResult{
		ScheduleID: info.ScheduleID,
		// since we do this in a single thread, we can use a thread-unsafe set and avoid the cost of locking
		UsersToStart:         mapset.NewThreadUnsafeSet[uuid.UUID](),
		UsersToStop:          mapset.NewThreadUnsafeSet[uuid.UUID](),
		NotificationChannels: mapset.NewThreadUnsafeSet[uuid.UUID](),
	}
	now = now.In(info.TimeZone)

	newOnCall := info.calcLatestOnCall(now)
	onCallChanged := !newOnCall.Equal(info.CurrentOnCall)
	if onCallChanged {
		result.UsersToStop = info.CurrentOnCall.Difference(newOnCall)  // currently on-call, but not anymore
		result.UsersToStart = newOnCall.Difference(info.CurrentOnCall) // not currently on-call, but should be
	}

	var dataNeedsUpdate bool
	newRules := make([]schedule.OnCallNotificationRule, len(info.ScheduleData.V1.OnCallNotificationRules))
	// we copy the rules to avoid modifying the original slice
	copy(newRules, info.ScheduleData.V1.OnCallNotificationRules)
	for i, r := range newRules {
		if r.Time == nil { // if time is not set, then it is a "when schedule changes" rule
			if onCallChanged {
				result.NotificationChannels.Add(r.ChannelID)
			}
			continue
		}
		if r.NextNotification != nil && !r.NextNotification.After(now) {
			result.NotificationChannels.Add(r.ChannelID)
		}
		newTime := nextOnCallNotification(now, r)
		if equalTimePtr(r.NextNotification, newTime) {
			// no change, skip
			continue
		}
		dataNeedsUpdate = true
		newRules[i].NextNotification = newTime
	}

	if dataNeedsUpdate {
		info.ScheduleData.V1.OnCallNotificationRules = newRules
		jsonData, err := jsonutil.Apply(info.RawScheduleData, info.ScheduleData)
		if err != nil {
			return nil, fmt.Errorf("apply schedule data: %w", err)
		}
		result.NewRawScheduleData = jsonData
	}

	return &result, nil
}