This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolution.go
executable file
·224 lines (188 loc) · 6.31 KB
/
resolution.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
package mgots
import (
"strconv"
"time"
)
// A Resolution specifies the granularity of saved samples and the organization
// in sets.
type Resolution interface {
// Split should return the set timestamp and sample key for the given time.
Split(t time.Time) (time.Time, string)
// Join should return the timestamp of a single sample based on the timestamp
// of a set and the key of the sample.
Join(t time.Time, key string) time.Time
// SetSize should return the number of samples per set.
SetSize() int
// SetTimestamp should return the set timestamp for the given time.
SetTimestamp(t time.Time) time.Time
// SetTimestamps should return a list set timestamps for the given time range.
SetTimestamps(first time.Time, last time.Time) []time.Time
// SampleKey should return the sample key for given time.
SampleKey(t time.Time) string
// SampleTimestamp should return the sample timestamp for the given time.
SampleTimestamp(t time.Time) time.Time
// SampleTimestamps should return a list sample timestamps for the given time
// range.
SampleTimestamps(first, last time.Time) []time.Time
}
// BasicResolution defines the granularity of the saved metrics.
type BasicResolution int
// The following basic resolutions are available:
const (
OneMinuteOf60Seconds BasicResolution = iota
OneHourOf60Minutes
OneDayOf24Hours
OneMonthOfUpTo31Days
OneHourOf3600Seconds
OneDayOf1440Minutes
)
// Split will return the set timestamp and sample key for the given time.
func (r BasicResolution) Split(t time.Time) (time.Time, string) {
return r.SetTimestamp(t), r.SampleKey(t)
}
// Join will return the timestamp of a single sample based on the start of a
// set and the key of the sample.
func (r BasicResolution) Join(t time.Time, key string) time.Time {
// convert key to integer
i, _ := strconv.Atoi(key)
var ts time.Time
switch r {
case OneMinuteOf60Seconds:
ts = t.Add(time.Duration(i) * time.Second)
case OneHourOf60Minutes:
ts = t.Add(time.Duration(i) * time.Minute)
case OneDayOf24Hours:
ts = t.Add(time.Duration(i) * time.Hour)
case OneMonthOfUpTo31Days:
ts = t.AddDate(0, 0, i-1)
case OneHourOf3600Seconds:
ts = t.Add(time.Duration(i) * time.Second)
case OneDayOf1440Minutes:
ts = t.Add(time.Duration(i) * time.Minute)
}
return ts
}
// SetSize will return the number of samples per set.
func (r BasicResolution) SetSize() int {
var size int
switch r {
case OneMinuteOf60Seconds:
size = 60
case OneHourOf60Minutes:
size = 60
case OneDayOf24Hours:
size = 24
case OneMonthOfUpTo31Days:
size = 31
case OneHourOf3600Seconds:
size = 3600
case OneDayOf1440Minutes:
size = 1440
}
return size
}
// SetTimestamp will return the set timestamp for the given time.
func (r BasicResolution) SetTimestamp(t time.Time) time.Time {
var ts time.Time
switch r {
case OneMinuteOf60Seconds:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
case OneHourOf60Minutes:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
case OneDayOf24Hours:
ts = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
case OneMonthOfUpTo31Days:
ts = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
case OneHourOf3600Seconds:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
case OneDayOf1440Minutes:
ts = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
return ts
}
// SetTimestamps will return a list set timestamps for the given time range.
func (r BasicResolution) SetTimestamps(first, last time.Time) []time.Time {
firstSet := r.SetTimestamp(first)
curSet := firstSet
list := make([]time.Time, 0)
for curSet.Before(last) || curSet.Equal(last) {
list = append(list, curSet)
switch r {
case OneMinuteOf60Seconds:
curSet = curSet.Add(1 * time.Minute)
case OneHourOf60Minutes:
curSet = curSet.Add(1 * time.Hour)
case OneDayOf24Hours:
curSet = curSet.AddDate(0, 0, 1)
case OneMonthOfUpTo31Days:
curSet = curSet.AddDate(0, 1, 0)
case OneHourOf3600Seconds:
curSet = curSet.Add(1 * time.Hour)
case OneDayOf1440Minutes:
curSet = curSet.AddDate(0, 0, 1)
}
}
return list
}
// SampleKey will return the sample key for given time.
func (r BasicResolution) SampleKey(t time.Time) string {
var key string
switch r {
case OneMinuteOf60Seconds:
key = strconv.Itoa(t.Second())
case OneHourOf60Minutes:
key = strconv.Itoa(t.Minute())
case OneDayOf24Hours:
key = strconv.Itoa(t.Hour())
case OneMonthOfUpTo31Days:
key = strconv.Itoa(t.Day())
case OneHourOf3600Seconds:
key = strconv.Itoa(t.Minute()*60 + t.Second())
case OneDayOf1440Minutes:
key = strconv.Itoa(t.Hour()*60 + t.Minute())
}
return key
}
// SampleTimestamp will return the sample timestamp for the given time.
func (r BasicResolution) SampleTimestamp(t time.Time) time.Time {
var ts time.Time
switch r {
case OneMinuteOf60Seconds:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location())
case OneHourOf60Minutes:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
case OneDayOf24Hours:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
case OneMonthOfUpTo31Days:
ts = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
case OneHourOf3600Seconds:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location())
case OneDayOf1440Minutes:
ts = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
}
return ts
}
// SampleTimestamps will return a list sample timestamps for the given time range.
func (r BasicResolution) SampleTimestamps(first, last time.Time) []time.Time {
firstSample := r.SampleTimestamp(first)
curSample := firstSample
list := make([]time.Time, 0)
for curSample.Before(last) || curSample.Equal(last) {
list = append(list, curSample)
switch r {
case OneMinuteOf60Seconds:
curSample = curSample.Add(1 * time.Second)
case OneHourOf60Minutes:
curSample = curSample.Add(1 * time.Minute)
case OneDayOf24Hours:
curSample = curSample.Add(1 * time.Hour)
case OneMonthOfUpTo31Days:
curSample = curSample.AddDate(0, 0, 1)
case OneHourOf3600Seconds:
curSample = curSample.Add(1 * time.Second)
case OneDayOf1440Minutes:
curSample = curSample.Add(1 * time.Minute)
}
}
return list
}