-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplestats.go
55 lines (47 loc) · 1005 Bytes
/
simplestats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package simplestats
import (
"fmt"
"sync"
)
// Stats - struct that wraps all stats
type Stats struct {
data map[string]int64
mutex sync.RWMutex
}
// String - format all stats as single string
func (s *Stats) String() (output string) {
for key, value := range s.data {
output += fmt.Sprintf("%s => %d, ", key, value)
}
return output
}
// New - simple stats
func New() (s *Stats) {
return &Stats{
data: make(map[string]int64),
}
}
// Increment - increment key count by 1
func (s *Stats) Increment(key string) {
s.IncrementBy(key, 1)
}
// IncrementBy - increment key by specified count
func (s *Stats) IncrementBy(key string, value int64) {
s.mutex.Lock()
s.data[key] += value
s.mutex.Unlock()
}
// Get - returns count of key
func (s *Stats) Get(key string) int64 {
s.mutex.Lock()
value := s.data[key]
s.mutex.Unlock()
return value
}
// GetData - returns tracked data
func (s *Stats) GetData() map[string]int64 {
s.mutex.Lock()
data := s.data
s.mutex.Unlock()
return data
}