blob: b8dce27cfe2ffcd0134737b2eb1b69bd33bbdc97 [file] [log] [blame]
Robin Thellendac59e972014-08-19 18:26:11 -07001package stats
2
3import (
4 "time"
5
Jiri Simsa519c5072014-09-17 21:37:57 -07006 "veyron.io/veyron/veyron/lib/stats/counter"
Robin Thellendac59e972014-08-19 18:26:11 -07007)
8
9// NewCounter creates a new Counter StatsObject with the given name and
10// returns a pointer to it.
11func NewCounter(name string) *counter.Counter {
12 lock.Lock()
13 defer lock.Unlock()
14
15 node := findNodeLocked(name, true)
16 c := counter.New()
17 cw := &counterWrapper{c}
18 node.object = cw
19
20 addCounterChild(node, name+"/delta1h", cw, time.Hour, cw.Delta1h)
21 addCounterChild(node, name+"/delta10m", cw, 10*time.Minute, cw.Delta10m)
22 addCounterChild(node, name+"/delta1m", cw, time.Minute, cw.Delta1m)
23 addCounterChild(node, name+"/rate1h", cw, time.Hour, cw.Rate1h)
24 addCounterChild(node, name+"/rate10m", cw, 10*time.Minute, cw.Rate10m)
25 addCounterChild(node, name+"/rate1m", cw, time.Minute, cw.Rate1m)
26 return c
27}
28
29type counterWrapper struct {
30 c *counter.Counter
31}
32
33func (cw counterWrapper) LastUpdate() time.Time {
34 return cw.c.LastUpdate()
35}
36
37func (cw counterWrapper) Value() interface{} {
38 return cw.c.Value()
39}
40
41func (cw counterWrapper) Delta1h() interface{} {
42 return cw.c.Delta1h()
43}
44func (cw counterWrapper) Delta10m() interface{} {
45 return cw.c.Delta10m()
46}
47func (cw counterWrapper) Delta1m() interface{} {
48 return cw.c.Delta1m()
49}
50func (cw counterWrapper) Rate1h() interface{} {
51 return cw.c.Rate1h()
52}
53func (cw counterWrapper) Rate10m() interface{} {
54 return cw.c.Rate10m()
55}
56func (cw counterWrapper) Rate1m() interface{} {
57 return cw.c.Rate1m()
58}
59
60type counterChild struct {
61 c *counterWrapper
62 period time.Duration
63 value func() interface{}
64}
65
66func (cc counterChild) LastUpdate() time.Time {
67 now := time.Now()
68 if t := cc.c.LastUpdate().Add(cc.period); t.Before(now) {
69 return t
70 }
71 return now
72}
73
74func (cc counterChild) Value() interface{} {
75 return cc.value()
76}
77
78func addCounterChild(parent *node, name string, c *counterWrapper, period time.Duration, value func() interface{}) {
79 child := findNodeLocked(name, true)
80 child.object = &counterChild{c, period, value}
81}