Class: Optimizely::DecisionService
- Inherits:
-
Object
- Object
- Optimizely::DecisionService
- Defined in:
- lib/optimizely/decision_service.rb
Defined Under Namespace
Classes: Decision
Constant Summary collapse
- DECISION_SOURCES =
{ 'FEATURE_TEST' => 'feature-test', 'ROLLOUT' => 'rollout' }.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_forced_variation(project_config, experiment_key, user_id) ⇒ Object
- #get_variation(project_config, experiment_key, user_id, attributes = nil) ⇒ Object
- #get_variation_for_feature(project_config, feature_flag, user_id, attributes = nil) ⇒ Object
- #get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes = nil) ⇒ Object
- #get_variation_for_feature_rollout(project_config, feature_flag, user_id, attributes = nil) ⇒ Object
-
#initialize(logger, user_profile_service = nil) ⇒ DecisionService
constructor
A new instance of DecisionService.
- #set_forced_variation(project_config, experiment_key, user_id, variation_key) ⇒ Object
Constructor Details
#initialize(logger, user_profile_service = nil) ⇒ DecisionService
Returns a new instance of DecisionService.
47 48 49 50 51 52 |
# File 'lib/optimizely/decision_service.rb', line 47 def initialize(logger, user_profile_service = nil) @logger = logger @user_profile_service = user_profile_service @bucketer = Bucketer.new(logger) @forced_variation_map = {} 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
-
Use Murmurhash3 to bucket the user
34 35 36 |
# File 'lib/optimizely/decision_service.rb', line 34 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.
38 39 40 |
# File 'lib/optimizely/decision_service.rb', line 38 def forced_variation_map @forced_variation_map end |
Instance Method Details
#get_forced_variation(project_config, experiment_key, user_id) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/optimizely/decision_service.rb', line 312 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 unless @forced_variation_map.key? user_id @logger.log(Logger::DEBUG, "User '#{user_id}' is not in the forced variation map.") return nil 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 if experiment_id.nil? || experiment_id.empty? unless experiment_to_variation_map.key? experiment_id @logger.log(Logger::DEBUG, "No experiment '#{experiment_key}' mapped to user '#{user_id}' "\ 'in the forced variation map.') return nil end variation_id = experiment_to_variation_map[experiment_id] variation_key = '' variation = project_config.get_variation_from_id(experiment_key, 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 if variation_key.empty? @logger.log(Logger::DEBUG, "Variation '#{variation_key}' is mapped to experiment '#{experiment_key}' "\ "and user '#{user_id}' in the forced variation map") variation end |
#get_variation(project_config, experiment_key, user_id, attributes = nil) ⇒ Object
54 55 56 57 58 59 60 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 |
# File 'lib/optimizely/decision_service.rb', line 54 def get_variation(project_config, experiment_key, user_id, attributes = nil) # Determines variation into which user will be bucketed. # # project_config - project_config - Instance of ProjectConfig # experiment_key - Experiment for which visitor variation needs to be determined # user_id - String ID for user # attributes - Hash representing user attributes # # Returns variation ID where visitor will be bucketed # (nil if experiment is inactive or user does not meet audience conditions) # By default, the bucketing ID should be the user ID bucketing_id = get_bucketing_id(user_id, attributes) # Check to make sure experiment is active experiment = project_config.get_experiment_from_key(experiment_key) return nil if experiment.nil? experiment_id = experiment['id'] unless project_config.experiment_running?(experiment) @logger.log(Logger::INFO, "Experiment '#{experiment_key}' is not running.") return nil end # Check if a forced variation is set for the user forced_variation = get_forced_variation(project_config, experiment_key, user_id) return forced_variation['id'] if forced_variation # Check if user is in a white-listed variation whitelisted_variation_id = get_whitelisted_variation_id(project_config, experiment_key, user_id) return whitelisted_variation_id if whitelisted_variation_id # Check for saved bucketing decisions user_profile = get_user_profile(user_id) saved_variation_id = get_saved_variation_id(project_config, experiment_id, user_profile) if saved_variation_id @logger.log( Logger::INFO, "Returning previously activated variation ID #{saved_variation_id} of experiment '#{experiment_key}' for user '#{user_id}' from user profile." ) return saved_variation_id end # Check audience conditions unless Audience.user_in_experiment?(project_config, experiment, attributes, @logger) @logger.log( Logger::INFO, "User '#{user_id}' does not meet the conditions to be in experiment '#{experiment_key}'." ) return nil end # Bucket normally variation = @bucketer.bucket(project_config, experiment, bucketing_id, user_id) variation_id = variation ? variation['id'] : nil # Persist bucketing decision save_user_profile(user_profile, experiment_id, variation_id) variation_id end |
#get_variation_for_feature(project_config, feature_flag, user_id, attributes = nil) ⇒ Object
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 |
# File 'lib/optimizely/decision_service.rb', line 114 def get_variation_for_feature(project_config, feature_flag, user_id, attributes = nil) # 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_id - String ID for the user # attributes - Hash representing user attributes # # Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature) # check if the feature is being experiment on and whether the user is bucketed into the experiment decision = get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes) return decision unless decision.nil? feature_flag_key = feature_flag['key'] decision = get_variation_for_feature_rollout(project_config, feature_flag, user_id, attributes) if decision @logger.log( Logger::INFO, "User '#{user_id}' is bucketed into a rollout for feature flag '#{feature_flag_key}'." ) return decision end @logger.log( Logger::INFO, "User '#{user_id}' is not bucketed into a rollout for feature flag '#{feature_flag_key}'." ) nil end |
#get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes = nil) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/optimizely/decision_service.rb', line 145 def get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes = nil) # 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_id - String ID for the user # attributes - Hash representing user attributes # # Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature) # or nil if the user is not bucketed into any of the experiments on the feature feature_flag_key = feature_flag['key'] if feature_flag['experimentIds'].empty? @logger.log( Logger::DEBUG, "The feature flag '#{feature_flag_key}' is not used in any experiments." ) return nil 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 @logger.log( Logger::DEBUG, "Feature flag experiment with ID '#{experiment_id}' is not in the datafile." ) return nil end experiment_key = experiment['key'] variation_id = get_variation(project_config, experiment_key, user_id, attributes) next unless variation_id variation = project_config.variation_id_map[experiment_key][variation_id] @logger.log( Logger::INFO, "The user '#{user_id}' is bucketed into experiment '#{experiment_key}' of feature '#{feature_flag_key}'." ) return Decision.new(experiment, variation, DECISION_SOURCES['FEATURE_TEST']) end @logger.log( Logger::INFO, "The user '#{user_id}' is not bucketed into any of the experiments on the feature '#{feature_flag_key}'." ) nil end |
#get_variation_for_feature_rollout(project_config, feature_flag, user_id, attributes = nil) ⇒ Object
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 239 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 |
# File 'lib/optimizely/decision_service.rb', line 196 def get_variation_for_feature_rollout(project_config, feature_flag, user_id, attributes = nil) # 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_id - String ID for the user # attributes - Hash representing user attributes # # Returns the Decision struct or nil if not bucketed into any of the targeting rules bucketing_id = get_bucketing_id(user_id, attributes) rollout_id = feature_flag['rolloutId'] if rollout_id.nil? || rollout_id.empty? feature_flag_key = feature_flag['key'] @logger.log( Logger::DEBUG, "Feature flag '#{feature_flag_key}' is not used in a rollout." ) return nil end rollout = project_config.get_rollout_from_id(rollout_id) if rollout.nil? @logger.log( Logger::DEBUG, "Rollout with ID '#{rollout_id}' is not in the datafile '#{feature_flag['key']}'" ) return nil end return nil if rollout['experiments'].empty? rollout_rules = rollout['experiments'] number_of_rules = rollout_rules.length - 1 # Go through each experiment in order and try to get the variation for the user number_of_rules.times do |index| rollout_rule = rollout_rules[index] audience_id = rollout_rule['audienceIds'][0] audience = project_config.get_audience_from_id(audience_id) audience_name = audience['name'] # Check that user meets audience conditions for targeting rule unless Audience.user_in_experiment?(project_config, rollout_rule, attributes, @logger) @logger.log( Logger::DEBUG, "User '#{user_id}' does not meet the conditions to be in rollout rule for audience '#{audience_name}'." ) # move onto the next targeting rule next end # Evaluate if user satisfies the traffic allocation for this rollout rule variation = @bucketer.bucket(project_config, rollout_rule, bucketing_id, user_id) return Decision.new(rollout_rule, variation, DECISION_SOURCES['ROLLOUT']) unless variation.nil? break end # get last rule which is the everyone else rule everyone_else_experiment = rollout_rules[number_of_rules] # Check that user meets audience conditions for last rule unless Audience.user_in_experiment?(project_config, everyone_else_experiment, attributes, @logger) audience_id = everyone_else_experiment['audienceIds'][0] audience = project_config.get_audience_from_id(audience_id) audience_name = audience['name'] @logger.log( Logger::DEBUG, "User '#{user_id}' does not meet the conditions to be in rollout rule for audience '#{audience_name}'." ) return nil end variation = @bucketer.bucket(project_config, everyone_else_experiment, bucketing_id, user_id) return Decision.new(everyone_else_experiment, variation, DECISION_SOURCES['ROLLOUT']) unless variation.nil? nil end |
#set_forced_variation(project_config, experiment_key, user_id, variation_key) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/optimizely/decision_service.rb', line 274 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(experiment_key, 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 |