-
Notifications
You must be signed in to change notification settings - Fork 60
/
abtest.go
263 lines (238 loc) · 5.56 KB
/
abtest.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package uadmin
import (
"fmt"
"net/http"
"strconv"
"sync"
"time"
)
// ABTestType is the type of the AB testing: model or static
type ABTestType int
// Static is used to do AB testing for static assets (images, js, css, ...)
func (ABTestType) Static() ABTestType {
return 1
}
// Model is used to do AB testing for model values coming from database
func (ABTestType) Model() ABTestType {
return 2
}
// ModelList a list of registered models
type ModelList int
// FieldList is a list of fields from schema for a registered model
type FieldList int
var staticABTests map[string][]struct {
v string
vid uint
imp uint
click uint
group string
}
var modelABTests map[string][]struct {
v string
vid uint
fname int
pk uint
imp uint
click uint
group string
}
var abTestsMutex = sync.Mutex{}
var abTestCount = 0
// ABTest is a model that stores an A/B test
type ABTest struct {
Model
Name string `uadmin:"required"`
Type ABTestType `uadmin:"required"`
StaticPath string
ModelName ModelList
Field FieldList
PrimaryKey int
Active bool
Group string
ResetABTest string `uadmin:"link"`
}
// Save an ABTest
func (a *ABTest) Save() {
if a.ResetABTest == "" {
if a.ID == 0 {
Save(a)
}
a.ResetABTest = RootURL + "api/d/abtest/method/Reset/" + fmt.Sprint(a.ID) + "/?$next=$back"
}
Save(a)
abTestCount = Count([]ABTest{}, "`active` = ?", true)
}
func loadModels(a interface{}, u *User) []Choice {
c := []Choice{}
for i, m := range modelList {
c = append(c, Choice{K: uint(i), V: getModelName(m)})
}
return c
}
func loadFields(a interface{}, u *User) []Choice {
m, ok := a.(ABTest)
if !ok {
mp, ok := a.(*ABTest)
if !ok {
Trail(ERROR, "loadFields Unable to cast a to ABTest")
return []Choice{}
}
m = *mp
}
if m.Type != m.Type.Model() {
return []Choice{}
}
s := Schema[getModelName(modelList[int(m.ModelName)])]
c := []Choice{}
for i, f := range s.Fields {
c = append(c, Choice{K: uint(i), V: f.Name})
}
return c
}
func syncABTests() {
// Check if there are stats to save to the DB
abTestsMutex.Lock()
if staticABTests != nil {
tx := db.Begin()
for _, v := range staticABTests {
for i := range v {
if v[i].imp != 0 || v[i].click != 0 {
// store results to DB
tx.Exec("UPDATE ab_test_values SET impressions = impressions + ?, clicks = clicks + ? WHERE id = ?", v[i].imp, v[i].click, v[i].vid)
}
}
}
for _, v := range modelABTests {
for i := range v {
if v[i].imp != 0 || v[i].click != 0 {
// store results to DB
tx.Exec("UPDATE ab_test_values SET impressions = impressions + ?, clicks = clicks + ? WHERE id = ?", v[i].imp, v[i].click, v[i].vid)
}
}
}
tx.Commit()
}
staticABTests = map[string][]struct {
v string
vid uint
imp uint
click uint
group string
}{}
modelABTests = map[string][]struct {
v string
vid uint
fname int
pk uint
imp uint
click uint
group string
}{}
tests := []ABTest{}
Filter(&tests, "`active` = ?", true)
// Process Static AB Tests
for _, t := range tests {
if t.Type != t.Type.Static() {
continue
}
values := []ABTestValue{}
Filter(&values, "ab_test_id = ? AND `active` = ?", t.ID, true)
tempList := []struct {
v string
vid uint
imp uint
click uint
group string
}{}
for _, v := range values {
tempList = append(tempList, struct {
v string
vid uint
imp uint
click uint
group string
}{v: v.Value, vid: v.ID, group: t.Group})
}
staticABTests[t.StaticPath] = tempList
}
// Process Models AB Tests
for _, t := range tests {
if t.Type != t.Type.Model() {
continue
}
schema := Schema[getModelName(modelList[int(t.ModelName)])]
fName := schema.Fields[int(t.Field)].Name
values := []ABTestValue{}
Filter(&values, "ab_test_id = ? AND `active` = ?", t.ID, true)
tempList := []struct {
v string
vid uint
fname int
pk uint
imp uint
click uint
group string
}{}
for _, v := range values {
tempList = append(tempList, struct {
v string
vid uint
fname int
pk uint
imp uint
click uint
group string
}{v: v.Value, vid: v.ID, group: t.Group, pk: uint(t.PrimaryKey), fname: int(t.Field)})
}
modelABTests[schema.ModelName+"__"+fName+"__"+fmt.Sprint(t.PrimaryKey)] = tempList
}
abTestsMutex.Unlock()
}
// ABTestClick is a function to register a click for an ABTest group
func ABTestClick(r *http.Request, group string) {
go func() {
abt := getABT(r)
var index int
abTestsMutex.Lock()
for k, v := range staticABTests {
if len(v) != 0 && v[0].group == group {
index = abt % len(v)
v[index].click++
staticABTests[k] = v
}
}
for k, v := range modelABTests {
if len(v) != 0 && v[0].group == group {
index = abt % len(v)
v[index].click++
modelABTests[k] = v
}
}
abTestsMutex.Unlock()
}()
}
func getABT(r *http.Request) int {
c, err := r.Cookie("abt")
if err != nil || c == nil {
now := time.Now().AddDate(0, 0, 1)
/*http.SetCookie(&http.Cookie{
Name: "abt",
Value: fmt.Sprint(now.Second()),
Path: "/",
Expires: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()),
})
*/
return now.Second()
}
v, _ := strconv.ParseInt(c.Value, 10, 64)
return int(v)
}
// Reset resets the impressions and clicks to 0 based on a specified
// AB Test ID
func (a ABTest) Reset() {
abTestsMutex.Lock()
abtestValue := ABTestValue{}
Update(&abtestValue, "impressions", 0, "ab_test_id = ?", a.ID)
Update(&abtestValue, "clicks", 0, "ab_test_id = ?", a.ID)
abTestsMutex.Unlock()
}