|
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 |
|
NotificationChannels mapset.Set[uuid.UUID] |
|
} |
|
|
|
func (info updateInfo) calcLatestOnCall(now time.Time) mapset.Set[uuid.UUID] { |
|
if isActive, users := info.ScheduleData.TempOnCall(now); isActive { |
|
|
|
|
|
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) { |
|
|
|
|
|
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, |
|
|
|
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) |
|
result.UsersToStart = newOnCall.Difference(info.CurrentOnCall) |
|
} |
|
|
|
var dataNeedsUpdate bool |
|
newRules := make([]schedule.OnCallNotificationRule, len(info.ScheduleData.V1.OnCallNotificationRules)) |
|
|
|
copy(newRules, info.ScheduleData.V1.OnCallNotificationRules) |
|
for i, r := range newRules { |
|
if r.Time == nil { |
|
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) { |
|
|
|
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 |
|
} |
|
|