forked from infinilabs/framework
/
simple.go
166 lines (133 loc) · 3.23 KB
/
simple.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package stats
import (
"encoding/json"
log "github.com/cihub/seelog"
"github.com/infinitbyte/framework/core/api"
. "github.com/infinitbyte/framework/core/config"
"github.com/infinitbyte/framework/core/env"
"github.com/infinitbyte/framework/core/global"
"github.com/infinitbyte/framework/core/stats"
"github.com/infinitbyte/framework/core/util"
"os"
"path"
"runtime"
"strings"
"sync"
)
func (module SimpleStatsModule) Name() string {
return "Stats"
}
var data *Stats
var dataPath string
var config *SimpleStatsConfig
type SimpleStatsConfig struct {
Persist bool `config:"persist"`
}
func (module SimpleStatsModule) Setup(cfg *Config) {
config = &SimpleStatsConfig{}
env.ParseConfig("stats", config)
if config.Persist {
dataPath = path.Join(global.Env().GetWorkingDir(), "stats")
os.MkdirAll(dataPath, 0777)
}
data = &Stats{}
data.initStats("simple")
stats.Register(data)
//register api
api.HandleAPIFunc("/stats", module.StatsAction)
}
func (module SimpleStatsModule) Start() error {
return nil
}
func (module SimpleStatsModule) Stop() error {
if config.Persist {
data.l.Lock()
defer data.l.Unlock()
v, _ := json.Marshal(data.Data)
_, err := util.FilePutContentWithByte(path.Join(dataPath, strings.ToLower(data.ID)), v)
if err != nil {
log.Error(err)
}
log.Trace("save stats db,", data.ID)
}
return nil
}
type SimpleStatsModule struct {
config *SimpleStatsConfig
api.Handler
}
type Stats struct {
l sync.RWMutex
ID string `storm:"id,unique" json:"id" gorm:"not null;unique;primary_key"`
Data *map[string]map[string]int64 `storm:"inline" json:"data,omitempty"`
}
func (s *Stats) initData(category, key string) {
s.l.Lock()
defer s.l.Unlock()
_, ok := (*s.Data)[category]
if !ok {
(*s.Data)[category] = make(map[string]int64)
}
_, ok1 := (*s.Data)[category][key]
if !ok1 {
(*s.Data)[category][key] = 0
}
runtime.Gosched()
}
func (s *Stats) Increment(category, key string) {
s.IncrementBy(category, key, 1)
}
func (s *Stats) IncrementBy(category, key string, value int64) {
s.initData(category, key)
s.l.Lock()
(*s.Data)[category][key] += value
s.l.Unlock()
runtime.Gosched()
}
func (s *Stats) Decrement(category, key string) {
s.DecrementBy(category, key, 1)
}
func (s *Stats) DecrementBy(category, key string, value int64) {
s.initData(category, key)
s.l.Lock()
(*s.Data)[category][key] -= value
s.l.Unlock()
runtime.Gosched()
}
func (s *Stats) Timing(category, key string, v int64) {
}
func (s *Stats) Gauge(category, key string, v int64) {
}
func (s *Stats) Stat(category, key string) int64 {
s.initData(category, key)
s.l.RLock()
v := ((*s.Data)[category][key])
s.l.RUnlock()
return v
}
func (s *Stats) StatsAll() *[]byte {
s.l.RLock()
defer s.l.RUnlock()
b, _ := json.MarshalIndent((*s.Data), "", " ")
return &b
}
func (s *Stats) initStats(id string) {
s.l.Lock()
defer s.l.Unlock()
s.ID = id
if config.Persist {
v, err := util.FileGetContent(path.Join(dataPath, strings.ToLower(data.ID)))
if err == nil && v != nil {
d := map[string]map[string]int64{}
err = json.Unmarshal(v, &d)
if err != nil {
log.Error(err)
}
s.Data = &d
}
}
if s.Data == nil {
s.Data = &map[string]map[string]int64{}
log.Trace("inited stats map")
}
}