8000 refactor: Changed the way reasons are being returned from decide APIs by zashraf1985 · Pull Request #279 · optimizely/ruby-sdk · GitHub
[go: up one dir, main page]

Skip to content

refactor: Changed the way reasons are being returned from decide APIs #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed get_forced_variation
  • Loading branch information
zashraf1985 committed Dec 16, 2020
commit 5d118861bef275e3e14af09300b62ce0042adeef
2 changes: 1 addition & 1 deletion lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def get_forced_variation(experiment_key, user_id)
config = project_config

forced_variation_key = nil
forced_variation = @decision_service.get_forced_variation(config, experiment_key, user_id)
forced_variation, = @decision_service.get_forced_variation(config, experiment_key, user_id)
forced_variation_key = forced_variation['key'] if forced_variation

forced_variation_key
Expand Down
26 changes: 14 additions & 12 deletions lib/optimizely/decision_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
end

# Check if a forced variation is set for the user
forced_variation = get_forced_variation(project_config, experiment_key, user_id, decide_reasons)
forced_variation, reasons_received = get_forced_variation(project_config, experiment_key, user_id)
decide_reasons&.push(*reasons_received)
return forced_variation['id'] if forced_variation

# Check if user is in a white-listed variation
Expand Down Expand Up @@ -141,8 +142,8 @@ def get_variation_for_feature(project_config, feature_flag, user_id, attributes
decision = get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes, decide_options, decide_reasons)
return decision unless decision.nil?

decision, reasons_recieved = get_variation_for_feature_rollout(project_config, feature_flag, user_id, attributes)
decide_reasons&.push(*reasons_recieved)
decision, reasons_received = get_variation_for_feature_rollout(project_config, feature_flag, user_id, attributes)
decide_reasons&.push(*reasons_received)

decision
end
Expand Down Expand Up @@ -313,7 +314,7 @@ def set_forced_variation(project_config, experiment_key, user_id, variation_key)
true
end

def get_forced_variation(project_config, experiment_key, user_id, decide_reasons = nil)
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
Expand All @@ -322,25 +323,26 @@ def get_forced_variation(project_config, experiment_key, user_id, decide_reasons
#
# 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)
decide_reasons&.push(message)
return nil
decide_reasons.push(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 if experiment_id.nil? || experiment_id.empty?
return nil, decide_reasons if experiment_id.nil? || experiment_id.empty?

unless experiment_to_variation_map.key? experiment_id
message = "No experiment '#{experiment_key}' mapped to user '#{user_id}' in the forced variation map."
@logger.log(Logger::DEBUG, message)
decide_reasons&.push(message)
return nil
decide_reasons.push(message)
return nil, decide_reasons
end

variation_id = experiment_to_variation_map[experiment_id]
Expand All @@ -350,13 +352,13 @@ def get_forced_variation(project_config, experiment_key, user_id, decide_reasons

# check if the variation exists in the datafile
# this case is logged in get_variation_from_id
return nil if variation_key.empty?
return nil, decide_reasons if variation_key.empty?

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

variation
[variation, decide_reasons]
end

private
Expand Down
18 changes: 10 additions & 8 deletions spec/decision_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,15 @@

# User ID is not defined in the forced variation map
it 'should log a message and return nil when user is not in forced variation map' do
expect(decision_service.get_forced_variation(config, valid_experiment[:key], user_id)).to eq(nil)
variation_received, = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
expect(variation_received).to eq(nil)
expect(spy_logger).to have_received(:log).with(Logger::DEBUG,
"User '#{user_id}' is not in the forced variation map.")
end
# Experiment key does not exist in the datafile
it 'should return nil when experiment key is not in datafile' do
expect(decision_service.get_forced_variation(config, invalid_experiment_key, user_id)).to eq(nil)
variation_received, = decision_service.get_forced_variation(config, invalid_experiment_key, user_id)
expect(variation_received).to eq(nil)
end
end

Expand Down Expand Up @@ -787,38 +789,38 @@
# Call set variation with different variations on one user/experiment to confirm that each set is expected.
it 'should set and return expected variations when different variations are set and removed for one user/experiment' do
expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation[:key])).to eq(true)
variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
variation, = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
expect(variation['id']).to eq(valid_variation[:id])
expect(variation['key']).to eq(valid_variation[:key])

expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation_2[:key])).to eq(true)
variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
variation, = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
expect(variation['id']).to eq(valid_variation_2[:id])
expect(variation['key']).to eq(valid_variation_2[:key])
end

# Set variation on multiple experiments for one user.
it 'should set and return expected variations when variation is set for multiple experiments for one user' do
expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation[:key])).to eq(true)
variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
variation, = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
expect(variation['id']).to eq(valid_variation[:id])
expect(variation['key']).to eq(valid_variation[:key])

expect(decision_service.set_forced_variation(config, valid_experiment_2[:key], user_id, valid_variation_for_exp_2[:key])).to eq(true)
variation = decision_service.get_forced_variation(config, valid_experiment_2[:key], user_id)
variation, = decision_service.get_forced_variation(config, valid_experiment_2[:key], user_id)
expect(variation['id']).to eq(valid_variation_for_exp_2[:id])
expect(variation['key']).to eq(valid_variation_for_exp_2[:key])
end

# Set variations for multiple users.
it 'should set and return expected variations when variations are set for multiple users' do
expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id, valid_variation[:key])).to eq(true)
variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
variation, = decision_service.get_forced_variation(config, valid_experiment[:key], user_id)
expect(variation['id']).to eq(valid_variation[:id])
expect(variation['key']).to eq(valid_variation[:key])

expect(decision_service.set_forced_variation(config, valid_experiment[:key], user_id_2, valid_variation[:key])).to eq(true)
variation = decision_service.get_forced_variation(config, valid_experiment[:key], user_id_2)
variation, = decision_service.get_forced_variation(config, valid_experiment[:key], user_id_2)
expect(variation['id']).to eq(valid_variation[:id])
expect(variation['key']).to eq(valid_variation[:key])
end
Expand Down
0