Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 1 | package stats |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // NewFloat creates a new Float StatsObject with the given name and |
| 9 | // returns a pointer to it. |
| 10 | func NewFloat(name string) *Float { |
| 11 | lock.Lock() |
| 12 | defer lock.Unlock() |
| 13 | |
| 14 | node := findNodeLocked(name, true) |
| 15 | f := Float{value: 0} |
| 16 | node.object = &f |
| 17 | return &f |
| 18 | } |
| 19 | |
| 20 | // Float implements the StatsObject interface. |
| 21 | type Float struct { |
| 22 | mu sync.RWMutex |
| 23 | lastUpdate time.Time |
| 24 | value float64 |
| 25 | } |
| 26 | |
| 27 | // Set sets the value of the object. |
| 28 | func (f *Float) Set(value float64) { |
| 29 | f.mu.Lock() |
| 30 | defer f.mu.Unlock() |
| 31 | f.lastUpdate = time.Now() |
| 32 | f.value = value |
| 33 | } |
| 34 | |
| 35 | // Incr increments the value of the object. |
| 36 | func (f *Float) Incr(delta float64) { |
| 37 | f.mu.Lock() |
| 38 | defer f.mu.Unlock() |
| 39 | f.value += delta |
| 40 | f.lastUpdate = time.Now() |
| 41 | } |
| 42 | |
| 43 | // LastUpdate returns the time at which the object was last updated. |
Robin Thellend | e6dcc5d | 2014-09-17 15:48:00 -0700 | [diff] [blame] | 44 | func (f *Float) LastUpdate() time.Time { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 45 | f.mu.RLock() |
| 46 | defer f.mu.RUnlock() |
| 47 | return f.lastUpdate |
| 48 | } |
| 49 | |
| 50 | // Value returns the current value of the object. |
Robin Thellend | e6dcc5d | 2014-09-17 15:48:00 -0700 | [diff] [blame] | 51 | func (f *Float) Value() interface{} { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 52 | f.mu.RLock() |
| 53 | defer f.mu.RUnlock() |
| 54 | return f.value |
| 55 | } |