blob: d74f957aecfaf5dc7fcf7d581e31e1bffebf5ee1 [file] [log] [blame]
Robin Thellendac59e972014-08-19 18:26:11 -07001package stats
2
3import (
4 "sync"
5 "time"
6)
7
8// NewFloat creates a new Float StatsObject with the given name and
9// returns a pointer to it.
10func 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.
21type Float struct {
22 mu sync.RWMutex
23 lastUpdate time.Time
24 value float64
25}
26
27// Set sets the value of the object.
28func (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.
36func (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 Thellende6dcc5d2014-09-17 15:48:00 -070044func (f *Float) LastUpdate() time.Time {
Robin Thellendac59e972014-08-19 18:26:11 -070045 f.mu.RLock()
46 defer f.mu.RUnlock()
47 return f.lastUpdate
48}
49
50// Value returns the current value of the object.
Robin Thellende6dcc5d2014-09-17 15:48:00 -070051func (f *Float) Value() interface{} {
Robin Thellendac59e972014-08-19 18:26:11 -070052 f.mu.RLock()
53 defer f.mu.RUnlock()
54 return f.value
55}