-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_gauge.go
61 lines (49 loc) · 1.34 KB
/
point_gauge.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
package rolling
var _ Metric = &pointGauge{}
var _ Aggregation = &pointGauge{}
// PointGauge represents a ring window.
// Every buckets within the window contains one point.
// When the window is full, the earliest point will be overwrite.
type PointGauge interface {
Aggregation
Metric
// Reduce applies the reduction function to all buckets within the window.
Reduce(func(Iterator) float64) float64
}
// PointGaugeOpts contains the arguments for creating PointGauge.
type PointGaugeOpts struct {
// Size represents the bucket size within the window.
Size int
}
type pointGauge struct {
policy *PointPolicy
}
// NewPointGauge creates a new PointGauge based on PointGaugeOpts.
func NewPointGauge(opts PointGaugeOpts) PointGauge {
window := NewWindow(WindowOpts{Size: opts.Size})
policy := NewPointPolicy(window)
return &pointGauge{
policy: policy,
}
}
func (r *pointGauge) Add(val int64) {
r.policy.Append(float64(val))
}
func (r *pointGauge) Reduce(f func(Iterator) float64) float64 {
return r.policy.Reduce(f)
}
func (r *pointGauge) Avg() float64 {
return r.policy.Reduce(Avg)
}
func (r *pointGauge) Min() float64 {
return r.policy.Reduce(Min)
}
func (r *pointGauge) Max() float64 {
return r.policy.Reduce(Max)
}
func (r *pointGauge) Sum() float64 {
return r.policy.Reduce(Sum)
}
func (r *pointGauge) Value() int64 {
return int64(r.Sum())
}