/
metric.go
102 lines (86 loc) · 2.04 KB
/
metric.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
package metrics
import (
"fmt"
)
// StateSet is the state of the metrics.
// ie Pod Phase, Deployment Status or CronJob Suspended state.
type StateSet map[string]map[string]int
// MetricSet is the metric set.
type MetricSet struct {
Items map[string]*Metric
}
// NewMetricSet creates a new metric set.
func NewMetricSet() *MetricSet {
return &MetricSet{
Items: map[string]*Metric{},
}
}
// Metric represents an individual metric.
type Metric struct {
Name string `json:"name"`
Value int `json:"value"`
Type string `json:"type"`
Labels map[string]string `json:"labels"`
}
// Increment the metric.
func (s *MetricSet) Increment(kind, namespace string, phase string) {
key := fmt.Sprintf("%s-%s-%s", kind, namespace, phase)
if metric, found := s.Items[key]; found {
metric.Value++
} else {
metric := &Metric{
Name: key,
Value: 1,
Labels: map[string]string{
dimensionKind: kind,
dimensionNamespace: namespace,
dimensionState: string(phase),
},
}
s.Items[key] = metric
}
}
// CombineRecords will combine two metric sets.
func CombineRecords(recordsInput *MetricSet, recordsAppend *MetricSet) *MetricSet {
output := NewMetricSet()
for _, record := range recordsInput.Items {
output.Items[record.Name] = record
}
for _, record := range recordsAppend.Items {
if output.Items[record.Name] == nil {
output.Items[record.Name] = record
} else {
output.Items[record.Name].Value++
}
}
return output
}
// CombineStates will combine two state sets.
func CombineStates(input *StateSet, og *StateSet) StateSet {
output := StateSet{}
for i, v := range *input {
for a, b := range v {
if output[i] == nil {
output[i] = make(map[string]int)
}
if output[i][a] == 0 {
output[i][a] = b
} else {
output[i][a] += b
}
}
}
for i, v := range *og {
for a, b := range v {
if output[i] == nil {
output[i] = make(map[string]int)
}
if output[i][a] == 0 {
output[i][a] = b
} else {
output[i][a] += b
}
}
}
return output
}