Class: Optimizely::DecisionService
- Inherits:
-
Object
- Object
- Optimizely::DecisionService
- Defined in:
- lib/optimizely/decision_service.rb
Defined Under Namespace
Classes: CmabDecisionResult, Decision, DecisionResult, VariationResult
Constant Summary collapse
- DECISION_SOURCES =
{ 'EXPERIMENT' => 'experiment', 'FEATURE_TEST' => 'feature-test', 'ROLLOUT' => 'rollout', 'HOLDOUT' => 'holdout' }.freeze
Instance Attribute Summary collapse
-
#bucketer ⇒ Object
readonly
Optimizely’s decision service that determines into which variation of an experiment a user will be allocated.
-
#forced_variation_map ⇒ Object
readonly
Hash of user IDs to a Hash of experiments to variations.
Instance Method Summary collapse
- #get_decision_for_flag(feature_flag, user_context, project_config, decide_options = [], user_profile_tracker = nil, decide_reasons = nil) ⇒ Object
- #get_forced_variation(project_config, experiment_key, user_id) ⇒ Object
- #get_variation(project_config, experiment_id, user_context, user_profile_tracker = nil, decide_options = [], reasons = []) ⇒ Object
- #get_variation_for_feature(project_config, feature_flag, user_context, decide_options = []) ⇒ Object
- #get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options = []) ⇒ Object
- #get_variation_for_feature_rollout(project_config, feature_flag, user_context) ⇒ Object
- #get_variation_for_holdout(holdout, user_context, project_config) ⇒ Object
- #get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index, user_context) ⇒ Object
- #get_variation_from_experiment_rule(project_config, flag_key, rule, user, user_profile_tracker, options = []) ⇒ Object
- #get_variations_for_feature_list(project_config, feature_flags, user_context, decide_options = []) ⇒ Object
-
#initialize(logger, cmab_service, user_profile_service = nil) ⇒ DecisionService
constructor
A new instance of DecisionService.
- #set_forced_variation(project_config, experiment_key, user_id, variation_key) ⇒ Object
- #validated_forced_decision(project_config, context, user_context) ⇒ Object
Constructor Details
#initialize(logger, cmab_service, user_profile_service = nil) ⇒ DecisionService
Returns a new instance of DecisionService.
53 54 55 56 57 58 59 |
# File 'lib/optimizely/decision_service.rb', line 53 def initialize(logger, cmab_service, user_profile_service = nil) @logger = logger @user_profile_service = user_profile_service @bucketer = Bucketer.new(logger) @forced_variation_map = {} @cmab_service = cmab_service end |
Instance Attribute Details
#bucketer ⇒ Object (readonly)
Optimizely’s decision service that determines into which variation of an experiment a user will be allocated.
The decision service contains all logic relating to how a user bucketing decisions is made. This includes all of the following (in order):
-
Check experiment status
-
Check forced bucketing
-
Check whitelisting
-
Check user profile service for past bucketing decisions (sticky bucketing)
-
Check audience targeting
-
Check cmab service
-
Use Murmurhash3 to bucket the user
35 36 37 |
# File 'lib/optimizely/decision_service.rb', line 35 def bucketer @bucketer end |
#forced_variation_map ⇒ Object (readonly)
Hash of user IDs to a Hash of experiments to variations. This contains all the forced variations set by the user by calling setForcedVariation.
39 40 41 |
# File 'lib/optimizely/decision_service.rb', line 39 def forced_variation_map @forced_variation_map end |
Instance Method Details
#get_decision_for_flag(feature_flag, user_context, project_config, decide_options = [], user_profile_tracker = nil, decide_reasons = nil) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/optimizely/decision_service.rb', line 180 def get_decision_for_flag(feature_flag, user_context, project_config, = [], user_profile_tracker = nil, decide_reasons = nil) # Get the decision for a single feature flag. # Processes holdouts, experiments, and rollouts in that order. # # feature_flag - The feature flag to get a decision for # user_context - The user context # project_config - The project config # decide_options - Array of decide options # user_profile_tracker - The user profile tracker # decide_reasons - Array of decision reasons to merge # # Returns a DecisionResult for the feature flag reasons = decide_reasons ? decide_reasons.dup : [] user_id = user_context.user_id # Check holdouts holdouts = project_config.get_holdouts_for_flag(feature_flag['id']) holdouts.each do |holdout| holdout_decision = get_variation_for_holdout(holdout, user_context, project_config) reasons.push(*holdout_decision.reasons) next unless holdout_decision.decision = "The user '#{user_id}' is bucketed into holdout '#{holdout['key']}' for feature flag '#{feature_flag['key']}'." @logger.log(Logger::INFO, ) reasons.push() return DecisionResult.new(holdout_decision.decision, false, reasons) end # Check if the feature flag has an experiment and the user is bucketed into that experiment experiment_decision = get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, ) reasons.push(*experiment_decision.reasons) return DecisionResult.new(experiment_decision.decision, experiment_decision.error, reasons) if experiment_decision.decision # Check if the feature flag has a rollout and the user is bucketed into that rollout rollout_decision = get_variation_for_feature_rollout(project_config, feature_flag, user_context) reasons.push(*rollout_decision.reasons) if rollout_decision.decision # Check if this was a forced decision (last reason contains "forced decision map") is_forced_decision = reasons.last&.include?('forced decision map') unless is_forced_decision # Only add the "bucketed into rollout" message for normal bucketing = "The user '#{user_id}' is bucketed into a rollout for feature flag '#{feature_flag['key']}'." @logger.log(Logger::INFO, ) reasons.push() end DecisionResult.new(rollout_decision.decision, rollout_decision.error, reasons) else = "The user '#{user_id}' is not bucketed into a rollout for feature flag '#{feature_flag['key']}'." @logger.log(Logger::INFO, ) DecisionResult.new(nil, false, reasons) end end |
#get_forced_variation(project_config, experiment_key, user_id) ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/optimizely/decision_service.rb', line 550 def get_forced_variation(project_config, experiment_key, user_id) # Gets the forced variation for the given user and experiment. # # project_config - Instance of ProjectConfig # experiment_key - String key for experiment # user_id - String ID for user # # Returns Variation The variation which the given user and experiment should be forced into decide_reasons = [] unless @forced_variation_map.key? user_id = "User '#{user_id}' is not in the forced variation map." @logger.log(Logger::DEBUG, ) return nil, decide_reasons end experiment_to_variation_map = @forced_variation_map[user_id] experiment = project_config.get_experiment_from_key(experiment_key) experiment_id = experiment['id'] if experiment # check for nil and empty string experiment ID # this case is logged in get_experiment_from_key return nil, decide_reasons if experiment_id.nil? || experiment_id.empty? unless experiment_to_variation_map.key? experiment_id = "No experiment '#{experiment_id}' mapped to user '#{user_id}' in the forced variation map." @logger.log(Logger::DEBUG, ) decide_reasons.push() return nil, decide_reasons end variation_id = experiment_to_variation_map[experiment_id] variation_key = '' variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) variation_key = variation['key'] if variation # check if the variation exists in the datafile # this case is logged in get_variation_from_id return nil, decide_reasons if variation_key.empty? = "Variation '#{variation_key}' is mapped to experiment '#{experiment_id}' and user '#{user_id}' in the forced variation map" @logger.log(Logger::DEBUG, ) decide_reasons.push() [variation, decide_reasons] end |
#get_variation(project_config, experiment_id, user_context, user_profile_tracker = nil, decide_options = [], reasons = []) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/optimizely/decision_service.rb', line 61 def get_variation(project_config, experiment_id, user_context, user_profile_tracker = nil, = [], reasons = []) # Determines variation into which user will be bucketed. # # project_config - project_config - Instance of ProjectConfig # experiment_id - Experiment for which visitor variation needs to be determined # user_context - Optimizely user context instance # user_profile_tracker: Tracker for reading and updating user profile of the user. # reasons: Decision reasons. # # Returns VariationResult struct user_profile_tracker = UserProfileTracker.new(user_context.user_id, @user_profile_service, @logger) unless user_profile_tracker.is_a?(Optimizely::UserProfileTracker) decide_reasons = [] decide_reasons.push(*reasons) user_id = user_context.user_id attributes = user_context.user_attributes # By default, the bucketing ID should be the user ID bucketing_id, bucketing_id_reasons = get_bucketing_id(user_id, attributes) decide_reasons.push(*bucketing_id_reasons) # Check to make sure experiment is active experiment = project_config.get_experiment_from_id(experiment_id) return VariationResult.new(nil, false, decide_reasons, nil) if experiment.nil? experiment_key = experiment['key'] unless project_config.experiment_running?(experiment) = "Experiment '#{experiment_key}' is not running." @logger.log(Logger::INFO, ) decide_reasons.push() return VariationResult.new(nil, false, decide_reasons, nil) end # Check if a forced variation is set for the user forced_variation, reasons_received = get_forced_variation(project_config, experiment['key'], user_id) decide_reasons.push(*reasons_received) return VariationResult.new(nil, false, decide_reasons, forced_variation['id']) if forced_variation # Check if user is in a white-listed variation whitelisted_variation_id, reasons_received = get_whitelisted_variation_id(project_config, experiment_id, user_id) decide_reasons.push(*reasons_received) return VariationResult.new(nil, false, decide_reasons, whitelisted_variation_id) if whitelisted_variation_id should_ignore_user_profile_service = .include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE # Check for saved bucketing decisions if decide_options do not include ignoreUserProfileService unless should_ignore_user_profile_service && user_profile_tracker saved_variation_id, reasons_received = get_saved_variation_id(project_config, experiment_id, user_profile_tracker.user_profile) decide_reasons.push(*reasons_received) if saved_variation_id = "Returning previously activated variation ID #{saved_variation_id} of experiment '#{experiment_key}' for user '#{user_id}' from user profile." @logger.log(Logger::INFO, ) decide_reasons.push() return VariationResult.new(nil, false, decide_reasons, saved_variation_id) end end # Check audience conditions user_meets_audience_conditions, reasons_received = Audience.user_meets_audience_conditions?(project_config, experiment, user_context, @logger) decide_reasons.push(*reasons_received) unless user_meets_audience_conditions = "User '#{user_id}' does not meet the conditions to be in experiment '#{experiment_key}'." @logger.log(Logger::INFO, ) decide_reasons.push() return VariationResult.new(nil, false, decide_reasons, nil) end # Check if this is a CMAB experiment # If so, handle CMAB-specific traffic allocation and decision logic. # Otherwise, proceed with standard bucketing logic for non-CMAB experiments. if experiment.key?('cmab') cmab_decision_result = get_decision_for_cmab_experiment(project_config, experiment, user_context, bucketing_id, ) decide_reasons.push(*cmab_decision_result.reasons) if cmab_decision_result.error # CMAB decision failed, return error return VariationResult.new(nil, true, decide_reasons, nil) end cmab_decision = cmab_decision_result.result variation_id = cmab_decision&.variation_id cmab_uuid = cmab_decision&.cmab_uuid variation = variation_id ? project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) : nil else # Bucket normally variation, bucket_reasons = @bucketer.bucket(project_config, experiment, bucketing_id, user_id) decide_reasons.push(*bucket_reasons) variation_id = variation ? variation['id'] : nil cmab_uuid = nil end variation_key = variation['key'] if variation = if variation_id "User '#{user_id}' is in variation '#{variation_key}' of experiment '#{experiment_id}'." else "User '#{user_id}' is in no variation." end @logger.log(Logger::INFO, ) decide_reasons.push() if # Persist bucketing decision user_profile_tracker.update_user_profile(experiment_id, variation_id) unless should_ignore_user_profile_service && user_profile_tracker VariationResult.new(cmab_uuid, false, decide_reasons, variation_id) end |
#get_variation_for_feature(project_config, feature_flag, user_context, decide_options = []) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/optimizely/decision_service.rb', line 162 def get_variation_for_feature(project_config, feature_flag, user_context, = []) # Get the variation the user is bucketed into for the given FeatureFlag. # # project_config - project_config - Instance of ProjectConfig # feature_flag - The feature flag the user wants to access # user_context - Optimizely user context instance # # Returns DecisionResult struct. holdouts = project_config.get_holdouts_for_flag(feature_flag['id']) if holdouts && !holdouts.empty? # Has holdouts - use get_decision_for_flag which checks holdouts first get_decision_for_flag(feature_flag, user_context, project_config, ) else get_variations_for_feature_list(project_config, [feature_flag], user_context, ).first end end |
#get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options = []) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/optimizely/decision_service.rb', line 329 def get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, = []) # Gets the variation the user is bucketed into for the feature flag's experiment. # # project_config - project_config - Instance of ProjectConfig # feature_flag - The feature flag the user wants to access # user_context - Optimizely user context instance # # Returns a DecisionResult containing the decision (or nil if not bucketed), # an error flag, and an array of decision reasons. decide_reasons = [] user_id = user_context.user_id feature_flag_key = feature_flag['key'] if feature_flag['experimentIds'].empty? = "The feature flag '#{feature_flag_key}' is not used in any experiments." @logger.log(Logger::DEBUG, ) decide_reasons.push() return DecisionResult.new(nil, false, decide_reasons) end # Evaluate each experiment and return the first bucketed experiment variation feature_flag['experimentIds'].each do |experiment_id| experiment = project_config.experiment_id_map[experiment_id] unless experiment = "Feature flag experiment with ID '#{experiment_id}' is not in the datafile." @logger.log(Logger::DEBUG, ) decide_reasons.push() return DecisionResult.new(nil, false, decide_reasons) end experiment_id = experiment['id'] variation_result = get_variation_from_experiment_rule(project_config, feature_flag_key, experiment, user_context, user_profile_tracker, ) error = variation_result.error reasons_received = variation_result.reasons variation_id = variation_result.variation_id cmab_uuid = variation_result.cmab_uuid decide_reasons.push(*reasons_received) # If there's an error, return immediately instead of falling back to next experiment return DecisionResult.new(nil, error, decide_reasons) if error next unless variation_id variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id) variation = project_config.get_variation_from_flag(feature_flag['key'], variation_id, 'id') if variation.nil? decision = Decision.new(experiment, variation, DECISION_SOURCES['FEATURE_TEST'], cmab_uuid) return DecisionResult.new(decision, error, decide_reasons) end = "The user '#{user_id}' is not bucketed into any of the experiments on the feature '#{feature_flag_key}'." @logger.log(Logger::INFO, ) decide_reasons.push() DecisionResult.new(nil, false, decide_reasons) end |
#get_variation_for_feature_rollout(project_config, feature_flag, user_context) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/optimizely/decision_service.rb', line 385 def get_variation_for_feature_rollout(project_config, feature_flag, user_context) # Determine which variation the user is in for a given rollout. # Returns the variation of the first experiment the user qualifies for. # # project_config - project_config - Instance of ProjectConfig # feature_flag - The feature flag the user wants to access # user_context - Optimizely user context instance # # Returns a DecisionResult containing the decision (or nil if not bucketed), # an error flag, and an array of decision reasons. decide_reasons = [] rollout_id = feature_flag['rolloutId'] feature_flag_key = feature_flag['key'] if rollout_id.nil? || rollout_id.empty? = "Feature flag '#{feature_flag_key}' is not used in a rollout." @logger.log(Logger::DEBUG, ) decide_reasons.push() return DecisionResult.new(nil, false, decide_reasons) end rollout = project_config.get_rollout_from_id(rollout_id) if rollout.nil? = "Rollout with ID '#{rollout_id}' is not in the datafile '#{feature_flag['key']}'" @logger.log(Logger::DEBUG, ) decide_reasons.push() return DecisionResult.new(nil, false, decide_reasons) end return DecisionResult.new(nil, false, decide_reasons) if rollout['experiments'].empty? index = 0 rollout_rules = rollout['experiments'] while index < rollout_rules.length variation, skip_to_everyone_else, reasons_received = get_variation_from_delivery_rule(project_config, feature_flag_key, rollout_rules, index, user_context) decide_reasons.push(*reasons_received) if variation rule = rollout_rules[index] feature_decision = Decision.new(rule, variation, DECISION_SOURCES['ROLLOUT'], nil) return DecisionResult.new(feature_decision, false, decide_reasons) end index = skip_to_everyone_else ? (rollout_rules.length - 1) : (index + 1) end DecisionResult.new(nil, false, decide_reasons) end |
#get_variation_for_holdout(holdout, user_context, project_config) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/optimizely/decision_service.rb', line 240 def get_variation_for_holdout(holdout, user_context, project_config) # Get the variation for holdout # # holdout - The holdout configuration # user_context - The user context # project_config - The project config # # Returns a DecisionResult for the holdout decide_reasons = [] user_id = user_context.user_id attributes = user_context.user_attributes if holdout.nil? || holdout['status'].nil? || holdout['status'] != 'Running' key = holdout && holdout['key'] ? holdout['key'] : 'unknown' = "Holdout '#{key}' is not running." @logger.log(Logger::INFO, ) decide_reasons.push() return DecisionResult.new(nil, false, decide_reasons) end bucketing_id, bucketing_id_reasons = get_bucketing_id(user_id, attributes) decide_reasons.push(*bucketing_id_reasons) # Check audience conditions user_meets_audience_conditions, reasons_received = Audience.user_meets_audience_conditions?(project_config, holdout, user_context, @logger) decide_reasons.push(*reasons_received) unless user_meets_audience_conditions = "User '#{user_id}' does not meet the conditions for holdout '#{holdout['key']}'." @logger.log(Logger::DEBUG, ) decide_reasons.push() return DecisionResult.new(nil, false, decide_reasons) end # Bucket user into holdout variation variation, bucket_reasons = @bucketer.bucket(project_config, holdout, bucketing_id, user_id) decide_reasons.push(*bucket_reasons) if variation && !variation['key'].nil? && !variation['key'].empty? = "The user '#{user_id}' is bucketed into variation '#{variation['key']}' of holdout '#{holdout['key']}'." @logger.log(Logger::INFO, ) decide_reasons.push() holdout_decision = Decision.new(holdout, variation, DECISION_SOURCES['HOLDOUT'], nil) DecisionResult.new(holdout_decision, false, decide_reasons) else = "The user '#{user_id}' is not bucketed into holdout '#{holdout['key']}'." @logger.log(Logger::DEBUG, ) decide_reasons.push() DecisionResult.new(nil, false, decide_reasons) end end |
#get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index, user_context) ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/optimizely/decision_service.rb', line 455 def get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index, user_context) # Determine which variation the user is in for a given rollout. # Returns the variation from delivery rules. # # project_config - project_config - Instance of ProjectConfig # flag_key - The feature flag the user wants to access # rule - An experiment rule key # user_context - Optimizely user context instance # # Returns variation, boolean to skip for eveyone else rule and reasons reasons = [] skip_to_everyone_else = false rule = rules[rule_index] context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(flag_key, rule['key']) variation, forced_reasons = validated_forced_decision(project_config, context, user_context) reasons.push(*forced_reasons) return [variation, skip_to_everyone_else, reasons] if variation user_id = user_context.user_id attributes = user_context.user_attributes bucketing_id, bucketing_id_reasons = get_bucketing_id(user_id, attributes) reasons.push(*bucketing_id_reasons) everyone_else = (rule_index == rules.length - 1) logging_key = everyone_else ? 'Everyone Else' : (rule_index + 1).to_s user_meets_audience_conditions, reasons_received = Audience.user_meets_audience_conditions?(project_config, rule, user_context, @logger, 'ROLLOUT_AUDIENCE_EVALUATION_LOGS', logging_key) reasons.push(*reasons_received) unless user_meets_audience_conditions = "User '#{user_id}' does not meet the conditions for targeting rule '#{logging_key}'." @logger.log(Logger::DEBUG, ) reasons.push() return [nil, skip_to_everyone_else, reasons] end = "User '#{user_id}' meets the audience conditions for targeting rule '#{logging_key}'." @logger.log(Logger::DEBUG, ) reasons.push() bucket_variation, bucket_reasons = @bucketer.bucket(project_config, rule, bucketing_id, user_id) reasons.push(*bucket_reasons) if bucket_variation = "User '#{user_id}' is in the traffic group of targeting rule '#{logging_key}'." @logger.log(Logger::DEBUG, ) reasons.push() elsif !everyone_else = "User '#{user_id}' is not in the traffic group for targeting rule '#{logging_key}'." @logger.log(Logger::DEBUG, ) reasons.push() skip_to_everyone_else = true end [bucket_variation, skip_to_everyone_else, reasons] end |
#get_variation_from_experiment_rule(project_config, flag_key, rule, user, user_profile_tracker, options = []) ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/optimizely/decision_service.rb', line 433 def get_variation_from_experiment_rule(project_config, flag_key, rule, user, user_profile_tracker, = []) # Determine which variation the user is in for a given rollout. # Returns the variation from experiment rules. # # project_config - project_config - Instance of ProjectConfig # flag_key - The feature flag the user wants to access # rule - An experiment rule key # user - Optimizely user context instance # # Returns variation_id and reasons reasons = [] context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(flag_key, rule['key']) variation, forced_reasons = validated_forced_decision(project_config, context, user) reasons.push(*forced_reasons) return VariationResult.new(nil, false, reasons, variation['id']) if variation variation_result = get_variation(project_config, rule['id'], user, user_profile_tracker, ) variation_result.reasons = reasons + variation_result.reasons variation_result end |
#get_variations_for_feature_list(project_config, feature_flags, user_context, decide_options = []) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/optimizely/decision_service.rb', line 294 def get_variations_for_feature_list(project_config, feature_flags, user_context, = []) # Returns the list of experiment/variation the user is bucketed in for the given list of features. # # Args: # project_config: Instance of ProjectConfig. # feature_flags: Array of features for which we are determining if it is enabled or not for the given user. # user_context: User context for user. # decide_options: Decide options. # # Returns: # Array of DecisionResult struct. ignore_ups = .include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE user_profile_tracker = nil unless ignore_ups && @user_profile_service user_id = user_context.user_id user_profile_tracker = UserProfileTracker.new(user_id, @user_profile_service, @logger) user_profile_tracker.load_user_profile end decisions = [] feature_flags.each do |feature_flag| # check if the feature is being experiment on and whether the user is bucketed into the experiment decision_result = get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, ) # Only process rollout if no experiment decision was found and no error if decision_result.decision.nil? && !decision_result.error decision_result_rollout = get_variation_for_feature_rollout(project_config, feature_flag, user_context) unless decision_result.decision decision_result.decision = decision_result_rollout.decision decision_result.reasons.push(*decision_result_rollout.reasons) end decisions << decision_result end user_profile_tracker&.save_user_profile decisions end |
#set_forced_variation(project_config, experiment_key, user_id, variation_key) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/optimizely/decision_service.rb', line 512 def set_forced_variation(project_config, experiment_key, user_id, variation_key) # Sets a Hash of user IDs to a Hash of experiments to forced variations. # # project_config - Instance of ProjectConfig # experiment_key - String Key for experiment # user_id - String ID for user. # variation_key - String Key for variation. If null, then clear the existing experiment-to-variation mapping # # Returns a boolean value that indicates if the set completed successfully experiment = project_config.get_experiment_from_key(experiment_key) experiment_id = experiment['id'] if experiment # check if the experiment exists in the datafile return false if experiment_id.nil? || experiment_id.empty? # clear the forced variation if the variation key is null if variation_key.nil? @forced_variation_map[user_id].delete(experiment_id) if @forced_variation_map.key? user_id @logger.log(Logger::DEBUG, "Variation mapped to experiment '#{experiment_key}' has been removed for user "\ "'#{user_id}'.") return true end variation_id = project_config.get_variation_id_from_key_by_experiment_id(experiment_id, variation_key) # check if the variation exists in the datafile unless variation_id # this case is logged in get_variation_id_from_key return false end @forced_variation_map[user_id] = {} unless @forced_variation_map.key? user_id @forced_variation_map[user_id][experiment_id] = variation_id @logger.log(Logger::DEBUG, "Set variation '#{variation_id}' for experiment '#{experiment_id}' and "\ "user '#{user_id}' in the forced variation map.") true end |
#validated_forced_decision(project_config, context, user_context) ⇒ Object
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
# File 'lib/optimizely/decision_service.rb', line 596 def validated_forced_decision(project_config, context, user_context) decision = user_context.get_forced_decision(context) flag_key = context[:flag_key] rule_key = context[:rule_key] variation_key = decision ? decision[:variation_key] : decision reasons = [] target = rule_key ? "flag (#{flag_key}), rule (#{rule_key})" : "flag (#{flag_key})" if variation_key variation = project_config.get_variation_from_flag(flag_key, variation_key, 'key') if variation reason = "Variation (#{variation_key}) is mapped to #{target} and user (#{user_context.user_id}) in the forced decision map." reasons.push(reason) return variation, reasons else reason = "Invalid variation is mapped to #{target} and user (#{user_context.user_id}) in the forced decision map." reasons.push(reason) end end [nil, reasons] end |