File size: 4,119 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 |
package oncall
import (
"sort"
"time"
"github.com/target/goalert/assignment"
"github.com/target/goalert/override"
"github.com/target/goalert/schedule"
"github.com/target/goalert/schedule/rotation"
"github.com/target/goalert/schedule/rule"
)
type ResolvedRule struct {
rule.Rule
Rotation *ResolvedRotation
}
type ResolvedRotation struct {
rotation.Rotation
CurrentIndex int
CurrentStart time.Time
CurrentEnd time.Time
Users []string
}
type state struct {
tempScheds []schedule.TemporarySchedule
rules []ResolvedRule
overrides []override.UserOverride
history []Shift
now time.Time
loc *time.Location
}
func (r *ResolvedRotation) UserID(t time.Time) string {
if r == nil || len(r.Users) == 0 {
return ""
}
if len(r.Users) == 1 {
return r.Users[0]
}
if r.CurrentStart.IsZero() {
r.CurrentStart = r.StartTime(t)
}
if r.CurrentEnd.IsZero() {
r.CurrentStart = r.StartTime(r.CurrentStart)
r.CurrentEnd = r.EndTime(r.CurrentStart)
}
if t.Before(r.CurrentEnd) && !t.Before(r.CurrentStart) {
return r.Users[r.CurrentIndex]
}
for !t.Before(r.CurrentEnd) {
r.CurrentStart = r.CurrentEnd
r.CurrentEnd = r.EndTime(r.CurrentStart)
r.CurrentIndex++
}
for t.Before(r.CurrentStart) {
r.CurrentEnd = r.CurrentStart
r.CurrentStart = r.StartTime(r.CurrentStart.Add(-1))
r.CurrentIndex--
}
r.CurrentIndex %= len(r.Users)
if r.CurrentIndex < 0 {
r.CurrentIndex += len(r.Users)
}
return r.Users[r.CurrentIndex]
}
func (r ResolvedRule) UserID(t time.Time) string {
if !r.IsActive(t) {
return ""
}
switch r.Target.TargetType() {
case assignment.TargetTypeUser:
return r.Target.TargetID()
case assignment.TargetTypeRotation:
return r.Rotation.UserID(t)
}
panic("unknown target type " + r.Target.TargetType().String())
}
func sortShifts(s []Shift) {
sort.Slice(s, func(i, j int) bool {
if s[i].Start.Equal(s[j].Start) {
return s[i].UserID < s[j].UserID
}
return s[i].Start.Before(s[j].Start)
})
}
func (s *state) CalculateShifts(start, end time.Time) []Shift {
start = start.Truncate(time.Minute)
end = end.Truncate(time.Minute)
tiStart := start
if !s.now.IsZero() && s.now.Before(start) {
tiStart = s.now.Truncate(time.Minute)
}
historyCutoff := s.now.Truncate(time.Minute).Add(time.Minute)
t := NewTimeIterator(tiStart, end, time.Minute)
defer t.Close()
hist := t.NewUserCalculator()
sort.Slice(s.history, func(i, j int) bool { return s.history[i].Start.Before(s.history[j].Start) })
for _, s := range s.history {
if s.End.IsZero() {
// have currently active shifts "end"
// at the cutoff.
s.End = historyCutoff
}
hist.SetSpan(s.Start, s.End, s.UserID)
}
hist.Init()
tempScheds := t.NewTemporaryScheduleCalculator(s.tempScheds)
// sort overrides so that overlapping spans are merged properly
overrides := t.NewOverrideCalculator(s.overrides)
rules := t.NewRulesCalculator(s.loc, s.rules)
var shifts []Shift
isOnCall := make(map[string]*Shift)
stillOnCall := make(map[string]bool)
setOnCall := func(userIDs []string) {
// reset map
for id := range stillOnCall {
delete(stillOnCall, id)
}
now := time.Unix(t.Unix(), 0)
for _, id := range userIDs {
stillOnCall[id] = true
if isOnCall[id] != nil {
continue
}
isOnCall[id] = &Shift{
Start: now,
UserID: id,
}
}
for id, shift := range isOnCall {
if stillOnCall[id] {
continue
}
// no longer on call
if now.After(start) {
shift.End = now
shifts = append(shifts, *shift)
}
delete(isOnCall, id)
}
}
for t.Next() {
if time.Unix(t.Unix(), 0).Before(historyCutoff) {
// use history if in the past
setOnCall(hist.ActiveUsers())
continue
}
if tempScheds.Active() {
// use TemporarySchedule if one is active
setOnCall(tempScheds.ActiveUsers())
continue
}
// apply any overrides
setOnCall(overrides.MapUsers(rules.ActiveUsers()))
}
// remaining shifts are truncated
for _, s := range isOnCall {
s.Truncated = true
s.End = time.Unix(t.Unix(), 0)
shifts = append(shifts, *s)
}
sortShifts(shifts)
return shifts
}
|