File size: 4,500 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
package rotation

import (
	"time"

	"github.com/target/goalert/util/timeutil"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

type Rotation struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`

	Type           Type      `json:"type"`
	Start          time.Time `json:"start"`
	ShiftLength    int       `json:"shift_length"`
	isUserFavorite bool
}

func (r Rotation) IsUserFavorite() bool {
	return r.isUserFavorite
}

func (r Rotation) shiftClock() timeutil.Clock {
	switch r.Type {
	case TypeHourly:
		return timeutil.NewClock(r.ShiftLength, 0)
	case TypeDaily:
		return timeutil.NewClock(r.ShiftLength*24, 0)
	case TypeWeekly:
		return timeutil.NewClock(r.ShiftLength*24*7, 0)
	default:
		// monthly is handled separately
		panic("unexpected rotation type")
	}
}

// monthStartTime recursively calculates the previous handoff time of a rotation active at t.
func (r Rotation) monthStartTime(t time.Time, n int) time.Time {
	if n > 10000 {
		panic("too many iterations")
	}

	if !t.Before(r.Start) { // t is at or after start of rotation
		next := r.Start.AddDate(0, r.ShiftLength*n, 0)
		if next.After(t) {
			return r.Start.AddDate(0, r.ShiftLength*(n-1), 0)
		}

		// recursively finds the end of shift time of the rotation which came immediately before t
		return r.monthStartTime(t, n+1)
	}

	// t is before start of rotation
	prev := r.Start.AddDate(0, -r.ShiftLength*n, 0)
	if prev.Before(t) {
		return prev
	}

	// recursively finds the end of shift time of the rotation which came immediately before t when t is before the rotation start time
	return r.monthStartTime(t, n+1)
}

// monthEndTime recursively calculates the end of the rotation (handoff time) that was active at time t.
func (r Rotation) monthEndTime(t time.Time, n int) time.Time {
	if n > 10000 {
		panic("too many iterations")
	}

	if !t.Before(r.Start) { // t is at or after start of rotation
		next := r.Start.AddDate(0, r.ShiftLength*n, 0)
		if next.After(t) {
			return next
		}

		// recursively finds the immediate end of shift time after t
		return r.monthEndTime(t, n+1)
	}

	// t is before start of rotation
	prev := r.Start.AddDate(0, -r.ShiftLength*n, 0)
	if prev.Before(t) || prev.Equal(t) {
		return r.Start.AddDate(0, -r.ShiftLength*(n-1), 0)
	}

	// recursively finds the immediate end of shift time after t for cases when t is before the rotation start time
	return r.monthEndTime(t, n+1)
}

// StartTime calculates the start of the "shift" that started at (or was active) at t.
// For daily, weekly, and monthly rotations, start time will be the previous handoff time (from start).
// For monthly rotations, the monthStartTime function is used to recursively handle calculations as the length of months vary.
func (r Rotation) StartTime(t time.Time) time.Time {
	if r.ShiftLength <= 0 {
		r.ShiftLength = 1
	}
	t = t.In(r.Start.Location()).Truncate(time.Minute)
	r.Start = r.Start.Truncate(time.Minute)

	if r.Type == TypeMonthly {
		return r.monthStartTime(t, 1)
	}

	shiftClockLen := r.shiftClock()
	rem := timeutil.ClockDiff(r.Start, t) % shiftClockLen

	if rem < 0 {
		rem += shiftClockLen
	}

	return timeutil.AddClock(t, -rem)
}

// EndTime calculates the end of the "shift" that started at (or was active) at t.
// For monthly rotations, the monthEndTime function is used to recursively handle calculations as the length of months vary.
//
// It is guaranteed to occur after t.
func (r Rotation) EndTime(t time.Time) time.Time {
	if r.ShiftLength <= 0 {
		r.ShiftLength = 1
	}
	t = t.In(r.Start.Location()).Truncate(time.Minute)
	r.Start = r.Start.Truncate(time.Minute)

	if r.Type == TypeMonthly {
		return r.monthEndTime(t, 1)
	}

	shiftClockLen := r.shiftClock()
	rem := timeutil.ClockDiff(r.Start, t) % shiftClockLen

	if rem < 0 {
		rem += shiftClockLen
	}

	return timeutil.AddClock(t, shiftClockLen-rem)
}

func (r Rotation) Normalize() (*Rotation, error) {
	if r.ShiftLength == 0 {
		// default to 1
		r.ShiftLength = 1
	}
	r.Start = r.Start.Truncate(time.Minute)

	if r.Start.Location() == nil {
		return nil, validation.NewFieldError("TimeZone", "must be specified")
	}
	err := validate.Many(
		validate.IDName("Name", r.Name),
		validate.Range("ShiftLength", r.ShiftLength, 1, 9000),
		validate.OneOf("Type", r.Type, TypeMonthly, TypeWeekly, TypeDaily, TypeHourly),
		validate.Text("Description", r.Description, 1, 255),
	)
	if err != nil {
		return nil, err
	}

	return &r, nil
}