-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_avg_aggregator.go
81 lines (73 loc) · 2.04 KB
/
time_avg_aggregator.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
package estream
import (
"math"
)
// TimeAvgAggregator struct represents aggregator calculating average value of
// the process over the time
type TimeAvgAggregator struct {
getValue func(interface{}) int64
initialized bool
startTime int64
startValue int64
endTime int64
endValue int64
integral int64
}
// NewTimeAvgAggregator creates a new TimeAvgAggregator and returns a
// reference to it. Argument is a function that accepts an event as the
// argument and returns the numeric value for this event.
func NewTimeAvgAggregator(getValue func(interface{}) int64) *TimeAvgAggregator {
return &TimeAvgAggregator{getValue: getValue}
}
// Enter is called when a new event enters window
func (c *TimeAvgAggregator) Enter(e Event, w Window) {
val := c.getValue(e.Value)
if !c.initialized {
c.startTime = e.Timestamp
c.startValue = val
c.initialized = true
}
c.endTime = e.Timestamp
c.endValue = val
}
// Leave is called when an event leaves window
func (c *TimeAvgAggregator) Leave(e Event, w Window) {
if len(w.Events) > 0 && w.Events[0].Timestamp == w.StartTime {
c.startValue = c.getValue(w.Events[0].Value)
} else {
c.startValue = c.getValue(e.Value)
}
}
// Reset is called for batch aggregators when the window is full
func (c *TimeAvgAggregator) Reset(w Window) {
c.integral = 0
if c.initialized {
c.startTime = w.StartTime
c.startValue = c.endValue
c.endTime = w.EndTime
}
}
// TimeChange is called when time for the window has changed
func (c *TimeAvgAggregator) TimeChange(w Window) {
if !c.initialized {
return
}
if c.startTime < w.StartTime {
c.integral -= c.startValue * (w.StartTime - c.startTime)
c.startTime = w.StartTime
}
if c.endTime < w.EndTime {
c.integral += c.endValue * (w.EndTime - c.endTime)
c.endTime = w.EndTime
}
}
// Mean returns average value of the process in the window
func (c *TimeAvgAggregator) Mean() float64 {
if !c.initialized {
return math.NaN()
}
if c.startTime == c.endTime {
return float64(c.endValue)
}
return float64(c.integral) / float64(c.endTime-c.startTime)
}