Skip to content

Commit f03ea25

Browse files
committed
update vars in engine.evaluator
1 parent d683efa commit f03ea25

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

splitio/engine/evaluator.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
class Evaluator(object): # pylint: disable=too-few-public-methods
1414
"""Split Evaluator class."""
1515

16-
def __init__(self, split_storage, segment_storage, splitter):
16+
def __init__(self, feature_flag_storage, segment_storage, splitter):
1717
"""
1818
Construct a Evaluator instance.
1919
20-
:param split_storage: Split storage.
21-
:type split_storage: splitio.storage.SplitStorage
20+
:param feature_flag_storage: feature_flag storage.
21+
:type feature_flag_storage: splitio.storage.SplitStorage
2222
23-
:param split_storage: Storage storage.
24-
:type split_storage: splitio.storage.SegmentStorage
23+
:param segment_storage: Segment storage.
24+
:type segment_storage: splitio.storage.SegmentStorage
2525
"""
26-
self._split_storage = split_storage
26+
self._feature_flag_storage = feature_flag_storage
2727
self._segment_storage = segment_storage
2828
self._splitter = splitter
2929

30-
def _evaluate_treatment(self, feature, matching_key, bucketing_key, attributes, split):
30+
def _evaluate_treatment(self, feature_flag_name, matching_key, bucketing_key, attributes, feature_flag):
3131
"""
3232
Evaluate the user submitted data against a feature and return the resulting treatment.
3333
34-
:param feature: The feature for which to get the treatment
34+
:param feature_flag_name: The feature flag for which to get the treatment
3535
:type feature: str
3636
3737
:param matching_key: The matching_key for which to get the treatment
@@ -43,51 +43,51 @@ def _evaluate_treatment(self, feature, matching_key, bucketing_key, attributes,
4343
:param attributes: An optional dictionary of attributes
4444
:type attributes: dict
4545
46-
:param split: Split object
46+
:param feature_flag: Split object
4747
:type attributes: splitio.models.splits.Split|None
4848
49-
:return: The treatment for the key and split
49+
:return: The treatment for the key and feature flag
5050
:rtype: object
5151
"""
5252
label = ''
5353
_treatment = CONTROL
5454
_change_number = -1
5555

56-
if split is None:
57-
_LOGGER.warning('Unknown or invalid feature: %s', feature)
56+
if feature_flag is None:
57+
_LOGGER.warning('Unknown or invalid feature: %s', feature_flag_name)
5858
label = Label.SPLIT_NOT_FOUND
5959
else:
60-
_change_number = split.change_number
61-
if split.killed:
60+
_change_number = feature_flag.change_number
61+
if feature_flag.killed:
6262
label = Label.KILLED
63-
_treatment = split.default_treatment
63+
_treatment = feature_flag.default_treatment
6464
else:
6565
treatment, label = self._get_treatment_for_split(
66-
split,
66+
feature_flag,
6767
matching_key,
6868
bucketing_key,
6969
attributes
7070
)
7171
if treatment is None:
7272
label = Label.NO_CONDITION_MATCHED
73-
_treatment = split.default_treatment
73+
_treatment = feature_flag.default_treatment
7474
else:
7575
_treatment = treatment
7676

7777
return {
7878
'treatment': _treatment,
79-
'configurations': split.get_configurations_for(_treatment) if split else None,
79+
'configurations': feature_flag.get_configurations_for(_treatment) if feature_flag else None,
8080
'impression': {
8181
'label': label,
8282
'change_number': _change_number
8383
}
8484
}
8585

86-
def evaluate_feature(self, feature, matching_key, bucketing_key, attributes=None):
86+
def evaluate_feature(self, feature_flag_name, matching_key, bucketing_key, attributes=None):
8787
"""
8888
Evaluate the user submitted data against a feature and return the resulting treatment.
8989
90-
:param feature: The feature for which to get the treatment
90+
:param feature_flag_name: The feature flag for which to get the treatment
9191
:type feature: str
9292
9393
:param matching_key: The matching_key for which to get the treatment
@@ -103,20 +103,20 @@ def evaluate_feature(self, feature, matching_key, bucketing_key, attributes=None
103103
:rtype: object
104104
"""
105105
# Fetching Split definition
106-
split = self._split_storage.get(feature)
106+
feature_flag = self._feature_flag_storage.get(feature_flag_name)
107107

108108
# Calling evaluation
109-
evaluation = self._evaluate_treatment(feature, matching_key,
110-
bucketing_key, attributes, split)
109+
evaluation = self._evaluate_treatment(feature_flag_name, matching_key,
110+
bucketing_key, attributes, feature_flag)
111111

112112
return evaluation
113113

114-
def evaluate_features(self, features, matching_key, bucketing_key, attributes=None):
114+
def evaluate_features(self, feature_flag_names, matching_key, bucketing_key, attributes=None):
115115
"""
116116
Evaluate the user submitted data against multiple features and return the resulting
117117
treatment.
118118
119-
:param features: The features for which to get the treatments
119+
:param feature_flag_names: The feature flags for which to get the treatments
120120
:type feature: list(str)
121121
122122
:param matching_key: The matching_key for which to get the treatment
@@ -128,24 +128,24 @@ def evaluate_features(self, features, matching_key, bucketing_key, attributes=No
128128
:param attributes: An optional dictionary of attributes
129129
:type attributes: dict
130130
131-
:return: The treatments for the key and splits
131+
:return: The treatments for the key and feature flags
132132
:rtype: object
133133
"""
134134
return {
135-
feature: self._evaluate_treatment(feature, matching_key,
136-
bucketing_key, attributes, split)
137-
for (feature, split) in self._split_storage.fetch_many(features).items()
135+
feature_flag_name: self._evaluate_treatment(feature_flag_name, matching_key,
136+
bucketing_key, attributes, feature_flag)
137+
for (feature_flag_name, feature_flag) in self._feature_flag_storage.fetch_many(feature_flag_names).items()
138138
}
139139

140-
def _get_treatment_for_split(self, split, matching_key, bucketing_key, attributes=None):
140+
def _get_treatment_for_split(self, feature_flag, matching_key, bucketing_key, attributes=None):
141141
"""
142142
Evaluate the feature considering the conditions.
143143
144144
If there is a match, it will return the condition and the label.
145145
Otherwise, it will return (None, None)
146146
147-
:param split: The split for which to get the treatment
148-
:type split: Split
147+
:param feature_flag: The feature flag for which to get the treatment
148+
:type feature_flag: Split
149149
150150
:param matching_key: The key for which to get the treatment
151151
:type key: str
@@ -170,17 +170,17 @@ def _get_treatment_for_split(self, split, matching_key, bucketing_key, attribute
170170
'bucketing_key': bucketing_key
171171
}
172172

173-
for condition in split.conditions:
173+
for condition in feature_flag.conditions:
174174
if (not roll_out and
175175
condition.condition_type == ConditionType.ROLLOUT):
176-
if split.traffic_allocation < 100:
176+
if feature_flag.traffic_allocation < 100:
177177
bucket = self._splitter.get_bucket(
178178
bucketing_key,
179-
split.traffic_allocation_seed,
180-
split.algo
179+
feature_flag.traffic_allocation_seed,
180+
feature_flag.algo
181181
)
182-
if bucket > split.traffic_allocation:
183-
return split.default_treatment, Label.NOT_IN_SPLIT
182+
if bucket > feature_flag.traffic_allocation:
183+
return feature_flag.default_treatment, Label.NOT_IN_SPLIT
184184
roll_out = True
185185

186186
condition_matches = condition.matches(
@@ -192,9 +192,9 @@ def _get_treatment_for_split(self, split, matching_key, bucketing_key, attribute
192192
if condition_matches:
193193
return self._splitter.get_treatment(
194194
bucketing_key,
195-
split.seed,
195+
feature_flag.seed,
196196
condition.partitions,
197-
split.algo
197+
feature_flag.algo
198198
), condition.label
199199

200200
# No condition matches

tests/engine/test_evaluator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _build_evaluator_with_mocks(self, mocker):
2424
def test_evaluate_treatment_missing_split(self, mocker):
2525
"""Test that a missing split logs and returns CONTROL."""
2626
e = self._build_evaluator_with_mocks(mocker)
27-
e._split_storage.get.return_value = None
27+
e._feature_flag_storage.get.return_value = None
2828
result = e.evaluate_feature('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
2929
assert result['configurations'] == None
3030
assert result['treatment'] == evaluator.CONTROL
@@ -39,7 +39,7 @@ def test_evaluate_treatment_killed_split(self, mocker):
3939
mocked_split.killed = True
4040
mocked_split.change_number = 123
4141
mocked_split.get_configurations_for.return_value = '{"some_property": 123}'
42-
e._split_storage.get.return_value = mocked_split
42+
e._feature_flag_storage.get.return_value = mocked_split
4343
result = e.evaluate_feature('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
4444
assert result['treatment'] == 'off'
4545
assert result['configurations'] == '{"some_property": 123}'
@@ -57,7 +57,7 @@ def test_evaluate_treatment_ok(self, mocker):
5757
mocked_split.killed = False
5858
mocked_split.change_number = 123
5959
mocked_split.get_configurations_for.return_value = '{"some_property": 123}'
60-
e._split_storage.get.return_value = mocked_split
60+
e._feature_flag_storage.get.return_value = mocked_split
6161
result = e.evaluate_feature('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
6262
assert result['treatment'] == 'on'
6363
assert result['configurations'] == '{"some_property": 123}'
@@ -76,7 +76,7 @@ def test_evaluate_treatment_ok_no_config(self, mocker):
7676
mocked_split.killed = False
7777
mocked_split.change_number = 123
7878
mocked_split.get_configurations_for.return_value = None
79-
e._split_storage.get.return_value = mocked_split
79+
e._feature_flag_storage.get.return_value = mocked_split
8080
result = e.evaluate_feature('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
8181
assert result['treatment'] == 'on'
8282
assert result['configurations'] == None
@@ -95,7 +95,7 @@ def test_evaluate_treatments(self, mocker):
9595
mocked_split.killed = False
9696
mocked_split.change_number = 123
9797
mocked_split.get_configurations_for.return_value = '{"some_property": 123}'
98-
e._split_storage.fetch_many.return_value = {
98+
e._feature_flag_storage.fetch_many.return_value = {
9999
'feature1': None,
100100
'feature2': mocked_split,
101101
}

0 commit comments

Comments
 (0)