-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
131 lines (115 loc) · 3.27 KB
/
common.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
package common
import (
"fmt"
"github.com/kadaan/promutil/lib/downsample"
"github.com/kadaan/promutil/lib/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/promql"
"math"
"net/url"
"path"
"sort"
"strconv"
"strings"
"time"
)
var (
minTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()
minTimeFormatted = minTime.Format(time.RFC3339Nano)
maxTimeFormatted = maxTime.Format(time.RFC3339Nano)
)
func FormatDateRange(start int64, end int64) string {
return fmt.Sprintf("%s to %s", FormatDate(start), FormatDate(end))
}
func FormatDate(value int64) string {
return time.UnixMilli(value).Format("2006-01-02T15:04:05")
}
func LabelProtosToLabels(labelPairs []prompb.Label) labels.Labels {
result := make(labels.Labels, 0, len(labelPairs))
for _, l := range labelPairs {
result = append(result, labels.Label{
Name: l.Name,
Value: l.Value,
})
}
sort.Sort(result)
return result
}
func DownsampleMatrix(matrix promql.Matrix, maxSamples int, avg bool) promql.Matrix {
var newMatrix promql.Matrix
downsampler := downsample.NewLttbDownsampler()
for _, series := range matrix {
series.Points = downsampler.Downsample(series.Points, maxSamples)
newMatrix = append(newMatrix, series)
}
return newMatrix
}
func ParseTime(s string) (time.Time, error) {
if t, err := strconv.ParseFloat(s, 64); err == nil {
s, ns := math.Modf(t)
ns = math.Round(ns*1000) / 1000
return time.Unix(int64(s), int64(ns*float64(time.Second))).UTC(), nil
}
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
return t, nil
}
// Stdlib's time parser can only handle 4 digit years. As a workaround until
// that is fixed we want to at least support our own boundary times.
// Context: https://github.com/prometheus/client_golang/issues/614
// Upstream issue: https://github.com/golang/go/issues/20555
switch s {
case minTimeFormatted:
return minTime, nil
case maxTimeFormatted:
return maxTime, nil
}
return time.Time{}, errors.New("cannot parse %q to a valid timestamp", s)
}
func ParseDuration(s string) (time.Duration, error) {
if d, err := strconv.ParseFloat(s, 64); err == nil {
ts := d * float64(time.Second)
if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) {
return 0, errors.Errorf("cannot parse %q to a valid duration. It overflows int64", s)
}
return time.Duration(ts), nil
}
if d, err := model.ParseDuration(s); err == nil {
return time.Duration(d), nil
}
return 0, errors.Errorf("cannot parse %q to a valid duration", s)
}
func TimeMilliseconds(t model.Time) int64 {
return t.UnixNano() / int64(time.Millisecond/time.Nanosecond)
}
func JoinUrl(base *url.URL, paths ...string) (*url.URL, error) {
s := base.String()
p := path.Join(paths...)
return url.Parse(fmt.Sprintf("%s/%s", strings.TrimRight(s, "/"), strings.TrimLeft(p, "/")))
}
func MaxInt64(x int64, y int64) int64 {
if x > y {
return x
}
return y
}
func MinInt64(x int64, y int64) int64 {
if x < y {
return x
}
return y
}
func MaxUInt8(x uint8, y uint8) uint8 {
if x > y {
return x
}
return y
}
func MinTime(x time.Time, y time.Time) time.Time {
if x.After(y) {
return y
}
return x
}