2015-03-12 22:22:48 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
2015-04-22 21:37:59 +00:00
|
|
|
"fmt"
|
2015-04-22 22:03:37 +00:00
|
|
|
"sort"
|
2015-03-12 22:22:48 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2015-05-30 14:14:10 +00:00
|
|
|
// Stats represents a collection of metrics as key-value pairs.
|
2015-03-12 22:22:48 +00:00
|
|
|
type Stats struct {
|
2015-05-30 14:14:10 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
name string
|
|
|
|
values map[string]int64
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// NewStats returns a Stats object with the given name.
|
2015-03-13 21:14:23 +00:00
|
|
|
func NewStats(name string) *Stats {
|
2015-03-12 22:22:48 +00:00
|
|
|
return &Stats{
|
2015-05-30 14:14:10 +00:00
|
|
|
name: name,
|
|
|
|
values: make(map[string]int64),
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 14:14:10 +00:00
|
|
|
// Name returns the name of the Stats object.
|
|
|
|
func (s *Stats) Name() string { return s.name }
|
2015-03-13 05:16:30 +00:00
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// Get returns a value for a given key.
|
2015-03-12 22:22:48 +00:00
|
|
|
func (s *Stats) Get(key string) int64 {
|
|
|
|
s.mu.RLock()
|
2015-05-30 14:14:10 +00:00
|
|
|
v := s.values[key]
|
|
|
|
s.mu.RUnlock()
|
|
|
|
return v
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 23:17:22 +00:00
|
|
|
// Set sets a value for the given key.
|
2015-03-12 22:22:48 +00:00
|
|
|
func (s *Stats) Set(key string, v int64) {
|
|
|
|
s.mu.Lock()
|
2015-05-30 14:14:10 +00:00
|
|
|
s.values[key] = v
|
|
|
|
s.mu.Unlock()
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 14:14:10 +00:00
|
|
|
// Add adds delta to the stat indiciated by key.
|
|
|
|
func (s *Stats) Add(key string, delta int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.values[key] += delta
|
|
|
|
s.mu.Unlock()
|
2015-03-13 21:14:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 14:14:10 +00:00
|
|
|
// Inc simply increments the given key by 1.
|
|
|
|
func (s *Stats) Inc(key string) { s.Add(key, 1) }
|
|
|
|
|
|
|
|
// Walk calls f for each entry in the stats.
|
|
|
|
func (s *Stats) Walk(fn func(string, int64)) {
|
2015-03-12 22:22:48 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2015-05-30 14:14:10 +00:00
|
|
|
for k, v := range s.values {
|
|
|
|
fn(k, v)
|
2015-03-12 22:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 14:14:10 +00:00
|
|
|
// Diff returns the difference between two sets of stats.
|
|
|
|
// The result is undefined if the two Stats objects do not contain the same keys.
|
2015-03-12 22:22:48 +00:00
|
|
|
func (s *Stats) Diff(other *Stats) *Stats {
|
2015-03-13 21:14:23 +00:00
|
|
|
diff := NewStats(s.name)
|
2015-03-12 22:22:48 +00:00
|
|
|
s.Walk(func(k string, v int64) {
|
|
|
|
diff.Set(k, v-other.Get(k))
|
|
|
|
})
|
|
|
|
return diff
|
|
|
|
}
|
2015-03-13 22:01:53 +00:00
|
|
|
|
2015-05-30 14:14:10 +00:00
|
|
|
// Clone returns a copy of the stats object.
|
|
|
|
func (s *Stats) Clone() *Stats {
|
|
|
|
other := NewStats(s.name)
|
2015-03-13 22:01:53 +00:00
|
|
|
s.Walk(func(k string, v int64) {
|
2015-05-30 14:14:10 +00:00
|
|
|
other.Set(k, s.values[k])
|
2015-03-13 22:01:53 +00:00
|
|
|
})
|
2015-05-30 14:14:10 +00:00
|
|
|
return other
|
2015-03-13 22:01:53 +00:00
|
|
|
}
|
2015-04-22 21:37:59 +00:00
|
|
|
|
|
|
|
func (s *Stats) String() string {
|
2015-05-30 14:14:10 +00:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
|
2015-04-22 21:37:59 +00:00
|
|
|
var out string
|
2015-04-22 22:03:37 +00:00
|
|
|
var keys []string
|
2015-05-30 14:14:10 +00:00
|
|
|
for k, _ := range s.values {
|
2015-04-22 22:03:37 +00:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2015-05-30 14:14:10 +00:00
|
|
|
out += `{"` + s.name + `":[`
|
|
|
|
|
2015-04-22 21:37:59 +00:00
|
|
|
var j int
|
2015-04-22 22:03:37 +00:00
|
|
|
for _, k := range keys {
|
2015-05-30 14:14:10 +00:00
|
|
|
out += fmt.Sprintf(`{"%s":%d}`, k, s.values[k])
|
2015-04-22 21:37:59 +00:00
|
|
|
j++
|
2015-04-22 22:03:37 +00:00
|
|
|
if j != len(keys) {
|
2015-04-22 21:37:59 +00:00
|
|
|
out += `,`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out += `]}`
|
|
|
|
return out
|
|
|
|
}
|