forked from openark/orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregated.go
76 lines (65 loc) · 2.04 KB
/
aggregated.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
// Package query provdes query metrics with this file providing
// aggregared metrics based on the underlying values.
package query
import (
"time"
"github.com/montanaflynn/stats"
"github.com/github/orchestrator/go/collection"
)
type AggregatedQueryMetrics struct {
// fill me in here
Count int
MaxLatencySeconds float64
MeanLatencySeconds float64
MedianLatencySeconds float64
P95LatencySeconds float64
MaxWaitSeconds float64
MeanWaitSeconds float64
MedianWaitSeconds float64
P95WaitSeconds float64
}
// AggregatedSince returns the aggregated query metrics for the period
// given from the values provided.
func AggregatedSince(c *collection.Collection, t time.Time) AggregatedQueryMetrics {
// Initialise timing metrics
var waitTimings []float64
var queryTimings []float64
// Retrieve values since the time specified
values, err := c.Since(t)
a := AggregatedQueryMetrics{}
if err != nil {
return a // empty data
}
// generate the metrics
for _, v := range values {
waitTimings = append(waitTimings, v.(*Metric).WaitLatency.Seconds())
queryTimings = append(queryTimings, v.(*Metric).ExecuteLatency.Seconds())
}
a.Count = len(waitTimings)
// generate aggregate values
if s, err := stats.Max(stats.Float64Data(waitTimings)); err == nil {
a.MaxWaitSeconds = s
}
if s, err := stats.Mean(stats.Float64Data(waitTimings)); err == nil {
a.MeanWaitSeconds = s
}
if s, err := stats.Median(stats.Float64Data(waitTimings)); err == nil {
a.MedianWaitSeconds = s
}
if s, err := stats.Percentile(stats.Float64Data(waitTimings), 95); err == nil {
a.P95WaitSeconds = s
}
if s, err := stats.Max(stats.Float64Data(queryTimings)); err == nil {
a.MaxLatencySeconds = s
}
if s, err := stats.Mean(stats.Float64Data(queryTimings)); err == nil {
a.MeanLatencySeconds = s
}
if s, err := stats.Median(stats.Float64Data(queryTimings)); err == nil {
a.MedianLatencySeconds = s
}
if s, err := stats.Percentile(stats.Float64Data(queryTimings), 95); err == nil {
a.P95LatencySeconds = s
}
return a
}