This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
/
multilevel.go
133 lines (118 loc) · 2.54 KB
/
multilevel.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
package tally
import (
"fmt"
"time"
)
type CountBucket struct {
value float64
timestamp time.Time
next *CountBucket
}
func (b *CountBucket) String() string {
var next string
if b.next != nil {
next = "->" + b.next.String()
}
return fmt.Sprintf("%v%s", *b, next)
}
type CountLevel struct {
Current float64
interval time.Duration
top, bottom, ready *CountBucket
}
func (lvl *CountLevel) Reset() {
lvl.Current = 0
if lvl.top != nil {
lvl.bottom.next = lvl.ready
lvl.ready = lvl.top.next
lvl.top.next = nil
lvl.bottom = lvl.top
}
}
func (lvl *CountLevel) Count(value float64) {
lvl.Current += value
lvl.bottom.value += value
}
func (lvl *CountLevel) Duration() time.Duration {
return time.Since(lvl.top.timestamp)
}
func (lvl *CountLevel) RatePer(unit time.Duration) float64 {
return lvl.Current / (lvl.Duration().Seconds() / unit.Seconds())
}
func (lvl *CountLevel) NewBucket() {
var b *CountBucket
if lvl.ready == nil {
b = new(CountBucket)
} else {
b = lvl.ready
lvl.ready = lvl.ready.next
}
if lvl.top == nil {
lvl.top = b
} else {
lvl.bottom.next = b
}
lvl.bottom = b
b.value = 0
b.timestamp = time.Now()
b.next = nil
}
type MultilevelCount []CountLevel
func (mc MultilevelCount) Reset() {
for i, _ := range mc {
mc[i].Reset()
}
}
func (mc MultilevelCount) Count(value float64) {
for i, _ := range mc {
mc[i].Count(value)
}
}
func (mc MultilevelCount) Total() float64 {
return mc[len(mc)-1].Current
}
func (mc MultilevelCount) Rollup() {
if len(mc) == 0 {
return
}
current := &mc[0]
if current.interval == time.Duration(0) && len(mc) < 2 {
// special case to simplify testing, doesn't change functionality
return
}
remainder := mc[1:]
now := time.Now()
total := 0.0
var lastRemoved *CountBucket
b := current.top
for b != nil && now.Sub(b.timestamp) >= current.interval {
total += b.value
lastRemoved = b
b = b.next
}
if lastRemoved != nil {
t := lastRemoved.next
current.Current -= total
lastRemoved.next = current.ready
current.ready = current.top
current.top = t
if current.top == nil {
current.bottom = nil
}
remainder.Rollup()
}
current.NewBucket()
}
func NewMultilevelCount(intervals ...time.Duration) *MultilevelCount {
count := make(MultilevelCount, len(intervals)+1)
count[0].NewBucket()
for i, interval := range intervals {
count[i+1].interval = interval
count[i+1].NewBucket()
}
if len(intervals) == 0 {
// special case to simplify testing, doesn't change functionality
count[0].top.timestamp = time.Unix(0, 0)
}
return &count
}