Skip to content

Commit e5aaccb

Browse files
committed
addressed more PR comments
1 parent 94a0c26 commit e5aaccb

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

optimizely/project_config.py

+38-25
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,26 @@ def __init__(self, datafile, logger, error_handler):
124124
# As we cannot create json variables in datafile directly, here we convert
125125
# the variables of string type and json subType to json type
126126
# This is needed to fully support json variables
127-
for feature in self.feature_key_map:
128-
for variable in self.feature_key_map[feature].variables:
127+
self.experiment_feature_map = {}
128+
self.flag_rules_map = {}
129+
130+
for feature_key, feature_value in self.feature_key_map.items():
131+
for variable in self.feature_key_map[feature_key].variables:
129132
sub_type = variable.get('subType', '')
130133
if variable['type'] == entities.Variable.Type.STRING and sub_type == entities.Variable.Type.JSON:
131134
variable['type'] = entities.Variable.Type.JSON
132135

133-
# Dict containing map of experiment ID to feature ID.
134-
# for checking that experiment is a feature experiment or not.
135-
self.experiment_feature_map = {}
136-
for feature in self.feature_key_map.values():
137-
feature.variables = self._generate_key_map(feature.variables, 'key', entities.Variable)
138-
for exp_id in feature.experimentIds:
136+
# loop over features=flags already happening
137+
# get feature variables for eacg flag/feature
138+
feature_value.variables = self._generate_key_map(feature_value.variables, 'key', entities.Variable)
139+
for exp_id in feature_value.experimentIds:
139140
# Add this experiment in experiment-feature map.
140-
self.experiment_feature_map[exp_id] = [feature.id]
141+
self.experiment_feature_map[exp_id] = [feature_value.id]
141142

142143
# all rules(experiment rules and delivery rules) for each flag
143-
self.flag_rules_map = {}
144144
for flag in self.feature_flags:
145-
146145
experiments = []
147-
if not flag['experimentIds'] == '':
146+
if len(flag['experimentIds']) > 0:
148147
for exp_id in flag['experimentIds']:
149148
experiments.append(self.experiment_id_map[exp_id])
150149
if not flag['rolloutId'] == '':
@@ -160,19 +159,7 @@ def __init__(self, datafile, logger, error_handler):
160159
# All variations for each flag
161160
# Datafile does not contain a separate entity for this.
162161
# We collect variations used in each rule (experiment rules and delivery rules)
163-
self.flag_variations_map = {}
164-
165-
for flag_key, rules in self.flag_rules_map.items():
166-
variations = []
167-
for rule in rules:
168-
# get variations as objects (rule.variations gives list)
169-
170-
variation_objects = self.variation_id_map_by_experiment_id[rule.id].values()
171-
for variation in variation_objects:
172-
if variation.id not in [var.id for var in variations]:
173-
variations.append(variation)
174-
175-
self.flag_variations_map[flag_key] = variations
162+
self.flag_variations_map = self.get_all_variations_for_each_rule(self.flag_rules_map)
176163

177164
@staticmethod
178165
def _generate_key_map(entity_list, key, entity_class):
@@ -674,3 +661,29 @@ def get_flag_variation_by_id(self, flag_key, variation_id):
674661
return variation
675662

676663
return None
664+
665+
def get_all_variations_for_each_rule(self, flag_rules_map):
666+
""" Helper method to get all variation objects from each flag.
667+
collects variations used in each rule (experiment rules and delivery rules).
668+
669+
Args:
670+
flag_rules_map: A dictionary. A map of all rules per flag.
671+
672+
Returns:
673+
Map of flag variations.
674+
"""
675+
flag_variations_map = {}
676+
677+
for flag_key, rules in flag_rules_map.items():
678+
variations = []
679+
for rule in rules:
680+
# get variations as objects (rule.variations gives list)
681+
variation_objects = self.variation_id_map_by_experiment_id[rule.id].values()
682+
for variation in variation_objects:
683+
# append variation if it's not already in the list
684+
if variation not in variations:
685+
variations.append(variation)
686+
687+
flag_variations_map[flag_key] = variations
688+
689+
return flag_variations_map

tests/test_user_context.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,10 @@ def test_should_return_valid_decision_after_setting_invalid_delivery_rule_variat
15681568
'rule (211127) and user (test_user) in the forced decision map.'
15691569
])))
15701570

1571-
# TODO - JAE: Can we change the test name and description? Not clear which part is invalid. Also, I see the forced set flag and decide flag is different. Is it intentional?
1572-
def test_invalid_experiment_rule_return_decision__forced_decision(self): # TODO - CHECK WITH JAE if this test should return valid decision like docstring says!
1571+
# TODO - JAE: Can we change the test name and description? Not clear which part is invalid.
1572+
# Also, I see the forced set flag and decide flag is different. Is it intentional?
1573+
# TODO - CHECK WITH JAE if this test should return valid decision like docstring says!
1574+
def test_invalid_experiment_rule_return_decision__forced_decision(self):
15731575
"""
15741576
Should return valid decision after setting invalid experiment
15751577
rule variation in forced decision.
@@ -1596,7 +1598,6 @@ def test_invalid_experiment_rule_return_decision__forced_decision(self):
15961598
self.assertEqual(decide_decision.rule_key, None)
15971599
self.assertFalse(decide_decision.enabled)
15981600

1599-
16001601
self.assertEqual(decide_decision.flag_key, 'test_feature_in_rollout')
16011602
self.assertEqual(decide_decision.user_context.user_id, 'test_user')
16021603
self.assertEqual(decide_decision.user_context.get_user_attributes(), {})

0 commit comments

Comments
 (0)