This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
template_functions.go
78 lines (73 loc) · 1.88 KB
/
template_functions.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
package out
import (
"log"
"math/rand"
"strings"
)
// random choice between replacing a node with a wildcard, a char with a wildcard, and passthrough
func pattern(in string) string {
mode := rand.Intn(3)
if mode == 0 {
// in this mode, replaces a node with a wildcard
parts := strings.Split(in, ".")
parts[rand.Intn(len(parts))] = "*"
return strings.Join(parts, ".")
} else if mode == 1 {
// randomly replace chars with a *
// note that in 1/5 cases, nothing happens
// and otherwise, sometimes valid patterns are produced,
// but it's also possible to produce patterns that won't match anything (if '.' was taken out)
if len(in) < 5 {
log.Fatalf("metric %q too short for pattern replacement", in)
}
chars := rand.Intn(5)
pos := rand.Intn(len(in) - chars)
return in[0:pos] + "*" + in[pos+chars:]
}
// mode 3: do nothing :)
return in
}
// age returns age compared to time of query
func age(in int64) int64 {
return QueryTime - in
}
// roundDuration rounds a second-specified duration for rough classification
func roundDuration(in int64) int64 {
abs := in
if abs < 0 {
abs = -abs
}
if abs <= 10 { // 10s -> don't round
return in
} else if abs <= 60 { // 1min -> round to 10s
return round(in, 10)
} else if abs <= 600 { // 10min -> round to 1min
return round(in, 60)
} else if abs <= 3600 { // 1h -> round to 10min
return round(in, 600)
} else if abs <= 3600*24 { // 24h -> round to 1h
return round(in, 3600)
} else if abs <= 3600*24*7 { // 7d -> round to 1d
return round(in, 3600*24)
} else if abs <= 3600*24*30 { // 30d -> round to 7d
return round(in, 3600*24*7)
}
// default to rounding to months
return round(in, 3600*24*30)
}
// round rounds number d to the nearest r-boundary
func round(d, r int64) int64 {
neg := d < 0
if neg {
d = -d
}
if m := d % r; m+m < r {
d = d - m
} else {
d = d + r - m
}
if neg {
return -d
}
return d
}