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_variation_for_feature
  • Loading branch information
zashraf1985 committed Dec 16, 2020
commit 63525e6a4ce83e14d5fdb195b4d4cc9af9883403
9 changes: 5 additions & 4 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def decide(user_context, key, decide_options = [])
experiment = nil
decision_source = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']

decision = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes, decide_options, reasons)
decision, reasons_received = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes, decide_options)
reasons.push(*reasons_received)

# Send impression event if Decision came from a feature test and decide options doesn't include disableDecisionEvent
if decision.is_a?(Optimizely::DecisionService::Decision)
Expand Down Expand Up @@ -489,7 +490,7 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
return false
end

decision = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes)
decision, = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes)

feature_enabled = false
source_string = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
Expand Down Expand Up @@ -738,7 +739,7 @@ def get_all_feature_variables(feature_flag_key, user_id, attributes = nil)
return nil
end

decision = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes)
decision, = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes)
variation = decision ? decision['variation'] : nil
feature_enabled = variation ? variation['featureEnabled'] : false
all_variables = {}
Expand Down Expand Up @@ -944,7 +945,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
return nil
end

decision = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes)
decision, = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes)
variation = decision ? decision['variation'] : nil
feature_enabled = variation ? variation['featureEnabled'] : false

Expand Down
12 changes: 7 additions & 5 deletions lib/optimizely/decision_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
[variation_id, decide_reasons]
end

def get_variation_for_feature(project_config, feature_flag, user_id, attributes = nil, decide_options = [], decide_reasons = nil)
def get_variation_for_feature(project_config, feature_flag, user_id, attributes = nil, decide_options = [])
# Get the variation the user is bucketed into for the given FeatureFlag.
#
# project_config - project_config - Instance of ProjectConfig
Expand All @@ -142,15 +142,17 @@ def get_variation_for_feature(project_config, feature_flag, user_id, attributes
#
# 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_id, attributes, decide_options)
decide_reasons&.push(*reasons_received)
return decision unless decision.nil?
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_id, attributes)
decide_reasons&.push(*reasons_received)
decide_reasons.push(*reasons_received)

decision
[decision, decide_reasons]
end

def get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes = nil, decide_options = [])
Expand Down
19 changes: 11 additions & 8 deletions spec/decision_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,10 @@
'experiment' => expected_experiment,
'variation' => expected_variation
}
allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return(expected_decision)
allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return([expected_decision, nil])

expect(decision_service.get_variation_for_feature(config, feature_flag, user_id, user_attributes)).to eq(expected_decision)
decision_received, = decision_service.get_variation_for_feature(config, feature_flag, user_id, user_attributes)
expect(decision_received).to eq(expected_decision)
end
8000 end

Expand All @@ -708,20 +709,22 @@
variation,
Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
)
allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return(nil)
allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return(expected_decision)
allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return([nil, nil])
allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return([expected_decision, nil])

expect(decision_service.get_variation_for_feature(config, feature_flag, user_id, user_attributes)).to eq(expected_decision)
decision_received, = decision_service.get_variation_for_feature(config, feature_flag, user_id, user_attributes)
expect(decision_received).to eq(expected_decision)
end
end

describe 'and the user is not bucketed into the feature rollout' do
it 'should log a message and return nil' do
feature_flag = config.feature_flag_key_map['string_single_variable_feature']
allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return(nil)
allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return(nil)
allow(decision_service).to receive(:get_variation_for_feature_experiment).and_return([nil, nil])
allow(decision_service).to receive(:get_variation_for_feature_rollout).and_return([nil, nil])

expect(decision_service.get_variation_for_feature(config, feature_flag, user_id, user_attributes)).to eq(nil)
decision_received, = decision_service.get_variation_for_feature(config, feature_flag, user_id, user_attributes)
expect(decision_received).to eq(nil)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3974,11 +3974,11 @@ def callback(_args); end
user_context = project_instance.create_user_context('user1')

expect(project_instance.decision_service).to receive(:get_variation_for_feature)
.with(anything, anything, anything, anything, [], []).once
.with(anything, anything, anything, anything, []).once
project_instance.decide(user_context, 'multi_variate_feature')

expect(project_instance.decision_service).to receive(:get_variation_for_feature)
.with(anything, anything, anything, anything, [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT], []).once
.with(anything, anything, anything, anything, [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT]).once
project_instance.decide(user_context, 'multi_variate_feature', [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT])

expect(project_instance.decision_service).to receive(:get_variation_for_feature)
Expand All @@ -3989,7 +3989,7 @@ def callback(_args); end
Optimizely::Decide::OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE,
Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS,
Optimizely::Decide::OptimizelyDecideOption::EXCLUDE_VARIABLES
], []).once
]).once
project_instance
.decide(user_context, 'multi_variate_feature', [
Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT,
Expand Down
0