Skip to content

Commit f48dbc2

Browse files
update: Refactor DecisionService to return DecisionResult struct instead of Decision struct
1 parent 75ee816 commit f48dbc2

File tree

2 files changed

+96
-98
lines changed

2 files changed

+96
-98
lines changed

lib/optimizely/decision_service.rb

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def get_variation_for_feature(project_config, feature_flag, user_context, decide
164164
# feature_flag - The feature flag the user wants to access
165165
# user_context - Optimizely user context instance
166166
#
167-
# Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature)
167+
# Returns DecisionResult struct.
168168
get_variations_for_feature_list(project_config, [feature_flag], user_context, decide_options).first
169169
end
170170

@@ -178,7 +178,7 @@ def get_variations_for_feature_list(project_config, feature_flags, user_context,
178178
# decide_options: Decide options.
179179
#
180180
# Returns:
181-
# Array of Decision struct.
181+
# Array of DecisionResult struct.
182182
ignore_ups = decide_options.include? Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE
183183
user_profile_tracker = nil
184184
unless ignore_ups && @user_profile_service
@@ -187,18 +187,10 @@ def get_variations_for_feature_list(project_config, feature_flags, user_context,
187187
end
188188
decisions = []
189189
feature_flags.each do |feature_flag|
190-
decide_reasons = []
191190
# check if the feature is being experiment on and whether the user is bucketed into the experiment
192-
decision, reasons_received = get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options)
193-
decide_reasons.push(*reasons_received)
194-
if decision
195-
decisions << [decision, decide_reasons]
196-
else
197-
# Proceed to rollout if the decision is nil
198-
rollout_decision, reasons_received = get_variation_for_feature_rollout(project_config, feature_flag, user_context)
199-
decide_reasons.push(*reasons_received)
200-
decisions << [rollout_decision, decide_reasons]
201-
end
191+
decision_result = get_variation_for_feature_experiment(project_config, feature_flag, user_context, user_profile_tracker, decide_options)
192+
decision_result = get_variation_for_feature_rollout(project_config, feature_flag, user_context) unless decision_result.decision
193+
decisions << decision_result
202194
end
203195
user_profile_tracker&.save_user_profile
204196
decisions
@@ -211,8 +203,8 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_cont
211203
# feature_flag - The feature flag the user wants to access
212204
# user_context - Optimizely user context instance
213205
#
214-
# Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature)
215-
# or nil if the user is not bucketed into any of the experiments on the feature
206+
# Returns a DecisionResult containing the decision (or nil if not bucketed),
207+
# an error flag, and an array of decision reasons.
216208
decide_reasons = []
217209
user_id = user_context.user_id
218210
feature_flag_key = feature_flag['key']
@@ -265,7 +257,8 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context
265257
# feature_flag - The feature flag the user wants to access
266258
# user_context - Optimizely user context instance
267259
#
268-
# Returns the Decision struct or nil if not bucketed into any of the targeting rules
260+
# Returns a DecisionResult containing the decision (or nil if not bucketed),
261+
# an error flag, and an array of decision reasons.
269262
decide_reasons = []
270263

271264
rollout_id = feature_flag['rolloutId']
@@ -274,18 +267,18 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context
274267
message = "Feature flag '#{feature_flag_key}' is not used in a rollout."
275268
@logger.log(Logger::DEBUG, message)
276269
decide_reasons.push(message)
277-
return nil, decide_reasons
270+
return DecisionResult.new(nil, false, decide_reasons)
278271
end
279272

280273
rollout = project_config.get_rollout_from_id(rollout_id)
281274
if rollout.nil?
282275
message = "Rollout with ID '#{rollout_id}' is not in the datafile '#{feature_flag['key']}'"
283276
@logger.log(Logger::DEBUG, message)
284277
decide_reasons.push(message)
285-
return nil, decide_reasons
278+
return DecisionResult.new(nil, false, decide_reasons)
286279
end
287280

288-
return nil, decide_reasons if rollout['experiments'].empty?
281+
return DecisionResult.new(nil, false, decide_reasons) if rollout['experiments'].empty?
289282

290283
index = 0
291284
rollout_rules = rollout['experiments']
@@ -294,14 +287,14 @@ def get_variation_for_feature_rollout(project_config, feature_flag, user_context
294287
decide_reasons.push(*reasons_received)
295288
if variation
296289
rule = rollout_rules[index]
297-
feature_decision = Decision.new(rule, variation, DECISION_SOURCES['ROLLOUT'])
298-
return [feature_decision, decide_reasons]
290+
feature_decision = Decision.new(rule, variation, DECISION_SOURCES['ROLLOUT'], nil)
291+
return DecisionResult.new(feature_decision, false, decide_reasons)
299292
end
300293

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

304-
[nil, decide_reasons]
297+
DecisionResult.new(nil, false, decide_reasons)
305298
end
306299

307300
def get_variation_from_experiment_rule(project_config, flag_key, rule, user, user_profile_tracker, options = [])

0 commit comments

Comments
 (0)