Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 1 | package stats |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 6 | "veyron.io/veyron/veyron/lib/stats/counter" |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // NewCounter creates a new Counter StatsObject with the given name and |
| 10 | // returns a pointer to it. |
| 11 | func 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 | |
| 29 | type counterWrapper struct { |
| 30 | c *counter.Counter |
| 31 | } |
| 32 | |
| 33 | func (cw counterWrapper) LastUpdate() time.Time { |
| 34 | return cw.c.LastUpdate() |
| 35 | } |
| 36 | |
| 37 | func (cw counterWrapper) Value() interface{} { |
| 38 | return cw.c.Value() |
| 39 | } |
| 40 | |
| 41 | func (cw counterWrapper) Delta1h() interface{} { |
| 42 | return cw.c.Delta1h() |
| 43 | } |
| 44 | func (cw counterWrapper) Delta10m() interface{} { |
| 45 | return cw.c.Delta10m() |
| 46 | } |
| 47 | func (cw counterWrapper) Delta1m() interface{} { |
| 48 | return cw.c.Delta1m() |
| 49 | } |
| 50 | func (cw counterWrapper) Rate1h() interface{} { |
| 51 | return cw.c.Rate1h() |
| 52 | } |
| 53 | func (cw counterWrapper) Rate10m() interface{} { |
| 54 | return cw.c.Rate10m() |
| 55 | } |
| 56 | func (cw counterWrapper) Rate1m() interface{} { |
| 57 | return cw.c.Rate1m() |
| 58 | } |
| 59 | |
| 60 | type counterChild struct { |
| 61 | c *counterWrapper |
| 62 | period time.Duration |
| 63 | value func() interface{} |
| 64 | } |
| 65 | |
| 66 | func (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 | |
| 74 | func (cc counterChild) Value() interface{} { |
| 75 | return cc.value() |
| 76 | } |
| 77 | |
| 78 | func 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 | } |