8000 fix: Forced variation not available in experiment by ozayr-zaviar · Pull Request #292 · optimizely/ruby-sdk · GitHub
[go: up one dir, main page]

Skip to content

fix: Forced variation not available in experiment #292

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 4 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 18 additions & 9 deletions lib/optimizely.rb
decisions
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def decide(user_context, key, decide_options = [])
experiment = decision.experiment
rule_key = experiment ? experiment['key'] : nil
variation = decision['variation']
variation_key = variation['key']
feature_enabled = variation['featureEnabled']
variation_key = variation ? variation['key'] : nil
feature_enabled = variation ? variation['featureEnabled'] : false
decision_source = decision.source
end

Expand Down Expand Up @@ -298,8 +298,17 @@ def decide_for_keys(user_context, keys, decide_options = [])
end

def get_flag_variation_by_key(flag_key, variation_key)
project_config.get_variation_from_flag(flag_key, variation_key)
# Gets variation using variation key or id and flag key.
#
# @param flag_key - flag key from which the variation is required.
# @param target_value - variation value either id or key that will be matched.
# @param attribute - string representing variation attribute.
#
# @return [variation]
# @return [nil] if no variation found in flag_variation_map.

def get_flag_variation(flag_key, target_value, attribute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add description of this method.

project_config.get_variation_from_flag(flag_key, target_value, attribute)
end

# Buckets visitor and sends impression event to Optimizely.
Expand Down Expand Up @@ -1098,11 +1107,11 @@ def send_impression(config, experiment, variation_key, flag_key, rule_key, enabl
experiment_id = experiment['id']
experiment_key = experiment['key']

if experiment_id != ''
variation_id = config.get_variation_id_from_key_by_experiment_id(experiment_id, variation_key)
else
varaition = get_flag_variation_by_key(flag_key, variation_key)
variation_id = v 10000 araition ? varaition['id'] : ''
variation_id = config.get_variation_id_from_key_by_experiment_id(experiment_id, variation_key) unless experiment_id.empty?

unless variation_id
variation = !flag_key.empty? ? get_flag_variation(flag_key, variation_key, 'key') : nil
variation_id = variation ? variation['id'] : ''
end

metadata = {
Expand Down
4 changes: 2 additions & 2 deletions lib/optimizely/config/datafile_project_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ def get_audience_from_id(audience_id)
nil
end

def get_variation_from_flag(flag_key, variation_key)
def get_variation_from_flag(flag_key, target_value, attribute)
variations = @flag_variation_map[flag_key]
return variations.select { |variation| variation['key'] == variation_key }.first if variations
return variations.select { |variation| variation[attribute] == target_value }.first if variations

nil
end
Expand Down
1 change: 1 addition & 0 deletions lib/optimizely/decision_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_cont
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
Expand Down
2 changes: 1 addition & 1 deletion lib/optimizely/optimizely_user_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def find_validated_forced_decision(context)
reasons = []
target = rule_key ? "flag (#{flag_key}), rule (#{rule_key})" : "flag (#{flag_key})"
if variation_key
variation = @optimizely_client.get_flag_variation_by_key(flag_key, variation_key)
variation = @optimizely_client.get_flag_variation(flag_key, variation_key, 'key')
if variation
reason = "Variation (#{variation_key}) is mapped to #{target} and user (#{@user_id}) in the forced decision map."
reasons.push(reason)
Expand Down
23 changes: 23 additions & 0 deletions spec/optimizely_user_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@
expect(decision.reasons).to eq(['Variation (b) is mapped to flag (feature_1), rule (exp_with_audience) and user (tester) in the forced decision map.'])
end

it 'should return an expected decision object when forced decision is called and variation of different experiment but same flag key' do
user_id = 'tester'
feature_key = 'feature_1'
original_attributes = {}
user_context_obj = Optimizely::OptimizelyUserContext.new(forced_decision_project_instance, user_id, original_attributes)
context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, 'exp_with_audience')
forced_decision = Optimizely::OptimizelyUserContext::OptimizelyForcedDecision.new('3324490633')
user_context_obj.set_forced_decision(context, forced_decision)
expected = expect do
decision = user_context_obj.decide(feature_key, [Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS])
expect(decision.variation_key).to eq('3324490633')
expect(decision.rule_key).to eq('exp_with_audience')
expect(decision.enabled).to be false
expect(decision.flag_key).to eq(feature_key)
expect(decision.user_context.user_id).to eq(user_id)
expect(decision.user_context.user_attributes.length).to eq(0)
expect(decision.user_context.forced_decisions.length).to eq(1)
expect(decision.user_context.forced_decisions).to eq(context => forced_decision)
expect(decision.reasons).to eq(['Variation (3324490633) is mapped to flag (feature_1), rule (exp_with_audience) and user (tester) in the forced decision map.'])
end
expected.to raise_error
end

it 'should return correct variation if rule in forced decision is deleted' do
impression_log_url = 'https://logx.optimizely.com/v1/events'
time_now = Time.now
Expand Down
0