-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluator.go
More file actions
191 lines (165 loc) · 6.21 KB
/
Copy pathevaluator.go
File metadata and controls
191 lines (165 loc) · 6.21 KB
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
package evaluator
import (
"fmt"
"time"
"github.com/splitio/go-split-commons/v7/dtos"
"github.com/splitio/go-split-commons/v7/engine"
"github.com/splitio/go-split-commons/v7/engine/evaluator/impressionlabels"
"github.com/splitio/go-split-commons/v7/engine/grammar"
"github.com/splitio/go-split-commons/v7/storage"
"github.com/splitio/go-toolkit/v5/injection"
"github.com/splitio/go-toolkit/v5/logging"
)
const (
// Control is the treatment returned when something goes wrong
Control = "control"
)
// Result represents the result of an evaluation, including the resulting treatment, the label for the impression,
// the latency and error if any
type Result struct {
Treatment string
Label string
EvaluationTime time.Duration
SplitChangeNumber int64
Config *string
ImpressionsDisabled bool
}
// Results represents the result of multiple evaluations at once
type Results struct {
Evaluations map[string]Result
EvaluationTime time.Duration
}
// Evaluator struct is the main evaluator
type Evaluator struct {
splitStorage storage.SplitStorageConsumer
segmentStorage storage.SegmentStorageConsumer
eng *engine.Engine
logger logging.LoggerInterface
}
// NewEvaluator instantiates an Evaluator struct and returns a reference to it
func NewEvaluator(
splitStorage storage.SplitStorageConsumer,
segmentStorage storage.SegmentStorageConsumer,
eng *engine.Engine,
logger logging.LoggerInterface,
) *Evaluator {
return &Evaluator{
splitStorage: splitStorage,
segmentStorage: segmentStorage,
eng: eng,
logger: logger,
}
}
func (e *Evaluator) evaluateTreatment(key string, bucketingKey string, featureFlag string, splitDto *dtos.SplitDTO, attributes map[string]interface{}) *Result {
var config *string
if splitDto == nil {
e.logger.Warning(fmt.Sprintf("Feature flag %s not found, returning control.", featureFlag))
return &Result{Treatment: Control, Label: impressionlabels.SplitNotFound, Config: config}
}
ctx := injection.NewContext()
ctx.AddDependency("segmentStorage", e.segmentStorage)
ctx.AddDependency("evaluator", e)
split := grammar.NewSplit(splitDto, ctx, e.logger)
if split.Killed() {
e.logger.Warning(fmt.Sprintf(
"Feature flag %s has been killed, returning default treatment: %s",
featureFlag,
split.DefaultTreatment(),
))
if _, ok := split.Configurations()[split.DefaultTreatment()]; ok {
treatmentConfig := split.Configurations()[split.DefaultTreatment()]
config = &treatmentConfig
}
return &Result{
Treatment: split.DefaultTreatment(),
Label: impressionlabels.Killed,
SplitChangeNumber: split.ChangeNumber(),
Config: config,
ImpressionsDisabled: split.ImpressionsDisabled(),
}
}
treatment, label := e.eng.DoEvaluation(split, key, bucketingKey, attributes)
if treatment == nil {
e.logger.Warning(fmt.Sprintf(
"No condition matched, returning default treatment: %s",
split.DefaultTreatment(),
))
defaultTreatment := split.DefaultTreatment()
treatment = &defaultTreatment
label = impressionlabels.NoConditionMatched
}
if _, ok := split.Configurations()[*treatment]; ok {
treatmentConfig := split.Configurations()[*treatment]
config = &treatmentConfig
}
return &Result{
Treatment: *treatment,
Label: label,
SplitChangeNumber: split.ChangeNumber(),
Config: config,
ImpressionsDisabled: split.ImpressionsDisabled(),
}
}
// EvaluateFeature returns a struct with the resulting treatment and extra information for the impression
func (e *Evaluator) EvaluateFeature(key string, bucketingKey *string, featureFlag string, attributes map[string]interface{}) *Result {
before := time.Now()
splitDto := e.splitStorage.Split(featureFlag)
if bucketingKey == nil {
bucketingKey = &key
}
result := e.evaluateTreatment(key, *bucketingKey, featureFlag, splitDto, attributes)
after := time.Now()
result.EvaluationTime = after.Sub(before)
return result
}
// EvaluateFeatures returns a struct with the resulting treatment and extra information for the impression
func (e *Evaluator) EvaluateFeatures(key string, bucketingKey *string, featureFlags []string, attributes map[string]interface{}) Results {
var results = Results{
Evaluations: make(map[string]Result),
EvaluationTime: 0,
}
before := time.Now()
splits := e.splitStorage.FetchMany(featureFlags)
if bucketingKey == nil {
bucketingKey = &key
}
for _, featureFlag := range featureFlags {
results.Evaluations[featureFlag] = *e.evaluateTreatment(key, *bucketingKey, featureFlag, splits[featureFlag], attributes)
}
after := time.Now()
results.EvaluationTime = after.Sub(before)
return results
}
// EvaluateFeatureByFlagSets returns a struct with the resulting treatment and extra information for the impression
func (e *Evaluator) EvaluateFeatureByFlagSets(key string, bucketingKey *string, flagSets []string, attributes map[string]interface{}) Results {
featureFlagNamesBySets := e.getFeatureFlagNamesByFlagSets(flagSets)
if len(featureFlagNamesBySets) == 0 {
return Results{}
}
return e.EvaluateFeatures(key, bucketingKey, featureFlagNamesBySets, attributes)
}
// GetFeatureFlagNamesByFlagSets return flags that belong to some flag set
func (e *Evaluator) getFeatureFlagNamesByFlagSets(flagSets []string) []string {
uniqueFlags := make(map[string]struct{})
flagsBySets := e.splitStorage.GetNamesByFlagSets(flagSets)
for set, flags := range flagsBySets {
if len(flags) == 0 {
e.logger.Warning(fmt.Sprintf("you passed %s Flag Set that does not contain cached feature flag names, please double check what Flag Sets are in use in the Split user interface.", set))
continue
}
for _, featureFlag := range flags {
uniqueFlags[featureFlag] = struct{}{}
}
}
flags := make([]string, 0, len(uniqueFlags))
for flag := range uniqueFlags {
flags = append(flags, flag)
}
return flags
}
// EvaluateDependency SHOULD ONLY BE USED by DependencyMatcher.
// It's used to break the dependency cycle between matchers and evaluators.
func (e *Evaluator) EvaluateDependency(key string, bucketingKey *string, featureFlag string, attributes map[string]interface{}) string {
res := e.EvaluateFeature(key, bucketingKey, featureFlag, attributes)
return res.Treatment
}