-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
metrics.resolvers.go
130 lines (98 loc) · 2.75 KB
/
metrics.resolvers.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
package resolver
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"time"
"github.com/datarhei/core/http/graph/models"
"github.com/datarhei/core/http/graph/scalars"
"github.com/datarhei/core/monitor/metric"
)
func (r *queryResolver) Metrics(ctx context.Context, query models.MetricsInput) (*models.Metrics, error) {
patterns := []metric.Pattern{}
for _, m := range query.Metrics {
labels := []string{}
for k, v := range m.Labels {
switch v.(type) {
case string:
default:
continue
}
labels = append(labels, k, v.(string))
}
pattern := metric.NewPattern(m.Name, labels...)
patterns = append(patterns, pattern)
}
response := &models.Metrics{
Metrics: []*models.Metric{},
}
series := make(map[string]*models.Metric)
var timeframe int64 = 0
var interval int64 = 0
if query.TimerangeSeconds != nil {
timeframe = int64(*query.TimerangeSeconds)
}
if query.IntervalSeconds != nil {
interval = int64(*query.IntervalSeconds)
}
if timeframe == 0 {
// current data
now := time.Now()
data := r.Monitor.Collect(patterns)
for _, v := range data.All() {
hash := v.Hash()
if _, ok := series[hash]; !ok {
series[hash] = &models.Metric{
Name: v.Name(),
Labels: map[string]interface{}{},
Values: []*scalars.MetricsResponseValue{},
}
}
k := series[hash]
for lk, lv := range v.Labels() {
k.Labels[lk] = lv
}
k.Values = append(k.Values, &scalars.MetricsResponseValue{
TS: now,
Value: v.Val(),
})
series[hash] = k
}
} else {
// historic data
data := r.Monitor.History(time.Second*time.Duration(timeframe), time.Second*time.Duration(interval), patterns)
for _, d := range data {
if d.Metrics == nil {
continue
}
for _, v := range d.Metrics.All() {
hash := v.Hash()
if _, ok := series[hash]; !ok {
series[hash] = &models.Metric{
Name: v.Name(),
Labels: map[string]interface{}{},
Values: []*scalars.MetricsResponseValue{},
}
}
k := series[hash]
for lk, lv := range v.Labels() {
k.Labels[lk] = lv
}
k.Values = append(k.Values, &scalars.MetricsResponseValue{
TS: d.TS,
Value: v.Val(),
})
series[hash] = k
}
}
}
for _, metric := range series {
response.Metrics = append(response.Metrics, metric)
}
resolutionTimerange, resolutionInterval := r.Monitor.Resolution()
resolutionTimerangeInt := int(resolutionTimerange.Seconds())
resolutionIntervalInt := int(resolutionInterval.Seconds())
response.TimerangeSeconds = &resolutionTimerangeInt
response.IntervalSeconds = &resolutionIntervalInt
return response, nil
}