Class: Optimizely::DecisionService

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

Defined Under Namespace

Classes: Decision

Constant Summary collapse

DECISION_SOURCE_EXPERIMENT =
'experiment'
DECISION_SOURCE_ROLLOUT =
'rollout'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, user_profile_service = nil) ⇒ DecisionService

Returns a new instance of DecisionService.



43
44
45
46
47
# File 'lib/optimizely/decision_service.rb', line 43

def initialize(config,  = nil)
  @config = config
  @user_profile_service = 
  @bucketer = Bucketer.new(@config)
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



36
37
38
# File 'lib/optimizely/decision_service.rb', line 36

def bucketer
  @bucketer
end

#configObject (readonly)

Returns the value of attribute config.



37
38
39
# File 'lib/optimizely/decision_service.rb', line 37

def config
  @config
end

Instance Method Details

#get_variation(experiment_key, user_id, attributes = nil) ⇒ Object



49
50
51
52
53
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
# File 'lib/optimizely/decision_service.rb', line 49

def get_variation(experiment_key, user_id, attributes = nil)
  # Determines variation into which user will be bucketed.
  #
  # 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 = @config.get_experiment_from_key(experiment_key)
  return nil if experiment.nil?

  experiment_id = experiment['id']
  unless @config.experiment_running?(experiment)
    @config.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 = @config.get_forced_variation(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(experiment_key, user_id)
  return whitelisted_variation_id if whitelisted_variation_id

  # Check for saved bucketing decisions
   = (user_id)
  saved_variation_id = get_saved_variation_id(experiment_id, )
  if saved_variation_id
    @config.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?(@config, experiment, attributes)
    @config.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(experiment, bucketing_id, user_id)
  variation_id = variation ? variation['id'] : nil

  # Persist bucketing decision
  (, experiment_id, variation_id)
  variation_id
end

#get_variation_for_feature(feature_flag, user_id, attributes = nil) ⇒ Object



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 108

def get_variation_for_feature(feature_flag, user_id, attributes = nil)
  # Get the variation the user is bucketed into for the given FeatureFlag.
  #
  # 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(feature_flag, user_id, attributes)
  return decision unless decision.nil?

  feature_flag_key = feature_flag['key']
  decision = get_variation_for_feature_rollout(feature_flag, user_id, attributes)
  if decision
    @config.logger.log(
      Logger::INFO,
      "User '#{user_id}' is bucketed into a rollout for feature flag '#{feature_flag_key}'."
    )
    return decision
  end
  @config.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(feature_flag, user_id, attributes = nil) ⇒ Object



138
139
140
141
142
143
144
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
# File 'lib/optimizely/decision_service.rb', line 138

def get_variation_for_feature_experiment(feature_flag, user_id, attributes = nil)
  # Gets the variation the user is bucketed into for the feature flag's experiment.
  #
  # 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?
    @config.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 = @config.experiment_id_map[experiment_id]
    unless experiment
      @config.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(experiment_key, user_id, attributes)

    next unless variation_id
    variation = @config.variation_id_map[experiment_key][variation_id]
    @config.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_SOURCE_EXPERIMENT)
  end

  @config.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(feature_flag, user_id, attributes = nil) ⇒ Object



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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/optimizely/decision_service.rb', line 187

def get_variation_for_feature_rollout(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.
  #
  # 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']
    @config.logger.log(
      Logger::DEBUG,
      "Feature flag '#{feature_flag_key}' is not used in a rollout."
    )
    return nil
  end

  rollout = @config.get_rollout_from_id(rollout_id)
  if rollout.nil?
    @config.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 = @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?(@config, rollout_rule, attributes)
      @config.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(rollout_rule, bucketing_id, user_id)
    return Decision.new(rollout_rule, variation, DECISION_SOURCE_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?(@config, everyone_else_experiment, attributes)
    audience_id = everyone_else_experiment['audienceIds'][0]
    audience = @config.get_audience_from_id(audience_id)
    audience_name = audience['name']
    @config.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(everyone_else_experiment, bucketing_id, user_id)
  return Decision.new(everyone_else_experiment, variation, DECISION_SOURCE_ROLLOUT) unless variation.nil?
  nil
end