-
Notifications
You must be signed in to change notification settings - Fork 153
/
window_v1.go
203 lines (172 loc) · 5.4 KB
/
window_v1.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package models
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
const (
HoursInDay = 24
HoursInMonth = HoursInDay * 30
)
var monthExp = regexp.MustCompile("(\\+|-)?([0-9]+)(M)") //nolint:gosimple
type windowV1 struct {
truncateTo string
offset string
size string
}
func (w windowV1) Validate() error {
_, err := w.prepareWindow() // nolint:dogsled
return err
}
func (windowV1) GetVersion() int {
return 1
}
func (w windowV1) GetTruncateTo() string {
return w.truncateTo
}
func (w windowV1) GetOffset() string {
if w.offset != "" {
return w.offset
}
return w.inHrs(0)
}
func (w windowV1) GetSize() string {
if w.size != "" {
return w.size
}
return w.inHrs(HoursInDay)
}
func (w windowV1) GetStartTime(scheduledAt time.Time) (startTime time.Time, err error) {
jobSpecTaskWindow, err := w.prepareWindow()
if err != nil {
return
}
startTime, _ = jobSpecTaskWindow.getWindowDate(scheduledAt, jobSpecTaskWindow.Size, jobSpecTaskWindow.Offset, jobSpecTaskWindow.TruncateTo)
return
}
func (w windowV1) GetEndTime(scheduledAt time.Time) (endTime time.Time, err error) {
jobSpecTaskWindow, err := w.prepareWindow()
if err != nil {
return
}
_, endTime = jobSpecTaskWindow.getWindowDate(scheduledAt, jobSpecTaskWindow.Size, jobSpecTaskWindow.Offset, jobSpecTaskWindow.TruncateTo)
return
}
type JobSpecTaskWindow struct {
Size time.Duration
Offset time.Duration
TruncateTo string
}
func (w *windowV1) prepareWindow() (JobSpecTaskWindow, error) {
var err error
window := JobSpecTaskWindow{}
window.Size = time.Hour * HoursInDay
window.Offset = 0
window.TruncateTo = ""
if w.truncateTo != "" {
window.TruncateTo = w.truncateTo
}
// check if string contains monthly notation
if w.size != "" {
window.Size, err = w.tryParsingInMonths(w.size)
if err != nil {
// treat as normal duration
window.Size, err = time.ParseDuration(w.size)
if err != nil {
return window, fmt.Errorf("failed to parse task window with size %v: %w", w.size, err)
}
}
}
// check if string contains monthly notation
if w.offset != "" {
window.Offset, err = w.tryParsingInMonths(w.offset)
if err != nil {
// treat as normal duration
window.Offset, err = time.ParseDuration(w.offset)
if err != nil {
return window, fmt.Errorf("failed to parse task window with offset %v: %w", w.offset, err)
}
}
}
return window, nil
}
func (*JobSpecTaskWindow) getWindowDate(today time.Time, windowSize, windowOffset time.Duration, windowTruncateTo string) (time.Time, time.Time) {
floatingEnd := today
// apply truncation to end
if windowTruncateTo == "h" {
// remove time upto hours
floatingEnd = floatingEnd.Truncate(time.Hour)
} else if windowTruncateTo == "d" {
// remove time upto day
floatingEnd = floatingEnd.Truncate(time.Hour * HoursInDay)
} else if windowTruncateTo == "w" {
// shift current window to nearest Sunday
nearestSunday := time.Duration(time.Saturday-floatingEnd.Weekday()+1) * time.Hour * HoursInDay
floatingEnd = floatingEnd.Add(nearestSunday)
floatingEnd = floatingEnd.Truncate(time.Hour * HoursInDay)
}
windowEnd := floatingEnd.Add(windowOffset)
windowStart := windowEnd.Add(-windowSize)
// handle monthly windows separately as every month is not of same size
if windowTruncateTo == "M" {
floatingEnd = today
// shift current window to nearest month start and end
// truncate the date
floatingEnd = time.Date(floatingEnd.Year(), floatingEnd.Month(), 1, 0, 0, 0, 0, time.UTC)
// then add the month offset
// for handling offset, treat 30 days as 1 month
offsetMonths := windowOffset / (time.Hour * HoursInMonth)
floatingEnd = floatingEnd.AddDate(0, int(offsetMonths), 0)
// then find the last day of this month
floatingEnd = floatingEnd.AddDate(0, 1, -1)
// final end is computed
windowEnd = floatingEnd.Truncate(time.Hour * HoursInDay)
// truncate days/hours from window start as well
floatingStart := time.Date(floatingEnd.Year(), floatingEnd.Month(), 1, 0, 0, 0, 0, time.UTC)
// for handling size, treat 30 days as 1 month, and as we have already truncated current month
// subtract 1 from this
sizeMonths := (windowSize / (time.Hour * HoursInMonth)) - 1
if sizeMonths > 0 {
floatingStart = floatingStart.AddDate(0, int(-sizeMonths), 0)
}
// final start is computed
windowStart = floatingStart
}
return windowStart, windowEnd
}
func (windowV1) inHrs(hrs int) string {
if hrs == 0 {
return "0"
}
return fmt.Sprintf("%dh", hrs)
}
// check if string contains monthly notation
func (windowV1) tryParsingInMonths(str string) (time.Duration, error) {
sz := time.Duration(0)
monthMatches := monthExp.FindAllStringSubmatch(str, -1)
if len(monthMatches) > 0 && len(monthMatches[0]) == 4 {
// replace month notation with days first, treating 1M as 30 days
monthsCount, err := strconv.Atoi(monthMatches[0][2])
if err != nil {
return sz, fmt.Errorf("failed to parse task configuration of %s: %w", str, err)
}
sz = time.Hour * HoursInMonth * time.Duration(monthsCount)
if monthMatches[0][1] == "-" {
sz *= -1
}
str = strings.TrimSpace(monthExp.ReplaceAllString(str, ""))
if len(str) > 0 {
// check if there is remaining time that we can still parse
remainingTime, err := time.ParseDuration(str)
if err != nil {
return sz, fmt.Errorf("failed to parse task configuration of %s: %w", str, err)
}
sz += remainingTime
}
return sz, nil
}
return sz, errors.New("invalid month string")
}