-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathevaluator.go
330 lines (269 loc) · 8.88 KB
/
evaluator.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
package growthbook
import (
"fmt"
"github.com/growthbook/growthbook-golang/internal/condition"
"github.com/growthbook/growthbook-golang/internal/value"
)
type evaluator struct {
features FeatureMap
savedGroups condition.SavedGroups
evaluated stack[string]
client *Client
}
func (e *evaluator) evalFeature(key string) *FeatureResult {
if e.evaluated.has(key) {
return getFeatureResult(nil, CyclicPrerequisiteResultSource, "", nil, nil)
}
e.evaluated.push(key)
defer e.evaluated.pop()
feature := e.features[key]
if feature == nil {
return getFeatureResult(nil, UnknownFeatureResultSource, "", nil, nil)
}
for _, rule := range feature.Rules {
res := e.evalRule(key, &rule)
if res != nil {
return res
}
}
return getFeatureResult(feature.DefaultValue, DefaultValueResultSource, "", nil, nil)
}
func (e *evaluator) runExperiment(exp *Experiment, featureId string) *ExperimentResult {
// 1. If experiment.variations has fewer than 2 variations, return getExperimentResult(experiment)
if len(exp.Variations) < 2 {
e.client.logger.Debug("Invalid experiment", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 2. If context.enabled is false, return getExperimentResult(experiment)
if !e.client.enabled {
e.client.logger.Debug("Client disabled", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 3. If context.url exists
if qsOverride, ok := getQueryStringOverride(exp.Key, e.client.url, len(exp.Variations)); ok {
e.client.logger.Debug("Force via querystring", "id", exp.Key, "variation", qsOverride)
return e.getExperimentResult(exp, qsOverride, false, featureId, nil)
}
// 4. Return if forced via context
if varId, ok := e.client.forcedVariations[exp.Key]; ok {
e.client.logger.Debug("Force via dev tools", "id", exp.Key, "variation", varId)
return e.getExperimentResult(exp, varId, false, featureId, nil)
}
// 5. If experiment.active is set to false, return getExperimentResult(experiment)
if !exp.getActive() {
e.client.logger.Debug("Skip because inactive", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 6. Get the user hash value and return if empty
_, hashValue := e.getHashAttribute(exp.HashAttribute, exp.FallbackAttribute)
if hashValue == "" {
e.client.logger.Debug("Skip because of missing hashAttribute", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 6.5 TODO If sticky bucketing is permitted, check to see if a sticky bucket value exists. If so, skip steps 7-8.
// 7. Apply filters and namespace
if len(exp.Filters) > 0 {
if e.isFilteredOut(exp.Filters) {
e.client.logger.Debug("Skip because of filters", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
} else if exp.Namespace != nil && !exp.Namespace.inNamespace(hashValue) {
e.client.logger.Debug("Skip because of namespace", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 8 Return if any conditions are not met, return
if !exp.Condition.Eval(e.client.attributes, e.savedGroups) {
e.client.logger.Debug("Skip because of condition exp", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 8.2 If experiment.parentConditions is set (prerequisites), return if any of them evaluate to false. See the corresponding logic in
if len(exp.ParentConditions) > 0 {
for _, parent := range exp.ParentConditions {
res := e.evalFeature(parent.Id)
if res == nil {
e.client.logger.Debug("Skip because of prerequisite fails", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
if res.Source == CyclicPrerequisiteResultSource {
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
evalObj := value.ObjValue{"value": value.New(res.Value)}
evaled := parent.Condition.Eval(evalObj, e.savedGroups)
if !evaled {
e.client.logger.Debug("Skip because of prerequisite evaluation fails", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
}
}
// 8.3 TODO Apply any url targeting based on experiment.urlPatterns, return if no match
// 9 Choose a variation
// 9.1 TODO If a sticky bucket value exists, use it.
// 9.2 Else, calculate bucket ranges for the variations and choose one
ranges := exp.Ranges
if len(exp.Ranges) == 0 {
ranges = e.client.getBucketRanges(len(exp.Variations), exp.getCoverage(), exp.Weights)
}
n := hash(exp.getSeed(), hashValue, if0(exp.HashVersion, 1))
if n == nil {
e.client.logger.Debug("Skip because of invalid hash version", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
assigned := chooseVariation(*n, ranges)
// 10. If assigned == -1, return getExperimentResult(experiment)
if assigned < 0 {
e.client.logger.Debug("Skip because of coverage", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 11. If experiment has a forced variation, return
if exp.Force != nil {
e.client.logger.Debug("Force variation", "id", exp.Key, "variation", *exp.Force)
return e.getExperimentResult(exp, *exp.Force, false, featureId, nil)
}
// 12. If context.qaMode, return getExperimentResult(experiment)
if e.client.qaMode {
e.client.logger.Debug("Skip because of QA mode", "id", exp.Key)
return e.getExperimentResult(exp, -1, false, featureId, nil)
}
// 13. Build the result object
return e.getExperimentResult(exp, assigned, true, featureId, n)
}
func (e *evaluator) getExperimentResult(
exp *Experiment,
variationId int,
hashUsed bool,
featureId string,
bucket *float64,
) *ExperimentResult {
inExperiment := true
if variationId < 0 || variationId >= len(exp.Variations) {
variationId = 0
inExperiment = false
}
hashAttribute, hashValue := e.getHashAttribute(exp.HashAttribute, "")
var meta *VariationMeta
if variationId > 0 && variationId < len(exp.Meta) {
meta = &exp.Meta[variationId]
}
key := fmt.Sprint(variationId)
if meta != nil && meta.Key != "" {
key = meta.Key
}
res := ExperimentResult{
Key: key,
FeatureId: featureId,
InExperiment: inExperiment,
HashUsed: hashUsed,
VariationId: variationId,
Value: exp.Variations[variationId],
HashAttribute: hashAttribute,
HashValue: hashValue,
Bucket: bucket,
}
if meta != nil {
res.Name = meta.Name
res.Passthrough = meta.Passthrough
}
return &res
}
func (e *evaluator) evalRule(featureId string, rule *FeatureRule) *FeatureResult {
if len(rule.ParentConditions) > 0 {
for _, parent := range rule.ParentConditions {
res := e.evalFeature(parent.Id)
if res == nil {
return nil
}
if res.Source == CyclicPrerequisiteResultSource {
return res
}
evalObj := value.ObjValue{"value": value.New(res.Value)}
evaled := parent.Condition.Eval(evalObj, e.savedGroups)
if !evaled {
if parent.Gate {
return getFeatureResult(nil, PrerequisiteResultSource, "", nil, nil)
}
return nil
}
}
}
if e.isFilteredOut(rule.Filters) {
return nil
}
if rule.Force != nil {
if !rule.Condition.Eval(e.client.attributes, e.savedGroups) {
return nil
}
if !e.isIncludedInRollout(featureId, rule) {
return nil
}
return getFeatureResult(rule.Force, ForceResultSource, rule.Id, nil, nil)
}
if len(rule.Variations) == 0 {
return nil
}
exp := experimentFromFeatureRule(featureId, rule)
res := e.runExperiment(exp, featureId)
if !res.InExperiment || res.Passthrough {
return nil
}
return getFeatureResult(res.Value, ExperimentResultSource, rule.Id, exp, res)
}
func (e *evaluator) isIncludedInRollout(featureId string, rule *FeatureRule) bool {
if rule == nil {
return true
}
if rule.Coverage == nil && rule.Range == nil {
return true
}
if rule.Range == nil && *rule.Coverage == 0.0 {
return false
}
_, hashValue := e.getHashAttribute(rule.HashAttribute, "")
if hashValue == "" {
return false
}
seed := rule.Seed
if seed == "" {
seed = featureId
}
n := hash(seed, hashValue, if0(rule.HashVersion, 1))
if n == nil {
return false
}
if rule.Range != nil {
return rule.Range.InRange(*n)
}
if rule.Coverage != nil {
return *n <= *rule.Coverage
}
return true
}
func (e *evaluator) isFilteredOut(filters []Filter) bool {
for _, filter := range filters {
_, hashValue := e.getHashAttribute(filter.Attribute, "")
if hashValue == "" {
return true
}
hash := hash(filter.Seed, hashValue, if0(filter.HashVersion, 2))
if hash == nil {
return true
}
if chooseVariation(*hash, filter.Ranges) == -1 {
return true
}
}
return false
}
func (e *evaluator) getHashAttribute(key string, fallback string) (string, string) {
if key == "" {
key = "id"
}
hashValue, ok := e.client.attributes[key]
if ok && !value.IsNull(hashValue) {
return key, hashValue.String()
}
hashValue, ok = e.client.attributes[fallback]
if ok && !value.IsNull(hashValue) {
return fallback, hashValue.String()
}
return key, ""
}