forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.go
365 lines (337 loc) · 8.7 KB
/
experiment.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package growthbook
import (
"encoding/json"
"regexp"
)
type ExperimentStatus string
const (
DraftStatus ExperimentStatus = "draft"
RunningStatus ExperimentStatus = "running"
StoppedStatus ExperimentStatus = "stopped"
)
// Experiment defines a single experiment.
type Experiment struct {
Key string
Variations []FeatureValue
Ranges []Range
Meta []VariationMeta
Filters []Filter
Seed string
Name string
Phase string
URLPatterns []URLTarget
Weights []float64
Condition Condition
Coverage *float64
Include func() bool
Namespace *Namespace
Force *int
HashAttribute string
HashVersion int
Active bool
Status ExperimentStatus
URL *regexp.Regexp
Groups []string
}
// NewExperiment creates an experiment with default settings: active,
// but all other fields empty.
func NewExperiment(key string) *Experiment {
return &Experiment{
Key: key,
Active: true,
}
}
// WithVariations set the feature variations for an experiment.
func (exp *Experiment) WithVariations(variations ...FeatureValue) *Experiment {
exp.Variations = variations
return exp
}
// WithRanges set the ranges for an experiment.
func (exp *Experiment) WithRanges(ranges ...Range) *Experiment {
exp.Ranges = ranges
return exp
}
// WithMeta sets the meta information for an experiment.
func (exp *Experiment) WithMeta(meta ...VariationMeta) *Experiment {
exp.Meta = meta
return exp
}
// WithFilters sets the filters for an experiment.
func (exp *Experiment) WithFilters(filters ...Filter) *Experiment {
exp.Filters = filters
return exp
}
// WithSeed sets the hash seed for an experiment.
func (exp *Experiment) WithSeed(seed string) *Experiment {
exp.Seed = seed
return exp
}
// WithName sets the name for an experiment.
func (exp *Experiment) WithName(name string) *Experiment {
exp.Name = name
return exp
}
// WithPhase sets the phase for an experiment.
func (exp *Experiment) WithPhase(phase string) *Experiment {
exp.Phase = phase
return exp
}
// WithWeights set the weights for an experiment.
func (exp *Experiment) WithWeights(weights ...float64) *Experiment {
exp.Weights = weights
return exp
}
// WithCondition sets the condition for an experiment.
func (exp *Experiment) WithCondition(condition Condition) *Experiment {
exp.Condition = condition
return exp
}
// WithCoverage sets the coverage for an experiment.
func (exp *Experiment) WithCoverage(coverage float64) *Experiment {
exp.Coverage = &coverage
return exp
}
// WithInclude sets the inclusion function for an experiment.
func (exp *Experiment) WithIncludeFunction(include func() bool) *Experiment {
exp.Include = include
return exp
}
// WithNamespace sets the namespace for an experiment.
func (exp *Experiment) WithNamespace(namespace *Namespace) *Experiment {
exp.Namespace = namespace
return exp
}
// WithForce sets the forced value index for an experiment.
func (exp *Experiment) WithForce(force int) *Experiment {
exp.Force = &force
return exp
}
// WithHashAttribute sets the hash attribute for an experiment.
func (exp *Experiment) WithHashAttribute(hashAttribute string) *Experiment {
exp.HashAttribute = hashAttribute
return exp
}
// WithHashVersion sets the hash version for an experiment.
func (exp *Experiment) WithHashVersion(hashVersion int) *Experiment {
exp.HashVersion = hashVersion
return exp
}
// WithActive sets the enabled flag for an experiment.
func (exp *Experiment) WithActive(active bool) *Experiment {
exp.Active = active
return exp
}
// WithStatus sets the status for an experiment.
func (exp *Experiment) WithStatus(status ExperimentStatus) *Experiment {
exp.Status = status
return exp
}
// WithGroups sets the groups for an experiment.
func (exp *Experiment) WithGroups(groups ...string) *Experiment {
exp.Groups = groups
return exp
}
// WithURL sets the URL for an experiment.
func (exp *Experiment) WithURL(url *regexp.Regexp) *Experiment {
exp.URL = url
return exp
}
// ParseExperiment creates an Experiment value from raw JSON input.
func ParseExperiment(data []byte) *Experiment {
dict := make(map[string]interface{})
err := json.Unmarshal(data, &dict)
if err != nil {
logError("Failed parsing JSON input", "Experiment")
return nil
}
return BuildExperiment(dict)
}
// BuildExperiment creates an Experiment value from a JSON object
// represented as a Go map.
func BuildExperiment(dict map[string]interface{}) *Experiment {
exp := NewExperiment("tmp")
gotKey := false
for k, v := range dict {
switch k {
case "key":
key, ok := jsonString(v, "Experiment", "key")
if !ok {
return nil
}
exp.Key = key
gotKey = true
case "variations":
exp = exp.WithVariations(BuildFeatureValues(v)...)
case "ranges":
ranges, ok := jsonRangeArray(v, "Experiment", "ranges")
if !ok {
return nil
}
exp = exp.WithRanges(ranges...)
case "meta":
meta, ok := jsonVariationMetaArray(v, "Experiment", "meta")
if !ok {
return nil
}
exp = exp.WithMeta(meta...)
case "filters":
filters, ok := jsonFilterArray(v, "Experiment", "filters")
if !ok {
return nil
}
exp = exp.WithFilters(filters...)
case "seed":
seed, ok := jsonString(v, "FeatureRule", "seed")
if !ok {
return nil
}
exp = exp.WithSeed(seed)
case "name":
name, ok := jsonString(v, "FeatureRule", "name")
if !ok {
return nil
}
exp = exp.WithName(name)
case "phase":
phase, ok := jsonString(v, "FeatureRule", "phase")
if !ok {
return nil
}
exp = exp.WithPhase(phase)
case "weights":
weights, ok := jsonFloatArray(v, "Experiment", "weights")
if !ok {
return nil
}
exp = exp.WithWeights(weights...)
case "active":
active, ok := jsonBool(v, "Experiment", "active")
if !ok {
return nil
}
exp = exp.WithActive(active)
case "coverage":
coverage, ok := jsonFloat(v, "Experiment", "coverage")
if !ok {
return nil
}
exp = exp.WithCoverage(coverage)
case "condition":
tmp, ok := v.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", "Experiment", "condition")
continue
}
cond := BuildCondition(tmp)
if cond == nil {
logError("Invalid condition in JSON experiment data")
} else {
exp = exp.WithCondition(cond)
}
case "namespace":
namespace := BuildNamespace(v)
if namespace == nil {
return nil
}
exp = exp.WithNamespace(namespace)
case "force":
force, ok := jsonInt(v, "Experiment", "force")
if !ok {
return nil
}
exp = exp.WithForce(force)
case "hashAttribute":
hashAttribute, ok := jsonString(v, "Experiment", "hashAttribute")
if !ok {
return nil
}
exp = exp.WithHashAttribute(hashAttribute)
case "hashVersion":
hashVersion, ok := jsonInt(v, "Experiment", "hashVersion")
if !ok {
return nil
}
exp.HashVersion = hashVersion
default:
logWarn("Unknown key in JSON data", "Experiment", k)
}
}
if !gotKey {
logWarn("Key not set in JSON experiment data")
}
return exp
}
func (exp *Experiment) applyOverride(override *ExperimentOverride) *Experiment {
newExp := *exp
if override.Condition != nil {
newExp.Condition = override.Condition
}
if override.Weights != nil {
newExp.Weights = override.Weights
}
if override.Active != nil {
newExp.Active = *override.Active
}
if override.Status != nil {
newExp.Status = *override.Status
}
if override.Force != nil {
newExp.Force = override.Force
}
if override.Coverage != nil {
newExp.Coverage = override.Coverage
}
if override.Groups != nil {
newExp.Groups = override.Groups
}
if override.Namespace != nil {
newExp.Namespace = override.Namespace
}
if override.URL != nil {
newExp.URL = override.URL
}
return &newExp
}
func experimentFromFeatureRule(id string, rule *FeatureRule) *Experiment {
exp := NewExperiment(id).WithVariations(rule.Variations...)
if rule.Key != "" {
exp.Key = rule.Key
}
if rule.Coverage != nil {
exp = exp.WithCoverage(*rule.Coverage)
}
if rule.Weights != nil {
tmp := make([]float64, len(rule.Weights))
copy(tmp, rule.Weights)
exp = exp.WithWeights(tmp...)
}
if rule.HashAttribute != "" {
exp = exp.WithHashAttribute(rule.HashAttribute)
}
if rule.Namespace != nil {
val := Namespace{rule.Namespace.ID, rule.Namespace.Start, rule.Namespace.End}
exp = exp.WithNamespace(&val)
}
if rule.Meta != nil {
exp = exp.WithMeta(rule.Meta...)
}
if rule.Ranges != nil {
exp = exp.WithRanges(rule.Ranges...)
}
if rule.Name != "" {
exp = exp.WithName(rule.Name)
}
if rule.Phase != "" {
exp = exp.WithPhase(rule.Phase)
}
if rule.Seed != "" {
exp = exp.WithSeed(rule.Seed)
}
if rule.HashVersion != 0 {
exp = exp.WithHashVersion(rule.HashVersion)
}
if rule.Filters != nil {
exp = exp.WithFilters(rule.Filters...)
}
return exp
}