-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
283 lines (254 loc) · 9.4 KB
/
window.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package window
import (
"fmt"
"time"
)
func mapUnitToDuration(unit string) (d time.Duration) {
switch unit {
case "nanosecond", "nanoseconds":
d = time.Nanosecond
case "microsecond", "microseconds":
d = time.Microsecond
case "millisecond", "milliseconds":
d = time.Millisecond
case "second", "seconds":
d = time.Second
case "minute", "minutes":
d = time.Minute
case "hour", "hours":
d = time.Hour
case "day", "days":
d = time.Hour * 24
case "week", "weeks":
d = time.Hour * 24 * 7
}
return d
}
// getPeriodWords returns a list of possible predefined words that can be used in the bound definition relative to now
// ex: "last X" or "next Y"
func getPeriodWords() []string {
return []string{
"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday",
"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december",
"nanosecond", "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "year",
"nanoseconds", "microseconds", "milliseconds", "seconds", "minutes", "hour", "days", "weeks", "months", "years",
}
}
// getShortWords returns a list of possible predefined words that can be used in the bound definition relative to now as is
func getShortWords() []string {
return []string{"today", "yesterday", "now", "tomorrow"}
}
// boundRelativeToNow contains a time specification relative to another point in time
// ex: "yesterday", "last june", "next week", "2 days after"
type boundRelativeToNow struct {
inFuture bool // direction
verbal string // "june", "year", "week", "today", "yesterday"
duration time.Duration // "2 days", "1 second"
}
// resolveAt map the relN bound to time. It uses isFuture/isLeftBound to understand which bound of the interval to pick.
// -----[last year]------[NOW]------[next year]----
// ^ ^ ^ ^ <---- possible picks depending on isLeftBound and isFuture
func (b *boundRelativeToNow) resolveAt(n time.Time, isLeftBound bool) time.Time {
layout := "2006-01-02 15:04:05.000000000 MST"
tz := n.Format("MST")
var leftBoundString, rightBoundString string
// verbal map
if b.verbal != "" {
sign := -1
if b.inFuture {
sign = 1
}
switch b.verbal {
case "now":
return n
case "today":
tomorrowString := n.Format("2006-01-02")
leftBoundString = fmt.Sprintf("%s 00:00:00.000000000 %s", tomorrowString, tz)
rightBoundString = fmt.Sprintf("%s 23:59:59.999999999 %s", tomorrowString, tz)
case "tomorrow":
tomorrowString := n.AddDate(0, 0, 1).Format("2006-01-02")
leftBoundString = fmt.Sprintf("%s 00:00:00.000000000 %s", tomorrowString, tz)
rightBoundString = fmt.Sprintf("%s 23:59:59.999999999 %s", tomorrowString, tz)
case "yesterday":
tomorrowString := n.AddDate(0, 0, -1).Format("2006-01-02")
leftBoundString = fmt.Sprintf("%s 00:00:00.000000000 %s", tomorrowString, tz)
rightBoundString = fmt.Sprintf("%s 23:59:59.999999999 %s", tomorrowString, tz)
case "nanosecond", "nanoseconds":
nanosecondString := n.Add(time.Duration(sign) * time.Nanosecond).Format("2006-01-02 15:04:05.999999999")
leftBoundString = fmt.Sprintf("%s %s", nanosecondString, tz)
rightBoundString = fmt.Sprintf("%s %s", nanosecondString, tz)
case "microsecond", "microseconds":
microsecondString := n.Add(time.Duration(sign) * time.Microsecond).Format("2006-01-02 15:04:05.999999")
leftBoundString = fmt.Sprintf("%s000 %s", microsecondString, tz)
rightBoundString = fmt.Sprintf("%s999 %s", microsecondString, tz)
case "millisecond", "milliseconds":
millisecondString := n.Add(time.Duration(sign) * time.Millisecond).Format("2006-01-02 15:04:05.999")
leftBoundString = fmt.Sprintf("%s000000 %s", millisecondString, tz)
rightBoundString = fmt.Sprintf("%s999999 %s", millisecondString, tz)
case "second", "seconds":
secondString := n.Add(time.Duration(sign) * time.Second).Format("2006-01-02 15:04:05")
leftBoundString = fmt.Sprintf("%s.000000000 %s", secondString, tz)
rightBoundString = fmt.Sprintf("%s.999999999 %s", secondString, tz)
case "minute", "minutes":
minuteString := n.Add(time.Duration(sign) * time.Minute).Format("2006-01-02 15:04")
leftBoundString = fmt.Sprintf("%s:00.000000000 %s", minuteString, tz)
rightBoundString = fmt.Sprintf("%s:59.999999999 %s", minuteString, tz)
case "hour", "hours":
hourString := n.Add(time.Duration(sign) * time.Hour).Format("2006-01-02 15")
leftBoundString = fmt.Sprintf("%s:00:00.000000000 %s", hourString, tz)
rightBoundString = fmt.Sprintf("%s:59:59.999999999 %s", hourString, tz)
case "day", "days":
dayString := n.AddDate(0, 0, sign*1).Format("2006-01-02")
leftBoundString = fmt.Sprintf("%s 00:00:00.000000000 %s", dayString, tz)
rightBoundString = fmt.Sprintf("%s 23:59:59.999999999 %s", dayString, tz)
case "week", "weeks":
var nextMonday time.Time
switch n.Weekday() {
case time.Monday:
nextMonday = n.AddDate(0, 0, 7)
case time.Tuesday:
nextMonday = n.AddDate(0, 0, 6)
case time.Wednesday:
nextMonday = n.AddDate(0, 0, 5)
case time.Thursday:
nextMonday = n.AddDate(0, 0, 4)
case time.Friday:
nextMonday = n.AddDate(0, 0, 3)
case time.Saturday:
nextMonday = n.AddDate(0, 0, 2)
case time.Sunday:
nextMonday = n.AddDate(0, 0, 1)
}
mondayString := nextMonday.Format("2006-01-02")
leftBoundString = fmt.Sprintf("%s 00:00:00.000000000 %s", mondayString, tz)
rightBoundString = fmt.Sprintf("%s 23:59:59.999999999 %s", mondayString, tz)
case "month", "months":
daysInMonth := map[string]uint8{
"1": 30,
"2": 31,
"3": 30,
"4": 31,
"5": 30,
"6": 31,
"7": 30,
"8": 31,
"9": 30,
"10": 31,
"11": 30,
"12": 31,
}
d := n.AddDate(0, sign*1, 0)
days := daysInMonth[d.Format("M")]
monthString := n.AddDate(0, sign*1, 0).Format("2006-01")
leftBoundString = fmt.Sprintf("%s-01 00:00:00.000000000 %s", monthString, tz)
rightBoundString = fmt.Sprintf("%s-%d 23:59:59.999999999 %s", monthString, days, tz)
case "year", "years":
yearString := n.AddDate(sign*1, 0, 0).Format("2006")
leftBoundString = fmt.Sprintf("%s-01-01 00:00:00.000000000 %s", yearString, tz)
rightBoundString = fmt.Sprintf("%s-12-31 23:59:59.999999999 %s", yearString, tz)
default:
panic(fmt.Errorf("verbal [%s] not recognized", b.verbal))
}
}
leftBoundTime, _ := time.Parse(layout, leftBoundString)
rightBoundTime, _ := time.Parse(layout, rightBoundString)
// If the period is in the left bound (from yesterday to ...) then we use the right bound of the period
// ----[period]-----NOW---
// ^ <-- this bound is used if the period is met in the left window bound
// ----NOW------[period]--
// ^ <-- otherwise the left bound is used
if isLeftBound {
return rightBoundTime
}
return leftBoundTime
}
// Specification contains left/right bounds for a window that can be resolved to absolute time when needed
type Specification struct {
leftBoundAbs, rightBoundAbs *time.Time // "2 April 2022"
leftBoundRel, rightBoundRel *time.Duration // "3 days"
leftBoundRelN, rightBoundRelN *boundRelativeToNow // "2 days ago" or "last june"
}
func makeSpecification(leftBound, rightBound any) Specification {
s := Specification{}
switch v := leftBound.(type) {
case time.Time:
s.leftBoundAbs = &v
case time.Duration:
s.leftBoundRel = &v
case boundRelativeToNow:
s.leftBoundRelN = &v
}
switch v := rightBound.(type) {
case time.Time:
s.rightBoundAbs = &v
case time.Duration:
s.rightBoundRel = &v
case boundRelativeToNow:
s.rightBoundRelN = &v
}
return s
}
// ResolveAt will generate a new Window instance
// It resolves all relative time points to absolute ones relatively to the given time point
func (s *Specification) ResolveAt(t time.Time) *Window {
w := Window{}
// left bound
if s.leftBoundAbs != nil {
w.from = s.leftBoundAbs
} else if s.leftBoundRel != nil {
w.slide = *s.leftBoundRel
} else {
rt := s.leftBoundRelN.resolveAt(t, true)
w.from = &rt
}
// right bound
if s.rightBoundAbs != nil {
w.to = s.rightBoundAbs
} else if s.rightBoundRel != nil {
rt := w.from.Add(*s.rightBoundRel)
w.to = &rt
} else if s.rightBoundRelN != nil {
rt := s.rightBoundRelN.resolveAt(t, false)
w.to = &rt
}
// edge-case: left bound is a Rel and the right is an Abs, so calculate the left bound abs value relative to the right bound abs value
if s.leftBoundRel != nil && w.to != nil {
lt := w.to.Add(-*s.leftBoundRel)
w.from = <
w.slide = 0 // reset the slide
}
w.validate()
return &w
}
func (s *Specification) validate() {
if s.rightBoundRel != nil && s.leftBoundRel != nil {
panic(fmt.Errorf("two rel bound are not allowed"))
}
}
type Window struct {
slide time.Duration
from, to *time.Time
}
// GetBounds return absolute times as left and right bound of the window
func (w *Window) GetBounds() (from, to time.Time) {
if w.from == nil || w.to == nil {
panic("absolute bound are not defined on this window")
}
return *w.from, *w.to
}
// IsSliding return true if the window has no absolute bounds, only the duration
func (w *Window) IsSliding() bool {
return w.slide != 0
}
// GetSlide return duration of a sliding window
func (w *Window) GetSlide() time.Duration {
return w.slide
}
func (w *Window) validate() {
if w.from != nil && w.to != nil && w.from.After(*w.to) {
panic(fmt.Errorf("window bounds are in wrong order"))
}
if w.from == nil && w.to == nil && w.slide == 0 {
panic("empty window")
}
}