Skip to content

Commit

Permalink
Merge 3955db0 into ab40d9e
Browse files Browse the repository at this point in the history
  • Loading branch information
msohailhussain committed Dec 1, 2021
2 parents ab40d9e + 3955db0 commit 2e5e72e
Showing 1 changed file with 25 additions and 57 deletions.
82 changes: 25 additions & 57 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self, datafile, logger, error_handler):
self.variation_variable_usage_map = {}
self.variation_id_map_by_experiment_id = {}
self.variation_key_map_by_experiment_id = {}
self.flag_variations_map = {}

for experiment in self.experiment_id_map.values():
self.experiment_key_map[experiment.key] = experiment
Expand All @@ -120,46 +121,39 @@ def __init__(self, datafile, logger, error_handler):
)

self.feature_key_map = self._generate_key_map(self.feature_flags, 'key', entities.FeatureFlag)

# As we cannot create json variables in datafile directly, here we convert
# the variables of string type and json subType to json type
# This is needed to fully support json variables
self.experiment_feature_map = {}
self.flag_rules_map = {}

for feature_key, feature_value in self.feature_key_map.items():
for variable in self.feature_key_map[feature_key].variables:
for feature in self.feature_key_map:
for variable in self.feature_key_map[feature].variables:
sub_type = variable.get('subType', '')
if variable['type'] == entities.Variable.Type.STRING and sub_type == entities.Variable.Type.JSON:
variable['type'] = entities.Variable.Type.JSON

# loop over features=flags already happening
# get feature variables for eacg flag/feature
feature_value.variables = self._generate_key_map(feature_value.variables, 'key', entities.Variable)
for exp_id in feature_value.experimentIds:
# Add this experiment in experiment-feature map.
self.experiment_feature_map[exp_id] = [feature_value.id]

# all rules(experiment rules and delivery rules) for each flag
for flag in self.feature_flags:
experiments = []
if len(flag['experimentIds']) > 0:
for exp_id in flag['experimentIds']:
experiments.append(self.experiment_id_map[exp_id])
if not flag['rolloutId'] == '':
rollout = self.rollout_id_map[flag['rolloutId']]

rollout_experiments = self.get_rollout_experiments(rollout)

if rollout and rollout.experiments:
experiments.extend(rollout_experiments)
# Dict containing map of experiment ID to feature ID.
# for checking that experiment is a feature experiment or not.
self.experiment_feature_map = {}
for feature in self.feature_key_map.values():
feature.variables = self._generate_key_map(feature.variables, 'key', entities.Variable)

self.flag_rules_map[flag['key']] = experiments
rules = []
variations = []
for exp_id in feature.experimentIds:
# Add this experiment in experiment-feature map.
self.experiment_feature_map[exp_id] = [feature.id]
rules.append(self.experiment_id_map[exp_id])
rollout = None if len(feature.rolloutId) == 0 else self.rollout_id_map[feature.rolloutId]
if rollout:
for exp in rollout.experiments:
rules.append(self.experiment_id_map[exp['id']])

# All variations for each flag
# Datafile does not contain a separate entity for this.
# We collect variations used in each rule (experiment rules and delivery rules)
self.flag_variations_map = self.get_all_variations_for_each_rule(self.flag_rules_map)
for rule in rules:
# variation_id_map_by_experiment_id gives variation entity object while
# experiment_id_map will give us dictionary
for rule_variation in self.variation_id_map_by_experiment_id.get(rule.id).values():
if len(list(filter(lambda variation: variation.id == rule_variation.id, variations))) == 0:
variations.append(rule_variation)
self.flag_variations_map[feature.key] = variations

@staticmethod
def _generate_key_map(entity_list, key, entity_class):
Expand Down Expand Up @@ -639,32 +633,6 @@ def get_variation_from_key_by_experiment_id(self, experiment_id, variation_key):

return {}

def get_all_variations_for_each_rule(self, flag_rules_map):
""" Helper method to get all variation objects from each flag.
collects variations used in each rule (experiment rules and delivery rules).
Args:
flag_rules_map: A dictionary. A map of all rules per flag.
Returns:
Map of flag variations.
"""
flag_variations_map = {}

for flag_key, rules in flag_rules_map.items():
variations = []
for rule in rules:
# get variations as objects (rule.variations gives list)
variation_objects = self.variation_id_map_by_experiment_id[rule.id].values()
for variation in variation_objects:
# append variation if it's not already in the list
if variation not in variations:
variations.append(variation)

flag_variations_map[flag_key] = variations

return flag_variations_map

def get_flag_variation(self, flag_key, variation_attribute, target_value):
"""
Gets variation by specified variation attribute.
Expand Down

0 comments on commit 2e5e72e

Please sign in to comment.