-
Notifications
You must be signed in to change notification settings - Fork 5
/
stats.go
694 lines (567 loc) · 14 KB
/
stats.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
package lib
import (
"fmt"
"sort"
"strconv"
"sync"
)
const NotSupported = "N/S"
const NotAvailable = "N/A"
type Stats map[string]interface{}
// NewStats returns a new stat
func NewStats() Stats {
return make(Stats)
}
func ToStats(inMap interface{}) Stats {
outMap := Stats{}
switch val := inMap.(type) {
case map[string]Stats:
for k, v := range val {
outMap[k] = v
}
case map[string]map[string]Stats:
for k1, mv1 := range val {
outMv := Stats{}
for k2, v2 := range mv1 {
outMv[k2] = v2
}
outMap[k1] = outMv
}
}
return outMap
}
func ToString(sv interface{}) (string, error) {
switch v := sv.(type) {
case string:
return v, nil
case int64:
return strconv.FormatInt(v, 10), nil
case int:
return strconv.FormatInt(int64(v), 10), nil
case uint64:
return strconv.FormatUint(v, 10), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(v), nil
default:
return "", fmt.Errorf("invalid value type")
}
}
func (s Stats) Len() int {
return len(s)
}
func (s Stats) Clone() Stats {
res := make(Stats, len(s))
for k, v := range s {
res[k] = v
}
return res
}
func (s Stats) FindKeysPath(del string, keys ...string) map[string][]string {
paths := map[string][]string{}
for _, key := range keys {
l := s.FindKeyPath(del, key)
paths[key] = l
}
return paths
}
func (s Stats) FindKeyPath(del, key string) []string {
var pathList []string
if del == "" {
del = "/"
}
for skey, sval := range s {
if key == skey {
pathList = append(pathList, key)
} else if v, ok := sval.(Stats); ok {
paths := v.FindKeyPath(del, key)
for _, path := range paths {
p := skey + del + path
pathList = append(pathList, p)
}
}
}
return pathList
}
// AggregateStats Value should be a float64 or a convertible string
// this function never panics
func (s Stats) AggregateStats(other Stats) {
for k, v := range other {
if val := addValues(s[k], v); val != nil {
s[k] = val
}
}
}
func (s Stats) ToStringValues() map[string]interface{} {
res := make(map[string]interface{}, len(s))
for k, v := range s {
sv, err := ToString(v)
if err != nil {
res[k] = sv
} else {
res[k] = v
}
}
return res
}
func (s Stats) Get(name string, aliases ...string) interface{} {
if val, exists := s[name]; exists {
return val
}
for _, alias := range aliases {
if val, exists := s[alias]; exists {
return val
}
}
return nil
}
func (s Stats) ExistsGet(name string) (interface{}, bool) {
val, exists := s[name]
return val, exists
}
func (s Stats) GetMulti(names ...string) Stats {
res := make(Stats, len(names))
for _, name := range names {
if val, exists := s[name]; exists {
res[name] = val
} else {
res[name] = NotAvailable
}
}
return res
}
func (s Stats) Del(names ...string) {
for _, name := range names {
delete(s, name)
}
}
// TryInt - Value should be an int64 or a convertible string; otherwise defValue is returned
// this function never panics
func (s Stats) TryInt(name string, defValue int64, aliases ...string) int64 {
field := s.Get(name, aliases...)
if field != nil {
if value, ok := field.(int64); ok {
return value
}
if value, ok := field.(float64); ok {
return int64(value)
}
}
return defValue
}
// Int returns in64 value of a field after asserting value is an int64 and
// exists otherwise panics
func (s Stats) Int(name string, aliases ...string) int64 {
field := s.Get(name, aliases...)
return field.(int64)
}
// TryFloat returns float64 value of a field after asserting it is float64 or a
// convertible string otherwise defValue is returned.
func (s Stats) TryFloat(
name string, defValue float64, aliases ...string,
) float64 {
field := s.Get(name, aliases...)
if field != nil {
if value, ok := field.(float64); ok {
return value
}
if value, ok := field.(int64); ok {
return float64(value)
}
}
return defValue
}
// TryString Value should be an int64 or a convertible string; otherwise
// defValue is returned
// this function never panics
func (s Stats) TryString(
name string, defValue string, aliases ...string,
) string {
field := s.Get(name, aliases...)
if field != nil {
if value, ok := field.(string); ok {
return value
}
}
return defValue
}
// TryStringP value should be an int64 or a convertible string; otherwise defValue is returned
// this function never panics
func (s Stats) TryStringP(
name string, defValue string, aliases ...string,
) *string {
field := s.Get(name, aliases...)
if field != nil {
s := field.(string)
return &s
}
return &defValue
}
func (s Stats) TryList(name string, aliases ...string) []string {
field := s.Get(name, aliases...)
if field != nil {
if value, ok := field.([]string); ok {
return value
}
}
return nil
}
func (s Stats) TryStats(name string, aliases ...string) Stats {
field := s.Get(name, aliases...)
if field != nil {
if value, ok := field.(Stats); ok {
return value
}
}
return nil
}
func (s Stats) TryStatsList(name string, aliases ...string) []Stats {
field := s.Get(name, aliases...)
if field != nil {
if value, ok := field.([]Stats); ok {
return value
}
}
return nil
}
func (s Stats) Flatten(sep string) Stats {
res := make(Stats, len(s))
for k, v := range s {
switch v := v.(type) {
case map[string]interface{}:
for k2, v2 := range Stats(v).Flatten(sep) {
res[k+sep+k2] = v2
}
case Stats:
for k2, v2 := range v.Flatten(sep) {
res[k+sep+k2] = v2
}
//FIXME:
case []interface{}:
for _, listElem := range v {
switch listElem := listElem.(type) {
case string:
res[k] = v
default:
for k2, v2 := range listElem.(Stats).Flatten(sep) {
res[k+sep+"["+listElem.(Stats)["name"].(string)+"]"+sep+k2] = v2
}
}
}
case []Stats:
for _, listElem := range v {
for k2, v2 := range listElem.Flatten(sep) {
res[k+sep+"["+listElem["name"].(string)+"]"+sep+k2] = v2
}
}
default:
res[k] = v
}
}
return res
}
// ToParsedValues Type should be map[string]interface{} otherwise same map is
// returned. info_parser needs parsed values
func (s Stats) ToParsedValues() map[string]interface{} {
res := make(map[string]interface{}, len(s))
for k, val := range s {
valStr, ok := val.(string)
if !ok {
res[k] = val
continue
}
if value, err := strconv.ParseInt(valStr, 10, 64); err == nil {
res[k] = value
} else if value, err := strconv.ParseFloat(valStr, 64); err == nil {
res[k] = value
} else if value, err := strconv.ParseBool(valStr); err == nil {
res[k] = value
} else if _, err := strconv.ParseUint(valStr, 10, 64); err == nil {
// this uint can not fit in int. uint numbers should not be put in tsdb. There may be few config/stats which
// are initialized with biggest uint. Put may be as zero but not as string.
res[k] = 0
} else {
res[k] = valStr
}
}
return res
}
// GetInnerVal will give inner map from a nested map, not the base field.
// By using this we can get the map at any level. if this map is the
// lowermost then there are other TryString type calls which can be
// used to further get any specific type of base field
func (s Stats) GetInnerVal(keys ...string) Stats {
temp := s
for _, k := range keys {
if val, ok := temp[k]; ok {
if temp, ok = val.(Stats); !ok {
return nil
}
} else {
return nil
}
}
return temp
}
type SyncStats struct {
_Stats Stats
mutex sync.RWMutex
}
func NewSyncStats(stats Stats) *SyncStats {
return &SyncStats{
_Stats: stats,
}
}
func (s *SyncStats) SetStats(info Stats) {
s.mutex.Lock()
defer s.mutex.Unlock()
s._Stats = info
}
func (s *SyncStats) Set(name string, value interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
s._Stats[name] = value
}
func (s *SyncStats) Clone() Stats {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.Clone()
}
func (s *SyncStats) Exists(name string) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
_, exists := s._Stats[name]
return exists
}
func (s *SyncStats) CloneInto(res Stats) {
s.mutex.RLock()
defer s.mutex.RUnlock()
for k, v := range s._Stats {
res[k] = v
}
}
func (s *SyncStats) Get(name string, aliases ...string) interface{} {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.Get(name, aliases...)
}
func (s *SyncStats) ExistsGet(name string) (interface{}, bool) {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.ExistsGet(name)
}
func (s *SyncStats) GetMulti(names ...string) Stats {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.GetMulti(names...)
}
func (s *SyncStats) Del(names ...string) {
s.mutex.RLock()
defer s.mutex.RUnlock()
s._Stats.Del(names...)
}
// Int Value MUST exist, and MUST be an int64 or a convertible string.
// Panics if the above constraints are not met
func (s *SyncStats) Int(name string, aliases ...string) int64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.Int(name, aliases...)
}
// TryInt Value should be an int64 or a convertible string; otherwise defValue is returned
// this function never panics
func (s *SyncStats) TryInt(
name string, defValue int64, aliases ...string,
) int64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.TryInt(name, defValue, aliases...)
}
// TryFloat Value should be a float64 or a convertible string; otherwise
// defValue is/returned this function never panics
func (s *SyncStats) TryFloat(
name string, defValue float64, aliases ...string,
) float64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.TryFloat(name, defValue, aliases...)
}
// TryString Value should be a string; otherwise defValue is returned this function never panics
func (s *SyncStats) TryString(
name string, defValue string, aliases ...string,
) string {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s._Stats.TryString(name, defValue, aliases...)
}
// AggregateStatsTo Value should be a float64 or a convertible string
// this function never panics
func (s *SyncStats) AggregateStatsTo(other Stats) {
s.mutex.RLock()
defer s.mutex.RUnlock()
other.AggregateStats(s._Stats)
}
/*
Utility functions
*/
// StatsBy is the type of "less" function that defines the ordering of its Stats arguments.
type StatsBy func(fieldName string, p1, p2 Stats) bool
var ByFloatField = func(fieldName string, p1, p2 Stats) bool {
return p1.TryFloat(fieldName, 0) < p2.TryFloat(fieldName, 0)
}
var ByIntField = func(fieldName string, p1, p2 Stats) bool {
return p1.TryInt(fieldName, 0) < p2.TryInt(fieldName, 0)
}
var ByStringField = func(fieldName string, p1, p2 Stats) bool {
return p1.TryString(fieldName, "") < p2.TryString(fieldName, "")
}
func (by StatsBy) Sort(fieldName string, statsList []Stats) {
ps := &statsSorter{
fieldName: fieldName,
statsList: statsList,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ps)
}
func (by StatsBy) SortReverse(fieldName string, statsList []Stats) {
ps := &statsSorter{
fieldName: fieldName,
statsList: statsList,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(sort.Reverse(ps))
}
// statsSorter joins a StatsBy function and a slice of statsList to be sorted.
type statsSorter struct {
by func(fieldName string, p1, p2 Stats) bool // Closure used in the Less method.
fieldName string
statsList []Stats
}
// Len is part of sort.Interface.
func (s *statsSorter) Len() int {
return len(s.statsList)
}
// Swap is part of sort.Interface.
func (s *statsSorter) Swap(i, j int) {
s.statsList[i], s.statsList[j] = s.statsList[j], s.statsList[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *statsSorter) Less(i, j int) bool {
return s.by(s.fieldName, s.statsList[i], s.statsList[j])
}
func addValues(v1, v2 interface{}) interface{} {
// If v1 is nil then also it should copy data from v2
// else multilayers stats will not work if v1 is nil
if v1S, ok := v1.(Stats); ok || v1 == nil {
if v2S, ok := v2.(Stats); ok {
res := Stats{}
res.AggregateStats(v1S)
res.AggregateStats(v2S)
return res
}
}
v1Vali, v1i := v1.(int64)
v2Vali, v2i := v2.(int64)
v1Valf, v1f := v1.(float64)
v2Valf, v2f := v2.(float64)
switch {
case v1i && v2i:
return v1Vali + v2Vali
case v1f && v2f:
return v1Valf + v2Valf
case v1i && v2f:
return float64(v1Vali) + v2Valf
case v1f && v2i:
return v1Valf + float64(v2Vali)
case v2 == nil && (v1i || v1f):
return v1
case v1 == nil && (v2i || v2f):
return v2
}
return nil
}
// GetRaw input is full qualified name
func (s Stats) GetRaw(keys ...string) interface{} {
var d = s
for _, k := range keys {
// TODO: Just (Map) does not work !!!
if d1, ok := d[k].(Stats); ok {
d = d1
continue
}
return d[k]
}
return d
}
// DeepClone CopyMap(map)
func (s Stats) DeepClone() Stats {
var result = make(Stats)
for k := range s {
v := s[k]
switch v := v.(type) {
case Stats:
result[k] = v.DeepClone()
default:
result[k] = v
}
}
return result
}
// GetType(map, key ...)
/*
func (input Stats) GetType(keys ...string) interface{} {
d := input
for _, k := range keys {
if d1, exists := d[k].(Stats); exists {
d = d1
continue
} else {
d = d[k]
break
}
}
return d
}
*/
func ToStatsDeep(input Stats) Stats {
result := make(Stats)
if len(input) == 0 {
return result
}
for k, v := range input {
switch v := v.(type) {
case Stats:
result[k] = ToStatsDeep(v)
case map[string]interface{}:
result[k] = ToStatsDeep(v)
case []Stats:
list := make([]Stats, 0, len(v))
for _, i := range v {
list = append(list, ToStatsDeep(i))
}
result[k] = list
case []map[string]interface{}:
list := make([]Stats, 0, len(v))
for _, i := range v {
list = append(list, ToStatsDeep(i))
}
result[k] = list
case []interface{}:
list := make([]interface{}, 0, len(v))
for _, i := range v {
switch i := i.(type) {
case Stats:
list = append(list, ToStatsDeep(i))
case map[string]interface{}:
list = append(list, ToStatsDeep(i))
default:
list = append(list, i)
}
}
result[k] = list
default:
result[k] = v
}
}
return result
}