-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathtype.go
426 lines (383 loc) · 9.46 KB
/
type.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package aggregation
import (
"fmt"
"strconv"
"strings"
"github.com/m3db/m3/src/metrics/generated/proto/aggregationpb"
"github.com/m3db/m3x/pool"
)
// Supported aggregation types.
const (
UnknownType Type = iota
Last
Min
Max
Mean
Median
Count
Sum
SumSq
Stdev
P10
P20
P30
P40
P50
P60
P70
P80
P90
P95
P99
P999
P9999
nextTypeID = iota
)
const (
// maxTypeID is the largest id of all the valid aggregation types.
// NB(cw) maxTypeID is guaranteed to be greater or equal
// to len(ValidTypes).
// Iff ids of all the valid aggregation types are consecutive,
// maxTypeID == len(ValidTypes).
maxTypeID = nextTypeID - 1
typesSeparator = ","
)
var (
emptyStruct struct{}
// DefaultTypes is a default list of aggregation types.
DefaultTypes Types
// ValidTypes is the list of all the valid aggregation types.
ValidTypes = map[Type]struct{}{
Last: emptyStruct,
Min: emptyStruct,
Max: emptyStruct,
Mean: emptyStruct,
Median: emptyStruct,
Count: emptyStruct,
Sum: emptyStruct,
SumSq: emptyStruct,
Stdev: emptyStruct,
P10: emptyStruct,
P20: emptyStruct,
P30: emptyStruct,
P40: emptyStruct,
P50: emptyStruct,
P60: emptyStruct,
P70: emptyStruct,
P80: emptyStruct,
P90: emptyStruct,
P95: emptyStruct,
P99: emptyStruct,
P999: emptyStruct,
P9999: emptyStruct,
}
typeStringMap map[string]Type
)
// Type defines an aggregation function.
type Type int
// NewTypeFromProto creates an aggregation type from a proto.
func NewTypeFromProto(input aggregationpb.AggregationType) (Type, error) {
aggType := Type(input)
if !aggType.IsValid() {
return UnknownType, fmt.Errorf("invalid aggregation type from proto: %s", input)
}
return aggType, nil
}
// ID returns the id of the Type.
func (a Type) ID() int {
return int(a)
}
// IsValid checks if an Type is valid.
func (a Type) IsValid() bool {
_, ok := ValidTypes[a]
return ok
}
// IsValidForGauge if an Type is valid for Gauge.
func (a Type) IsValidForGauge() bool {
switch a {
case Last, Min, Max, Mean, Count, Sum, SumSq, Stdev:
return true
default:
return false
}
}
// IsValidForCounter if an Type is valid for Counter.
func (a Type) IsValidForCounter() bool {
switch a {
case Min, Max, Mean, Count, Sum, SumSq, Stdev:
return true
default:
return false
}
}
// IsValidForTimer if an Type is valid for Timer.
func (a Type) IsValidForTimer() bool {
switch a {
case Last:
return false
default:
return true
}
}
// Quantile returns the quantile represented by the Type.
func (a Type) Quantile() (float64, bool) {
switch a {
case P10:
return 0.1, true
case P20:
return 0.2, true
case P30:
return 0.3, true
case P40:
return 0.4, true
case P50, Median:
return 0.5, true
case P60:
return 0.6, true
case P70:
return 0.7, true
case P80:
return 0.8, true
case P90:
return 0.9, true
case P95:
return 0.95, true
case P99:
return 0.99, true
case P999:
return 0.999, true
case P9999:
return 0.9999, true
default:
return 0, false
}
}
// Proto returns the proto of the aggregation type.
func (a Type) Proto() (aggregationpb.AggregationType, error) {
s := aggregationpb.AggregationType(a)
if err := validateProtoType(s); err != nil {
return aggregationpb.AggregationType_UNKNOWN, err
}
return s, nil
}
// MarshalJSON returns the JSON encoding of an aggregation type.
func (a Type) MarshalJSON() ([]byte, error) {
if !a.IsValid() {
return nil, fmt.Errorf("invalid aggregation type %s", a.String())
}
marshalled := strconv.Quote(a.String())
return []byte(marshalled), nil
}
// UnmarshalJSON unmarshals JSON-encoded data into an aggregation type.
func (a *Type) UnmarshalJSON(data []byte) error {
str := string(data)
unquoted, err := strconv.Unquote(str)
if err != nil {
return err
}
parsed, err := ParseType(unquoted)
if err != nil {
return err
}
*a = parsed
return nil
}
// UnmarshalYAML unmarshals aggregation type from a string.
func (a *Type) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}
parsed, err := ParseType(str)
if err != nil {
return err
}
*a = parsed
return nil
}
func validateProtoType(a aggregationpb.AggregationType) error {
_, ok := aggregationpb.AggregationType_name[int32(a)]
if !ok {
return fmt.Errorf("invalid proto aggregation type: %v", a)
}
return nil
}
// ParseType parses an aggregation type.
func ParseType(str string) (Type, error) {
var (
aggType Type
exactMatch bool
looseMatch bool
)
aggType, exactMatch = typeStringMap[str]
if !exactMatch {
for key, val := range typeStringMap {
if strings.ToLower(key) == strings.ToLower(str) {
looseMatch = true
aggType = val
break
}
}
}
if !exactMatch && !looseMatch {
return UnknownType, fmt.Errorf("invalid aggregation type: %s", str)
}
return aggType, nil
}
// Types is a list of Types.
type Types []Type
// NewTypesFromProto creates a list of aggregation types from a proto.
func NewTypesFromProto(input []aggregationpb.AggregationType) (Types, error) {
res := make([]Type, len(input))
for i, t := range input {
aggType, err := NewTypeFromProto(t)
if err != nil {
return DefaultTypes, err
}
res[i] = aggType
}
return res, nil
}
// Contains checks if the given type is contained in the aggregation types.
func (aggTypes Types) Contains(aggType Type) bool {
for _, at := range aggTypes {
if at == aggType {
return true
}
}
return false
}
// IsDefault checks if the Types is the default aggregation type.
func (aggTypes Types) IsDefault() bool {
return len(aggTypes) == 0
}
// String returns the string representation of the list of aggregation types.
func (aggTypes Types) String() string {
if len(aggTypes) == 0 {
return ""
}
parts := make([]string, len(aggTypes))
for i, aggType := range aggTypes {
parts[i] = aggType.String()
}
return strings.Join(parts, typesSeparator)
}
// IsValidForGauge checks if the list of aggregation types is valid for Gauge.
func (aggTypes Types) IsValidForGauge() bool {
for _, aggType := range aggTypes {
if !aggType.IsValidForGauge() {
return false
}
}
return true
}
// IsValidForCounter checks if the list of aggregation types is valid for Counter.
func (aggTypes Types) IsValidForCounter() bool {
for _, aggType := range aggTypes {
if !aggType.IsValidForCounter() {
return false
}
}
return true
}
// IsValidForTimer checks if the list of aggregation types is valid for Timer.
func (aggTypes Types) IsValidForTimer() bool {
for _, aggType := range aggTypes {
if !aggType.IsValidForTimer() {
return false
}
}
return true
}
// PooledQuantiles returns all the quantiles found in the list
// of aggregation types. Using a floats pool if available.
//
// A boolean will also be returned to indicate whether the
// returned float slice is from the pool.
func (aggTypes Types) PooledQuantiles(p pool.FloatsPool) ([]float64, bool) {
var (
res []float64
initialized bool
medianAdded bool
pooled bool
)
for _, aggType := range aggTypes {
q, ok := aggType.Quantile()
if !ok {
continue
}
// Dedup P50 and Median.
if aggType == P50 || aggType == Median {
if medianAdded {
continue
}
medianAdded = true
}
if !initialized {
if p == nil {
res = make([]float64, 0, len(aggTypes))
} else {
res = p.Get(len(aggTypes))
pooled = true
}
initialized = true
}
res = append(res, q)
}
return res, pooled
}
// Proto returns the proto of the aggregation types.
func (aggTypes Types) Proto() ([]aggregationpb.AggregationType, error) {
// This is the same as returning an empty slice from the functionality perspective.
// It makes creating testing fixtures much simpler.
if aggTypes == nil {
return nil, nil
}
res := make([]aggregationpb.AggregationType, len(aggTypes))
for i, aggType := range aggTypes {
s, err := aggType.Proto()
if err != nil {
return nil, err
}
res[i] = s
}
return res, nil
}
// ParseTypes parses a list of aggregation types in the form of type1,type2,type3.
func ParseTypes(str string) (Types, error) {
parts := strings.Split(str, typesSeparator)
res := make(Types, len(parts))
for i := range parts {
aggType, err := ParseType(parts[i])
if err != nil {
return nil, err
}
res[i] = aggType
}
return res, nil
}
func init() {
typeStringMap = make(map[string]Type, maxTypeID)
for aggType := range ValidTypes {
typeStringMap[aggType.String()] = aggType
}
}