/
statistic.go
94 lines (85 loc) · 2.07 KB
/
statistic.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
package statistics
import (
"time"
)
// A basic statistic
type Statistic struct {
// The statistic name
name string `json:"-"`
// The timestamp of the last operation
Timestamp int64 `json:"timestamp"`
// the current value
Value int64 `json:"value"`
// the number of updates to this statistic during the sample period
Count int64 `json:"count"`
// The minimum value during the sample period
Min int64 `json:"min"`
// The maximum value during the sample period
Max int64 `json:"max"`
// The average value during the sample period
Ave int64 `json:"average"`
// The sum of all values during the sample period
Sum int64 `json:"sum"`
// Historic data, max 72 entries at 5 minute intervals
History []*Statistic `json:"history,omitempty"`
// The last 5 minutes data, used to build the history
lastFive []*Statistic `json:"-"`
latest *Statistic `json:"-"`
}
func (s *Statistic) reset() {
s.Timestamp = time.Now().Unix()
s.Value = 0
s.Count = 0
s.Min = int64(^uint64(0) >> 1)
s.Max = -s.Min - 1
s.Ave = 0
s.Sum = 0
}
func (s *Statistic) clone() *Statistic {
var r *Statistic = new(Statistic)
r.name = s.name
r.Timestamp = s.Timestamp
r.Value = s.Value
r.Count = s.Count
r.Min = s.Min
r.Max = s.Max
r.Ave = s.Ave
r.Sum = s.Sum
return r
}
func statsCopyArray( s []*Statistic ) []*Statistic {
var a []*Statistic
if len( s ) > 0 {
for _, v := range s {
a = append( a, v.clone() )
}
}
return a
}
func (s *Statistic) update() {
s.Timestamp = time.Now().Unix()
if( s.Value < s.Min ) {
s.Min = s.Value
}
if( s.Value > s.Max ) {
s.Max = s.Value
}
// protect against /0 - incase count is reset for some reason
if( s.Count != 0 && s.Sum != 0 ) {
s.Ave = s.Sum / s.Count
} else {
s.Ave = 0
}
}
func (s *Statistic) set( v int64 ) {
s.Value = v
s.Sum += v
s.Count ++
s.update()
}
func (s *Statistic) incr( v int64 ) {
s.Value += v
s.Sum += v
s.Count ++
s.update()
}