-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
105 lines (93 loc) · 2.98 KB
/
aggregate.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
package quantile
import (
"fmt"
"math"
"sort"
"github.com/bitly/go-simplejson"
)
type E2eProcessingLatencyAggregate struct {
Count int `json:"count"`
Percentiles []map[string]float64 `json:"percentiles"`
Topic string `json:"topic"`
Channel string `json:"channel"`
Addr string `json:"host"`
}
func (e *E2eProcessingLatencyAggregate) Target(key string) ([]string, string) {
targets := make([]string, 0, len(e.Percentiles))
var target string
for _, percentile := range e.Percentiles {
if e.Channel != "" {
target = fmt.Sprintf(`%%stopic.%s.channel.%s.%s_%.0f`, e.Topic, e.Channel, key, percentile["quantile"]*100.0)
} else {
target = fmt.Sprintf(`%%stopic.%s.%s_%.0f`, e.Topic, key, percentile["quantile"]*100.0)
}
if e.Addr == "*" {
target = fmt.Sprintf(`averageSeries(%s)`, target)
}
target = fmt.Sprintf(`scale(%s,0.000001)`, target)
targets = append(targets, target)
}
return targets, ""
}
func (e *E2eProcessingLatencyAggregate) Host() string {
return e.Addr
}
func E2eProcessingLatencyAggregateFromJSON(j *simplejson.Json, topic, channel, host string) *E2eProcessingLatencyAggregate {
count := j.Get("count").MustInt()
rawPercentiles := j.Get("percentiles")
numPercentiles := len(rawPercentiles.MustArray())
percentiles := make([]map[string]float64, numPercentiles)
for i := 0; i < numPercentiles; i++ {
v := rawPercentiles.GetIndex(i)
n := v.Get("value").MustFloat64()
q := v.Get("quantile").MustFloat64()
percentiles[i] = make(map[string]float64)
percentiles[i]["min"] = n
percentiles[i]["max"] = n
percentiles[i]["average"] = n
percentiles[i]["quantile"] = q
percentiles[i]["count"] = float64(count)
}
return &E2eProcessingLatencyAggregate{
Count: count,
Percentiles: percentiles,
Topic: topic,
Channel: channel,
Addr: host,
}
}
func (e *E2eProcessingLatencyAggregate) Len() int { return len(e.Percentiles) }
func (e *E2eProcessingLatencyAggregate) Swap(i, j int) {
e.Percentiles[i], e.Percentiles[j] = e.Percentiles[j], e.Percentiles[i]
}
func (e *E2eProcessingLatencyAggregate) Less(i, j int) bool {
return e.Percentiles[i]["percentile"] > e.Percentiles[j]["percentile"]
}
// Add merges e2 into e by averaging the percentiles
func (e *E2eProcessingLatencyAggregate) Add(e2 *E2eProcessingLatencyAggregate) {
e.Addr = "*"
p := e.Percentiles
e.Count += e2.Count
for _, value := range e2.Percentiles {
i := -1
for j, v := range p {
if value["quantile"] == v["quantile"] {
i = j
break
}
}
if i == -1 {
i = len(p)
e.Percentiles = append(p, make(map[string]float64))
p = e.Percentiles
p[i]["quantile"] = value["quantile"]
}
p[i]["max"] = math.Max(value["max"], p[i]["max"])
p[i]["min"] = math.Min(value["max"], p[i]["max"])
p[i]["count"] += value["count"]
delta := value["average"] - p[i]["average"]
R := delta * value["count"] / p[i]["count"]
p[i]["average"] = p[i]["average"] + R
}
sort.Sort(e)
}