Skip to content

Commit e2f1db3

Browse files
committed
FSC fixes
1 parent 2dff4c6 commit e2f1db3

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

optimizely/event/user_event_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def create_impression_event(
5151

5252
if variation_id and experiment_id:
5353
variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)
54+
elif variation_id and flag_key:
55+
variation = project_config.get_flag_variation_by_id(flag_key, variation_id)
5456
event_context = user_event.EventContext(
5557
project_config.account_id, project_config.project_id, project_config.revision, project_config.anonymize_ip,
5658
)

optimizely/optimizely.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ def _send_impression_event(self, project_config, experiment, variation, flag_key
198198
user_id: ID for user.
199199
attributes: Dict representing user attributes and values which need to be recorded.
200200
"""
201+
if not experiment:
202+
experiment = entities.Experiment(
203+
id='',
204+
key='',
205+
layerId='',
206+
status='',
207+
variations=[],
208+
trafficAllocation=[],
209+
audienceIds=[],
210+
audienceConditions=[],
211+
forcedVariations={}
212+
)
213+
201214
variation_id = variation.id if variation is not None else None
202215
user_event = user_event_factory.UserEventFactory.create_impression_event(
203216
project_config, experiment, variation_id, flag_key, rule_key, rule_type, enabled, user_id, attributes
@@ -1057,7 +1070,7 @@ def _decide(self, user_context, key, decide_options=None):
10571070
if decision.experiment is not None:
10581071
experiment = decision.experiment
10591072
source_info["experiment"] = experiment
1060-
rule_key = experiment.key
1073+
rule_key = experiment.key if experiment else None
10611074
if decision.variation is not None:
10621075
variation = decision.variation
10631076
variation_key = variation.key

optimizely/project_config.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ def __init__(self, datafile, logger, error_handler):
166166
variations = []
167167
for rule in rules:
168168
# get variations as objects (rule.variations gives list)
169-
variation_objects = self.variation_key_map[rule.key].values()
170169

170+
variation_objects = self.variation_id_map_by_experiment_id[rule.id].values()
171171
for variation in variation_objects:
172172
if variation.id not in [var.id for var in variations]:
173173
variations.append(variation)
@@ -651,3 +651,26 @@ def get_variation_from_key_by_experiment_id(self, experiment_id, variation_key):
651651
variation_key, experiment_id)
652652

653653
return {}
654+
655+
def get_flag_variation_by_id(self, flag_key, variation_id):
656+
"""
657+
Gets variation by id.
658+
variation_id can be a string or in case of forced decisions, it can be an object.
659+
660+
Args:
661+
flag_key: flag key
662+
variation_key: variation id
663+
664+
Returns:
665+
Variation as a map.
666+
"""
667+
668+
if not flag_key:
669+
return None
670+
671+
variations = self.flag_variations_map.get(flag_key)
672+
for variation in variations:
673+
if variation.id == variation_id:
674+
return variation
675+
676+
return None

0 commit comments

Comments
 (0)