-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
244 lines (191 loc) · 6.64 KB
/
config.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"fmt"
"log"
"time"
"sync"
"strings"
"net/http"
"encoding/json"
)
type Feature struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Persistent bool `json:"persistent"`
Enabled bool `json:"enabled"`
Expire int `json:"expire"`
ShareStrategy *ShareStrategy `json:"shareStrategy,omitempty"`
FirstStrategy *FirstStrategy `json:"firstStrategy,omitempty"`
QueryStrategy *QueryStrategy `json:"queryStrategy,omitempty"`
HeaderStrategy *HeaderStrategy `json:"headerStrategy,omitempty"`
IPStrategy *IPStrategy `json:"ipStrategy,omitempty"`
RefererStrategy *RefererStrategy `json:"refererStrategy,omitempty"`
RetargetStrategy *RetargetStrategy `json:"retargetStrategy,omitempty"`
UserRecurrencyStrategy *UserRecurrencyStrategy `json:"userRecurrencyStrategy,omitempty"`
Stats map[string]int64 `json:"-"`
}
func (self *Feature) Toggle(res http.ResponseWriter, req *http.Request) bool {
if self.Enabled == false {
return false
}
if self.ShareStrategy != nil {
if self.ShareStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.FirstStrategy != nil {
if self.FirstStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.QueryStrategy != nil {
if self.QueryStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.HeaderStrategy != nil {
if self.HeaderStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.IPStrategy != nil {
if self.IPStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.RefererStrategy != nil {
if self.RefererStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.RetargetStrategy != nil {
if self.RetargetStrategy.Toggle(self, res, req) == false {
return false
}
}
if self.UserRecurrencyStrategy != nil {
if self.UserRecurrencyStrategy.Toggle(self, res, req) == false {
return false
}
}
return true
}
func (self *Feature) SetStat(stat string, val int64) {
if self.Stats == nil {
self.Stats = make(map[string]int64)
}
self.Stats[stat] = val
}
func (self *Feature) GetStat(stat string) int64 {
if val, ok := self.Stats[stat]; ok {
return val
}
return 0
}
var mutex *sync.Mutex
var features []Feature;
func getFeatures() []Feature {
return features
}
func setFeatures(_features []Feature) []Feature {
features = _features
return features
}
func saveFeaturesToRedis() {
featureBytes, _ := json.Marshal(features)
featureJson := string(featureBytes[:])
log.Print("Saving featureuration to redis")
log.Print(featureJson)
client := getRedisClient()
err := client.Set("toogles_features", featureJson, 0).Err()
if err != nil {
log.Fatal(err)
}
}
func loadFeaturesFromRedis() {
client := getRedisClient()
data, err := client.Get("toogles_features").Result()
if err = json.Unmarshal([]byte(data), &features); err != nil {
log.Print("Could not find features in redis")
return
}
log.Print("Loaded features from redis")
log.Print(data)
}
var FeatureStatsTypeMap = make(map[string]map[string]int64)
func IncrFeatureStatsType(featureId string, _type string) {
mutex.Lock()
if _, ok := FeatureStatsTypeMap[featureId]; ok == false {
FeatureStatsTypeMap[featureId] = make(map[string]int64)
}
if _, ok := FeatureStatsTypeMap[featureId][_type]; ok == false {
FeatureStatsTypeMap[featureId][_type] = 0
}
FeatureStatsTypeMap[featureId][_type]++
mutex.Unlock()
}
func syncStatsRedis() {
client := getRedisClient()
t := time.Now()
mutex.Lock()
for id, statsMap := range FeatureStatsTypeMap {
for statType, statVal := range statsMap {
totalKey := fmt.Sprintf("toogles_features_%s_stats_%s_total", id, statType)
client.IncrBy(totalKey, statVal)
map_key := fmt.Sprintf("toogles_features_%s_stats_historic_%s", id, statType)
minutesKey := fmt.Sprintf("%d_%d_%d_%d_%d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute())
client.HIncrBy(map_key + "_minutes", minutesKey, statVal)
hoursKey := fmt.Sprintf("%d_%d_%d_%d", t.Year(), t.Month(), t.Day(), t.Hour())
client.HIncrBy(map_key + "_hours", hoursKey, statVal)
daysKey := fmt.Sprintf("%d_%d_%d", t.Year(), t.Month(), t.Day())
client.HIncrBy(map_key + "_days", daysKey, statVal)
}
}
FeatureStatsTypeMap = make(map[string]map[string]int64)
for i, _ := range features {
feature := &features[i]
if feature.Stats == nil {
feature.Stats = make(map[string]int64)
}
pattern := fmt.Sprintf("toogles_features_%s_stats_", feature.Id)
keys, err := client.Keys(fmt.Sprintf("toogles_features_%s_stats_*", feature.Id)).Result()
if err != nil {
log.Print(err)
continue
}
for _, key := range keys {
value, err := client.Get(key).Int64()
if err != nil {
continue
}
//Users_total => [Users total]
keyType := strings.Split(strings.Replace(key, pattern, "", -1), "_")
feature.SetStat(keyType[0], value)
}
}
mutex.Unlock()
}
type FeatureStats struct {
FeatureId string `json:"featureId,omitempty"`
Stats map[string]int64 `json:"stats"`
}
func getFeatureStats(feature Feature) FeatureStats {
return FeatureStats {
FeatureId: feature.Id,
Stats: feature.Stats,
}
}
func deleteFeatureStatsFromRedis(featureId string) {
client := getRedisClient()
keys, err := client.Keys(fmt.Sprintf("toogles_features_%s_stats_", featureId) + "*").Result()
if err != nil {
return
}
for _, key := range keys {
err := client.Del(key).Err()
if err != nil {
continue
}
}
}