forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accumulator.go
267 lines (231 loc) · 6.2 KB
/
accumulator.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package testutil
import (
"fmt"
"reflect"
"sync"
"time"
)
// Point defines a single point measurement
type Point struct {
Measurement string
Tags map[string]string
Fields map[string]interface{}
Time time.Time
}
func (p *Point) String() string {
return fmt.Sprintf("%s %v", p.Measurement, p.Fields)
}
// Accumulator defines a mocked out accumulator
type Accumulator struct {
sync.Mutex
Points []*Point
}
// Add adds a measurement point to the accumulator
func (a *Accumulator) Add(
measurement string,
value interface{},
tags map[string]string,
t ...time.Time,
) {
fields := map[string]interface{}{"value": value}
a.AddFields(measurement, fields, tags, t...)
}
// AddFields adds a measurement point with a specified timestamp.
func (a *Accumulator) AddFields(
measurement string,
fields map[string]interface{},
tags map[string]string,
timestamp ...time.Time,
) {
a.Lock()
defer a.Unlock()
if tags == nil {
tags = map[string]string{}
}
var t time.Time
if len(timestamp) > 0 {
t = timestamp[0]
} else {
t = time.Now()
}
p := &Point{
Measurement: measurement,
Fields: fields,
Tags: tags,
Time: t,
}
a.Points = append(
a.Points,
p,
)
}
func (a *Accumulator) SetDefaultTags(tags map[string]string) {
// stub for implementing Accumulator interface.
}
func (a *Accumulator) AddDefaultTag(key, value string) {
// stub for implementing Accumulator interface.
}
func (a *Accumulator) Prefix() string {
// stub for implementing Accumulator interface.
return ""
}
func (a *Accumulator) SetPrefix(prefix string) {
// stub for implementing Accumulator interface.
}
func (a *Accumulator) Debug() bool {
// stub for implementing Accumulator interface.
return true
}
func (a *Accumulator) SetDebug(debug bool) {
// stub for implementing Accumulator interface.
}
// Get gets the specified measurement point from the accumulator
func (a *Accumulator) Get(measurement string) (*Point, bool) {
for _, p := range a.Points {
if p.Measurement == measurement {
return p, true
}
}
return nil, false
}
// CheckValue calls CheckFieldsValue passing a single-value map as fields
func (a *Accumulator) CheckValue(measurement string, val interface{}) bool {
return a.CheckFieldsValue(measurement, map[string]interface{}{"value": val})
}
// CheckValue checks that the accumulators point for the given measurement
// is the same as the given value.
func (a *Accumulator) CheckFieldsValue(measurement string, fields map[string]interface{}) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
if reflect.DeepEqual(fields, p.Fields) {
return true
} else {
fmt.Printf("Measurement %s Failure, expected: %v, got %v\n",
measurement, fields, p.Fields)
return false
}
}
}
fmt.Printf("Measurement %s, fields %s not found\n", measurement, fields)
return false
}
// CheckTaggedValue calls ValidateTaggedValue
func (a *Accumulator) CheckTaggedValue(
measurement string,
val interface{},
tags map[string]string,
) bool {
return a.ValidateTaggedValue(measurement, val, tags) == nil
}
// ValidateTaggedValue calls ValidateTaggedFieldsValue passing a single-value map as fields
func (a *Accumulator) ValidateTaggedValue(
measurement string,
val interface{},
tags map[string]string,
) error {
return a.ValidateTaggedFieldsValue(measurement, map[string]interface{}{"value": val}, tags)
}
// ValidateValue calls ValidateTaggedValue
func (a *Accumulator) ValidateValue(measurement string, val interface{}) error {
return a.ValidateTaggedValue(measurement, val, nil)
}
// CheckTaggedFieldsValue calls ValidateTaggedFieldsValue
func (a *Accumulator) CheckTaggedFieldsValue(
measurement string,
fields map[string]interface{},
tags map[string]string,
) bool {
return a.ValidateTaggedFieldsValue(measurement, fields, tags) == nil
}
// ValidateTaggedValue validates that the given measurement and value exist
// in the accumulator and with the given tags.
func (a *Accumulator) ValidateTaggedFieldsValue(
measurement string,
fields map[string]interface{},
tags map[string]string,
) error {
if tags == nil {
tags = map[string]string{}
}
for _, p := range a.Points {
if !reflect.DeepEqual(tags, p.Tags) {
continue
}
if p.Measurement == measurement {
if !reflect.DeepEqual(fields, p.Fields) {
return fmt.Errorf("%v != %v ", fields, p.Fields)
}
return nil
}
}
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
}
// ValidateFieldsValue calls ValidateTaggedFieldsValue
func (a *Accumulator) ValidateFieldsValue(
measurement string,
fields map[string]interface{},
) error {
return a.ValidateTaggedValue(measurement, fields, nil)
}
func (a *Accumulator) ValidateTaggedFields(
measurement string,
fields map[string]interface{},
tags map[string]string,
) error {
if tags == nil {
tags = map[string]string{}
}
for _, p := range a.Points {
if !reflect.DeepEqual(tags, p.Tags) {
continue
}
if p.Measurement == measurement {
if !reflect.DeepEqual(fields, p.Fields) {
return fmt.Errorf("%v (%T) != %v (%T)",
p.Fields, p.Fields, fields, fields)
}
return nil
}
}
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
}
// HasIntValue returns true if the measurement has an Int value
func (a *Accumulator) HasIntValue(measurement string) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
_, ok := p.Fields["value"].(int64)
return ok
}
}
return false
}
// HasUIntValue returns true if the measurement has a UInt value
func (a *Accumulator) HasUIntValue(measurement string) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
_, ok := p.Fields["value"].(uint64)
return ok
}
}
return false
}
// HasFloatValue returns true if the given measurement has a float value
func (a *Accumulator) HasFloatValue(measurement string) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
_, ok := p.Fields["value"].(float64)
return ok
}
}
return false
}
// HasMeasurement returns true if the accumulator has a measurement with the
// given name
func (a *Accumulator) HasMeasurement(measurement string) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
return true
}
}
return false
}