-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-range.go
51 lines (45 loc) · 1.38 KB
/
time-range.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
package main
import (
"fmt"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"time"
)
// initRange generate Range for data based on config value Days back
func initRange() v1.Range {
return initRangeFromTo(time.Now().AddDate(0, 0, -config.Days).Truncate(time.Hour*24), time.Now())
}
// initRangeFromTo generate range for limit data not for day back
func initRangeFromTo(from time.Time, to time.Time) v1.Range {
return v1.Range{
Start: from.UTC().Add(time.Minute * -timeRangeOverSize).Truncate(time.Minute),
End: to.UTC().Add(time.Minute * timeRangeOverSize).Truncate(time.Minute),
Step: time.Duration(config.Step) * time.Second,
}
}
// generateRangeTable Split required range to range array
func generateRangeTable(r v1.Range) []v1.Range {
var ret []v1.Range
startTime := r.Start
endTime := startTime
for r.End.After(endTime.Add(2 * time.Second)) {
endTime = startTime.Add(6 * time.Hour)
if endTime.After(r.End) {
endTime = r.End
}
r := v1.Range{
Start: startTime,
End: endTime,
Step: time.Duration(config.Step) * time.Second,
}
ret = append(ret, r)
startTime = endTime
}
return ret
}
func printTimeRanges(r []v1.Range) string {
a := fmt.Sprintln("Range data list:")
for _, v := range r {
a = fmt.Sprintf("%s [%s - %s / %d second]\r\n", a, v.Start.Format(time.RFC3339), v.End.Format(time.RFC3339), v.Step/1000000000)
}
return a
}