File size: 1,607 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
package oncall

import (
	"sort"

	"github.com/target/goalert/schedule"
)

// TemporaryScheduleCalculator will calculate active state and active users for a set of TemporarySchedules.
type TemporaryScheduleCalculator struct {
	*TimeIterator

	act *ActiveCalculator
	usr *UserCalculator
}

// NewTemporaryScheduleCalculator will create a new TemporaryScheduleCalculator bound to the TimeIterator.
func (t *TimeIterator) NewTemporaryScheduleCalculator(tempScheds []schedule.TemporarySchedule) *TemporaryScheduleCalculator {
	ts := &TemporaryScheduleCalculator{
		TimeIterator: t,
		act:          t.NewActiveCalculator(),
		usr:          t.NewUserCalculator(),
	}

	sort.Slice(tempScheds, func(i, j int) bool { return tempScheds[i].Start.Before(tempScheds[j].Start) })
	var allShifts []schedule.FixedShift

	for _, temp := range tempScheds {
		ts.act.SetSpan(temp.Start, temp.End)
		allShifts = append(allShifts, temp.Shifts...)
	}
	sort.Slice(allShifts, func(i, j int) bool { return allShifts[i].Start.Before(allShifts[j].Start) })

	for _, s := range allShifts {
		ts.usr.SetSpan(s.Start, s.End, s.UserID)
	}
	ts.act.Init()
	ts.usr.Init()

	return ts
}

// Active will return true if a TemporarySchedule is currently active.
func (fg *TemporaryScheduleCalculator) Active() bool { return fg.act.Active() }

// ActiveUsers will return the current set of ActiveUsers. It is only valid if `Active()` is true.
//
// It is only valid if `Active()` is true and until the following Next() call. It should not be modified.
func (fg *TemporaryScheduleCalculator) ActiveUsers() []string { return fg.usr.ActiveUsers() }