/
time_window_avg.go
56 lines (48 loc) · 1.33 KB
/
time_window_avg.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
package data
import (
"time"
)
// TimeWindowAverager accumulates points, and averages them on a fixed time
// period and outputs the average/min/max, etc as a point
type TimeWindowAverager struct {
start time.Time
windowLen time.Duration
total float64
count int
callBack func(Point)
pointType string
pointTime time.Time
}
// NewTimeWindowAverager initializes and returns an averager
func NewTimeWindowAverager(windowLen time.Duration, callBack func(Point), pointType string) *TimeWindowAverager {
return &TimeWindowAverager{
windowLen: windowLen,
callBack: callBack,
pointType: pointType,
}
}
// NewPoint takes a point, and if the time window expired, it calls
// the callback function with the a new point which is avg of
// all points since start time.
func (twa *TimeWindowAverager) NewPoint(s Point) {
// avg point timestamp is set to last point time
if s.Time.After(twa.pointTime) {
twa.pointTime = s.Time
}
// update statistical values.
twa.total += s.Value
twa.count++
// if time has expired, callback() with avg point
if time.Since(twa.start) >= twa.windowLen {
avgPoint := Point{
Type: twa.pointType,
Time: twa.pointTime,
Value: twa.total / float64(twa.count),
}
twa.callBack(avgPoint)
// reset statistical values and timestamp
twa.total = 0
twa.count = 0
twa.start = time.Now()
}
}