-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from 1 commit
c4bdcb2
7adb387
055ef6f
5d11886
7cbf993
37bf64d
ce727a9
63525e6
514a93d
f13d883
2c6533a
ef8ddd5
a71680f
af181e3
5720f83
dd4add4
87894b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,33 +38,24 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg | |
# This defaults to experiment['key']. | ||
# | ||
# Returns boolean representing if user satisfies audience conditions for the audiences or not. | ||
decide_reasons = [] | ||
logging_hash ||= 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS' | ||
logging_key ||= experiment['key'] | ||
|
||
logs_hash = Object.const_get "Optimizely::Helpers::Constants::#{logging_hash}" | ||
|
||
audience_conditions = experiment['audienceConditions'] || experiment['audienceIds'] | ||
|
||
logger.log( | ||
Logger::DEBUG, | ||
format( | ||
logs_hash['EVALUATING_AUDIENCES_COMBINED'], | ||
logging_key, | ||
audience_conditions | ||
) | ||
) | ||
message = format(logs_hash['EVALUATING_AUDIENCES_COMBINED'], logging_key, audience_conditions) | ||
logger.log(Logger::DEBUG, message) | ||
decide_reasons.push(message) | ||
|
||
# Return true if there are no audiences | ||
if audience_conditions.empty? | ||
logger.log( | ||
Logger::INFO, | ||
format( | ||
logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], | ||
logging_key, | ||
'TRUE' | ||
) | ||
) | ||
return true | ||
message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, 'TRUE') | ||
logger.log(Logger::INFO) | ||
decide_reasons.push(message) | ||
return true, decide_reasons | ||
end | ||
|
||
attributes ||= {} | ||
|
@@ -80,39 +71,28 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg | |
return nil unless audience | ||
|
||
audience_conditions = audience['conditions'] | ||
logger.log( | ||
Logger::DEBUG, | ||
format( | ||
logs_hash['EVALUATING_AUDIENCE'], | ||
audience_id, | ||
audience_conditions | ||
) | ||
) | ||
message = format(logs_hash['EVALUATING_AUDIENCE'], audience_id, audience_conditions) | ||
logger.log(Logger::DEBUG, message) | ||
decide_reasons.push(message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good! I think this message should BE in reasons since it shows "audience_conditions". |
||
|
||
audience_conditions = JSON.parse(audience_conditions) if audience_conditions.is_a?(String) | ||
result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_custom_attr) | ||
result_str = result.nil? ? 'UNKNOWN' : result.to_s.upcase | ||
logger.log( | ||
Logger::DEBUG, | ||
format(logs_hash['AUDIENCE_EVALUATION_RESULT'], audience_id, result_str) | ||
) | ||
message = format(logs_hash['AUDIENCE_EVALUATION_RESULT'], audience_id, result_str) | ||
logger.log(Logger::DEBUG, message) | ||
decide_reasons.push(message) | ||
|
||
result | ||
end | ||
|
||
eval_result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_audience) | ||
|
||
eval_result ||= false | ||
|
||
logger.log( | ||
Logger::INFO, | ||
format( | ||
logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], | ||
logging_key, | ||
eval_result.to_s.upcase | ||
) | ||
) | ||
message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, eval_result.to_s.upcase) | ||
logger.log(Logger::INFO, message) | ||
decide_reasons.push(message) | ||
|
||
eval_result | ||
[eval_result, decide_reasons] | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,27 +34,25 @@ | |
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be true | ||
spy_logger)[0]).to be true | ||
|
||
# Audience Ids exist but Audience Conditions is Empty | ||
experiment = config.experiment_key_map['test_experiment'] | ||
experiment['audienceIds'] = ['11154'] | ||
experiment['audienceConditions'] = [] | ||
|
||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be true | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be true | ||
expect(reasons).to eq(["Evaluating audiences for experiment 'test_experiment': [].", "Audiences for experiment 'test_experiment' collectively evaluated to TRUE."]) | ||
|
||
# Audience Ids is Empty and Audience Conditions is nil | ||
experiment = config.experiment_key_map['test_experiment'] | ||
experiment['audienceIds'] = [] | ||
experiment['audienceConditions'] = nil | ||
|
||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be true | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be true | ||
expect(reasons).to eq(["Evaluating audiences for experiment 'test_experiment': [].", "Audiences for experiment 'test_experiment' collectively evaluated to TRUE."]) | ||
end | ||
|
||
it 'should pass conditions when audience conditions exist else audienceIds are passed' do | ||
|
@@ -85,15 +83,25 @@ | |
allow(Optimizely::CustomAttributeConditionEvaluator).to receive(:new).and_call_original | ||
|
||
# attributes set to empty dict | ||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
{}, | ||
spy_logger)).to be false | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, {}, spy_logger) | ||
expect(user_meets_audience_conditions).to be false | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': [\"11154\"].", | ||
"Starting to evaluate audience '11154' with conditions: [\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"firefox\"}]]].", | ||
"Audience '11154' evaluated to UNKNOWN.", | ||
"Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE." | ||
]) | ||
|
||
# attributes set to nil | ||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
nil, | ||
spy_logger)).to be false | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, nil, spy_logger) | ||
expect(user_meets_audience_conditions).to be false | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': [\"11154\"].", | ||
"Starting to evaluate audience '11154' with conditions: [\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"firefox\"}]]].", | ||
"Audience '11154' evaluated to UNKNOWN.", | ||
"Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE." | ||
]) | ||
|
||
# asserts nil attributes default to empty dict | ||
expect(Optimizely::CustomAttributeConditionEvaluator).to have_received(:new).with({}, spy_logger).twice | ||
end | ||
|
@@ -104,10 +112,12 @@ | |
'test_attribute' => 'test_value_1' | ||
} | ||
allow(Optimizely::ConditionTreeEvaluator).to receive(:evaluate).and_return(true) | ||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be true | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be true | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment': [].", | ||
"Audiences for experiment 'test_experiment' collectively evaluated to TRUE." | ||
]) | ||
end | ||
|
||
it 'should return false for user_meets_audience_conditions? when condition tree evaluator returns false or nil' do | ||
|
@@ -118,17 +128,22 @@ | |
|
||
# condition tree evaluator returns nil | ||
allow(Optimizely::ConditionTreeEvaluator).to receive(:evaluate).and_return(nil) | ||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be false | ||
|
||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be false | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': [\"11154\"].", | ||
"Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE." | ||
]) | ||
|
||
# condition tree evaluator returns false | ||
allow(Optimizely::ConditionTreeEvaluator).to receive(:evaluate).and_return(false) | ||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be false | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be false | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': [\"11154\"].", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This debugging message should be removed from reasons. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
"Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE." | ||
]) | ||
end | ||
|
||
it 'should correctly evaluate audience Ids and call custom attribute evaluator for leaf nodes' do | ||
|
@@ -213,10 +228,13 @@ | |
} | ||
experiment['audienceIds'] = %w[11110] | ||
|
||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be false | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be false | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': [\"11110\"].", | ||
"Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE." | ||
]) | ||
|
||
expect(spy_logger).to have_received(:log).once.with( | ||
Logger::DEBUG, | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': " + '["11110"].' | ||
|
@@ -236,10 +254,17 @@ | |
experiment['audienceIds'] = %w[11154 11155] | ||
experiment['audienceConditions'] = nil | ||
|
||
expect(Optimizely::Audience.user_meets_audience_conditions?(config, | ||
experiment, | ||
user_attributes, | ||
spy_logger)).to be false | ||
user_meets_audience_conditions, reasons = Optimizely::Audience.user_meets_audience_conditions?(config, experiment, user_attributes, spy_logger) | ||
expect(user_meets_audience_conditions).to be false | ||
expect(reasons).to eq([ | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': [\"11154\", \"11155\"].", | ||
"Starting to evaluate audience '11154' with conditions: [\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"firefox\"}]]].", | ||
"Audience '11154' evaluated to UNKNOWN.", | ||
"Starting to evaluate audience '11155' with conditions: [\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"chrome\"}]]].", | ||
"Audience '11155' evaluated to UNKNOWN.", | ||
"Audiences for experiment 'test_experiment_with_audience' collectively evaluated to FALSE." | ||
]) | ||
|
||
expect(spy_logger).to have_received(:log).once.with( | ||
Logger::DEBUG, | ||
"Evaluating audiences for experiment 'test_experiment_with_audience': " + '["11154", "11155"].' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message should not be in reasons - no helpful info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done