Class: Optimizely::DecisionService

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/decision_service.rb

Defined Under Namespace

Classes: Decision

Constant Summary collapse

DECISION_SOURCES =
{
  'EXPERIMENT' => 'experiment',
  'FEATURE_TEST' => 'feature-test',
  'ROLLOUT' => 'rollout'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, user_profile_service = nil) ⇒ DecisionService

Returns a new instance of DecisionService.



48
49
50
51
52
53
# File 'lib/optimizely/decision_service.rb', line 48

def initialize(logger,  = nil)
  @logger = logger
  @user_profile_service = 
  @bucketer = Bucketer.new(logger)
  @forced_variation_map = {}
end

Instance Attribute Details

#bucketerObject (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):

  1. Check experiment status

  2. Check forced bucketing

  3. Check whitelisting

  4. Check user profile service for past bucketing decisions (sticky bucketing)

  5. Check audience targeting

  6. Use Murmurhash3 to bucket the user



34
35
36
# File 'lib/optimizely/decision_service.rb', line 34

def bucketer
  @bucketer
end

#forced_variation_mapObject (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



374
375
376
377
378
379
380
381
382
383
384
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
# File 'lib/optimizely/decision_service.rb', line 374

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
    message = "User '#{user_id}' is not in the forced variation map."
    @logger.log(Logger::DEBUG, message)
    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
    message = "No experiment '#{experiment_id}' mapped to user '#{user_id}' in the forced variation map."
    @logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)
    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?

  message = "Variation '#{variation_key}' is mapped to experiment '#{experiment_id}' and user '#{user_id}' in the forced variation map"
  @logger.log(Logger::DEBUG, message)
  decide_reasons.push(message)

  [variation, decide_reasons]
end

#get_variation(project_config, experiment_id, user_context, decide_options = []) ⇒ Object



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/optimizely/decision_service.rb', line 55

def get_variation(project_config, experiment_id, user_context, decide_options = [])
  # 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
  #
  # Returns variation ID where visitor will be bucketed
  #   (nil if experiment is inactive or user does not meet audience conditions)

  decide_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 nil, decide_reasons if experiment.nil?

  experiment_key = experiment['key']
  unless project_config.experiment_running?(experiment)
    message = "Experiment '#{experiment_key}' is not running."
    @logger.log(Logger::INFO, message)
    decide_reasons.push(message)
    return nil, decide_reasons
  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 forced_variation['id'], decide_reasons 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 whitelisted_variation_id, decide_reasons if whitelisted_variation_id

   = decide_options.include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE
  # Check for saved bucketing decisions if decide_options do not include ignoreUserProfileService
  unless 
    , reasons_received = (user_id)
    decide_reasons.push(*reasons_received)
    saved_variation_id, reasons_received = get_saved_variation_id(project_config, experiment_id, )
    decide_reasons.push(*reasons_received)
    if saved_variation_id
      message = "Returning previously activated variation ID #{saved_variation_id} of experiment '#{experiment_key}' for user '#{user_id}' from user profile."
      @logger.log(Logger::INFO, message)
      decide_reasons.push(message)
      return saved_variation_id, decide_reasons
    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
    message = "User '#{user_id}' does not meet the conditions to be in experiment '#{experiment_key}'."
    @logger.log(Logger::INFO, message)
    decide_reasons.push(message)
    return nil, decide_reasons
  end

  # 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

  message = ''
  if variation_id
    variation_key = variation['key']
    message = "User '#{user_id}' is in variation '#{variation_key}' of experiment '#{experiment_id}'."
  else
    message = "User '#{user_id}' is in no variation."
  end
  @logger.log(Logger::INFO, message)
  decide_reasons.push(message)

  # Persist bucketing decision
  (, experiment_id, variation_id) unless 
  [variation_id, decide_reasons]
end

#get_variation_for_feature(project_config, feature_flag, user_context, decide_options = []) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/optimizely/decision_service.rb', line 138

def get_variation_for_feature(project_config, feature_flag, user_context, decide_options = [])
  # 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 Decision struct (nil if the user is not bucketed into any of the experiments on the feature)

  decide_reasons = []

  # check if the feature is being experiment on and whether the user is bucketed into the experiment
  decision, reasons_received = get_variation_for_feature_experiment(project_config, feature_flag, user_context, decide_options)
  decide_reasons.push(*reasons_received)
  return decision, decide_reasons unless decision.nil?

  decision, reasons_received = get_variation_for_feature_rollout(project_config, feature_flag, user_context)
  decide_reasons.push(*reasons_received)

  [decision, decide_reasons]
end

#get_variation_for_feature_experiment(project_config, feature_flag, user_context, decide_options = []) ⇒ Object



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
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/optimizely/decision_service.rb', line 160

def get_variation_for_feature_experiment(project_config, feature_flag, user_context, decide_options = [])
  # 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 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
  decide_reasons = []
  user_id = user_context.user_id
  feature_flag_key = feature_flag['key']
  if feature_flag['experimentIds'].empty?
    message = "The feature flag '#{feature_flag_key}' is not used in any experiments."
    @logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)
    return nil, 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
      message = "Feature flag experiment with ID '#{experiment_id}' is not in the datafile."
      @logger.log(Logger::DEBUG, message)
      decide_reasons.push(message)
      return nil, decide_reasons
    end

    experiment_id = experiment['id']
    variation_id, reasons_received = get_variation_from_experiment_rule(project_config, feature_flag_key, experiment, user_context, decide_options)
    decide_reasons.push(*reasons_received)

    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?

    return Decision.new(experiment, variation, DECISION_SOURCES['FEATURE_TEST']), decide_reasons
  end

  message = "The user '#{user_id}' is not bucketed into any of the experiments on the feature '#{feature_flag_key}'."
  @logger.log(Logger::INFO, message)
  decide_reasons.push(message)

  [nil, decide_reasons]
end

#get_variation_for_feature_rollout(project_config, feature_flag, user_context) ⇒ Object



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
# File 'lib/optimizely/decision_service.rb', line 208

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 the Decision struct or nil if not bucketed into any of the targeting rules
  decide_reasons = []

  rollout_id = feature_flag['rolloutId']
  feature_flag_key = feature_flag['key']
  if rollout_id.nil? || rollout_id.empty?
    message = "Feature flag '#{feature_flag_key}' is not used in a rollout."
    @logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)
    return nil, decide_reasons
  end

  rollout = project_config.get_rollout_from_id(rollout_id)
  if rollout.nil?
    message = "Rollout with ID '#{rollout_id}' is not in the datafile '#{feature_flag['key']}'"
    @logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)
    return nil, decide_reasons
  end

  return nil, 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'])
      return [feature_decision, decide_reasons]
    end

    index = skip_to_everyone_else ? (rollout_rules.length - 1) : (index + 1)
  end

  [nil, decide_reasons]
end

#get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index, user_context) ⇒ Object



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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/optimizely/decision_service.rb', line 279

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
    message = "User '#{user_id}' does not meet the conditions for targeting rule '#{logging_key}'."
    @logger.log(Logger::DEBUG, message)
    reasons.push(message)
    return [nil, skip_to_everyone_else, reasons]
  end

  message = "User '#{user_id}' meets the audience conditions for targeting rule '#{logging_key}'."
  @logger.log(Logger::DEBUG, message)
  reasons.push(message)
  bucket_variation, bucket_reasons = @bucketer.bucket(project_config, rule, bucketing_id, user_id)

  reasons.push(*bucket_reasons)

  if bucket_variation
    message = "User '#{user_id}' is in the traffic group of targeting rule '#{logging_key}'."
    @logger.log(Logger::DEBUG, message)
    reasons.push(message)
  elsif !everyone_else
    message = "User '#{user_id}' is not in the traffic group for targeting rule '#{logging_key}'."
    @logger.log(Logger::DEBUG, message)
    reasons.push(message)
    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, options = []) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/optimizely/decision_service.rb', line 255

def get_variation_from_experiment_rule(project_config, flag_key, rule, user, options = [])
  # 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 [variation['id'], reasons] if variation

  variation_id, response_reasons = get_variation(project_config, rule['id'], user, options)
  reasons.push(*response_reasons)

  [variation_id, reasons]
end

#set_forced_variation(project_config, experiment_key, user_id, variation_key) ⇒ Object



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
# File 'lib/optimizely/decision_service.rb', line 336

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



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/optimizely/decision_service.rb', line 420

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