21
21
from .helpers import experiment as experiment_helper
22
22
from .helpers import validator
23
23
from .optimizely_user_context import OptimizelyUserContext
24
+ from .decision .optimizely_decide_option import OptimizelyDecideOption
24
25
from .user_profile import UserProfile
25
26
26
27
Decision = namedtuple ('Decision' , 'experiment variation source' )
@@ -224,9 +225,7 @@ def get_stored_variation(self, project_config, experiment, user_profile):
224
225
225
226
return None
226
227
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 ):
230
229
""" Top-level function to help determine variation user should be put in.
231
230
232
231
First, check if experiment is running.
@@ -239,7 +238,7 @@ def get_variation(
239
238
project_config: Instance of ProjectConfig.
240
239
experiment: Experiment for which user variation needs to be determined.
241
240
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 .
243
242
244
243
Returns:
245
244
Variation user should see. None if user is not in experiment or experiment is not running
@@ -248,6 +247,11 @@ def get_variation(
248
247
user_id = user_context .user_id
249
248
attributes = user_context .get_user_attributes ()
250
249
250
+ if options :
251
+ ignore_user_profile = OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE in options
252
+ else :
253
+ ignore_user_profile = False
254
+
251
255
decide_reasons = []
252
256
# Check if experiment is running
253
257
if not experiment_helper .is_experiment_running (experiment ):
@@ -346,10 +350,7 @@ def get_variation_for_rollout(self, project_config, feature, user, options):
346
350
"""
347
351
decide_reasons = []
348
352
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 :
353
354
return Decision (None , None , enums .DecisionSources .ROLLOUT ), decide_reasons
354
355
355
356
rollout = project_config .get_rollout_from_id (feature .rolloutId )
@@ -378,12 +379,7 @@ def get_variation_for_rollout(self, project_config, feature, user, options):
378
379
379
380
decide_reasons += reasons_received
380
381
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
387
383
388
384
if variation :
389
385
rule = rollout_rules [index ]
@@ -513,15 +509,15 @@ def get_variation_from_delivery_rule(self, config, feature, rules, rule_index, u
513
509
514
510
return (bucketed_variation , skip_to_everyone_else ), decide_reasons
515
511
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 ):
517
513
""" Returns the experiment/variation the user is bucketed in for the given feature.
518
514
519
515
Args:
520
516
project_config: Instance of ProjectConfig.
521
517
feature: Feature for which we are determining if it is enabled or not for the given user.
522
518
user: user context for user.
523
519
attributes: Dict representing user attributes.
524
- ignore_user_profile: True if we should bypass the user profile service
520
+ options: Decide options.
525
521
526
522
Returns:
527
523
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
535
531
experiment = project_config .get_experiment_from_id (experiment )
536
532
if experiment :
537
533
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 )
539
535
decide_reasons += variation_reasons
540
536
if variation :
541
537
return Decision (experiment , variation , enums .DecisionSources .FEATURE_TEST ), decide_reasons
542
538
543
539
# Next check if user is part of a rollout
544
540
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 :
549
543
return Decision (None , None , enums .DecisionSources .ROLLOUT ), decide_reasons
0 commit comments