Skip to content

Commit 6849c33

Browse files
committed
addressed more PR comments, fixed FSC test failuer about impressin events
1 parent e2f1db3 commit 6849c33

7 files changed

+154
-311
lines changed

optimizely/decision_service.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .helpers import experiment as experiment_helper
2222
from .helpers import validator
2323
from .optimizely_user_context import OptimizelyUserContext
24+
from .decision.optimizely_decide_option import OptimizelyDecideOption
2425
from .user_profile import UserProfile
2526

2627
Decision = namedtuple('Decision', 'experiment variation source')
@@ -224,9 +225,7 @@ def get_stored_variation(self, project_config, experiment, user_profile):
224225

225226
return None
226227

227-
def get_variation(
228-
self, project_config, experiment, user_context, ignore_user_profile=False
229-
):
228+
def get_variation(self, project_config, experiment, user_context, options=None):
230229
""" Top-level function to help determine variation user should be put in.
231230
232231
First, check if experiment is running.
@@ -239,7 +238,7 @@ def get_variation(
239238
project_config: Instance of ProjectConfig.
240239
experiment: Experiment for which user variation needs to be determined.
241240
user_context: contains user id and attributes
242-
ignore_user_profile: True to ignore the user profile lookup. Defaults to False.
241+
options: Decide options.
243242
244243
Returns:
245244
Variation user should see. None if user is not in experiment or experiment is not running
@@ -248,6 +247,11 @@ def get_variation(
248247
user_id = user_context.user_id
249248
attributes = user_context.get_user_attributes()
250249

250+
if options:
251+
ignore_user_profile = OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE in options
252+
else:
253+
ignore_user_profile = False
254+
251255
decide_reasons = []
252256
# Check if experiment is running
253257
if not experiment_helper.is_experiment_running(experiment):
@@ -346,10 +350,7 @@ def get_variation_for_rollout(self, project_config, feature, user, options):
346350
"""
347351
decide_reasons = []
348352

349-
if not feature:
350-
return Decision(None, None, enums.DecisionSources.ROLLOUT), decide_reasons
351-
352-
if not feature.rolloutId:
353+
if not feature or not feature.rolloutId:
353354
return Decision(None, None, enums.DecisionSources.ROLLOUT), decide_reasons
354355

355356
rollout = project_config.get_rollout_from_id(feature.rolloutId)
@@ -378,12 +379,7 @@ def get_variation_for_rollout(self, project_config, feature, user, options):
378379

379380
decide_reasons += reasons_received
380381

381-
if not decision_response:
382-
# TODO - MATJAZ - careful - check how this exists the loop and terminates properly
383-
# when return is hit
384-
return Decision(None, None, enums.DecisionSources.ROLLOUT), decide_reasons
385-
else:
386-
variation, skip_to_everyone_else = decision_response
382+
variation, skip_to_everyone_else = decision_response
387383

388384
if variation:
389385
rule = rollout_rules[index]
@@ -513,15 +509,15 @@ def get_variation_from_delivery_rule(self, config, feature, rules, rule_index, u
513509

514510
return (bucketed_variation, skip_to_everyone_else), decide_reasons
515511

516-
def get_variation_for_feature(self, project_config, feature, user_context, ignore_user_profile=False):
512+
def get_variation_for_feature(self, project_config, feature, user_context, options=None):
517513
""" Returns the experiment/variation the user is bucketed in for the given feature.
518514
519515
Args:
520516
project_config: Instance of ProjectConfig.
521517
feature: Feature for which we are determining if it is enabled or not for the given user.
522518
user: user context for user.
523519
attributes: Dict representing user attributes.
524-
ignore_user_profile: True if we should bypass the user profile service
520+
options: Decide options.
525521
526522
Returns:
527523
Decision namedtuple consisting of experiment and variation for the user.
@@ -535,15 +531,13 @@ def get_variation_for_feature(self, project_config, feature, user_context, ignor
535531
experiment = project_config.get_experiment_from_id(experiment)
536532
if experiment:
537533
variation, variation_reasons = self.get_variation_from_experiment_rule(
538-
project_config, feature.key, experiment, user_context, ignore_user_profile)
534+
project_config, feature.key, experiment, user_context, options)
539535
decide_reasons += variation_reasons
540536
if variation:
541537
return Decision(experiment, variation, enums.DecisionSources.FEATURE_TEST), decide_reasons
542538

543539
# Next check if user is part of a rollout
544540
if feature.rolloutId:
545-
return self.get_variation_for_rollout(project_config, feature, user_context, ignore_user_profile)
546-
547-
# check if not part of rollout
548-
if not feature.rolloutId:
541+
return self.get_variation_for_rollout(project_config, feature, user_context, options)
542+
else:
549543
return Decision(None, None, enums.DecisionSources.ROLLOUT), decide_reasons

optimizely/event/user_event_factory.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create_impression_event(
4343
"""
4444

4545
if not activated_experiment and rule_type is not enums.DecisionSources.ROLLOUT:
46-
return None
46+
return None # TODO - we get this when event not sent. Now we allow None for activated_experiment
4747

4848
variation, experiment_id = None, None
4949
if activated_experiment:
@@ -60,11 +60,11 @@ def create_impression_event(
6060
return user_event.ImpressionEvent(
6161
event_context,
6262
user_id,
63-
activated_experiment,
63+
activated_experiment, # TODO - check here also. that exper is used, that is not as None, - or pass empty strings etc
6464
event_factory.EventFactory.build_attribute_list(user_attributes, project_config),
6565
variation,
6666
flag_key,
67-
rule_key,
67+
rule_key, # TODO needs to be ampty string here - in relevnt APIs? or is variation that needs to be ampty string?
6868
rule_type,
6969
enabled,
7070
project_config.get_bot_filtering_value(),

optimizely/optimizely.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,6 @@ def _get_feature_variable_for_type(
306306
)
307307

308308
if decision.source == enums.DecisionSources.FEATURE_TEST:
309-
310-
print('DECISION.EXPERIMENT - ', decision.experiment)
311-
312309
source_info = {
313310
'experiment_key': decision.experiment.key,
314311
'variation_key': decision.variation.key,
@@ -1046,7 +1043,6 @@ def _decide(self, user_context, key, decide_options=None):
10461043
decision_source = DecisionSources.ROLLOUT
10471044
source_info = {}
10481045
decision_event_dispatched = False
1049-
ignore_ups = OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE in decide_options
10501046

10511047
# Check forced decisions first
10521048
optimizely_decision_context = OptimizelyUserContext.OptimizelyDecisionContext(flag_key=key, rule_key=rule_key)
@@ -1062,7 +1058,7 @@ def _decide(self, user_context, key, decide_options=None):
10621058
# Regular decision
10631059
decision, decision_reasons = self.decision_service.get_variation_for_feature(config,
10641060
feature_flag,
1065-
user_context, ignore_ups)
1061+
user_context, decide_options)
10661062

10671063
reasons += decision_reasons
10681064

@@ -1085,6 +1081,7 @@ def _decide(self, user_context, key, decide_options=None):
10851081
self._send_impression_event(config, experiment, variation, flag_key, rule_key or '',
10861082
decision_source, feature_enabled,
10871083
user_id, attributes)
1084+
10881085
decision_event_dispatched = True
10891086

10901087
# Generate all variables map if decide options doesn't include excludeVariables

similar_rule_keys_audience.json

-171
This file was deleted.

0 commit comments

Comments
 (0)