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 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
13 changes: 7 additions & 6 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 @@ -405,7 +406,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 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 @@ -878,7 +879,7 @@ def get_variation_with_config(experiment_key, user_id, attributes, config)

return nil unless user_inputs_valid?(attributes)

variation_id = @decision_service.get_variation(config, experiment_key, user_id, attributes)
variation_id, = @decision_service.get_variation(config, experiment_key, user_id, attributes)
variation = config.get_variation_from_id(experiment_key, variation_id) unless variation_id.nil?
variation_key = variation['key'] if variation
decision_notification_type = if config.feature_experiment?(experiment['id'])
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
57 changes: 18 additions & 39 deletions lib/optimizely/audience.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,23 @@ 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)

# 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, message)
decide_reasons.push(message)
return true, decide_reasons
end

attributes ||= {}
Expand All @@ -80,39 +70,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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
41 changes: 24 additions & 17 deletions lib/optimizely/bucketer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(logger)
@bucket_seed = HASH_SEED
end

def bucket(project_config, experiment, bucketing_id, user_id, decide_reasons = nil)
def bucket(project_config, experiment, bucketing_id, user_id)
# Determines ID of variation to be shown for a given experiment key and user ID.
#
# project_config - Instance of ProjectConfig
Expand All @@ -44,7 +44,9 @@ def bucket(project_config, experiment, bucketing_id, user_id, decide_reasons = n
# user_id - String ID for user.
#
# Returns variation in which visitor with ID user_id has been placed. Nil if no variation.
return nil if experiment.nil?
return nil, [] if experiment.nil?

decide_reasons = []

# check if experiment is in a group; if so, check if user is bucketed into specified experiment
# this will not affect evaluation of rollout rules.
Expand All @@ -55,72 +57,77 @@ def bucket(project_config, experiment, bucketing_id, user_id, decide_reasons = n
group = project_config.group_id_map.fetch(group_id)
if Helpers::Group.random_policy?(group)
traffic_allocations = group.fetch('trafficAllocation')
bucketed_experiment_id = find_bucket(bucketing_id, user_id, group_id, traffic_allocations)
bucketed_experiment_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, group_id, traffic_allocations)
decide_reasons.push(*find_bucket_reasons)

# return if the user is not bucketed into any experiment
unless bucketed_experiment_id
message = "User '#{user_id}' is in no experiment."
@logger.log(Logger::INFO, message)
decide_reasons&.push(message)
return nil
decide_reasons.push(message)
return nil, decide_reasons
end

# return if the user is bucketed into a different experiment than the one specified
if bucketed_experiment_id != experiment_id
message = "User '#{user_id}' is not in experiment '#{experiment_key}' of group #{group_id}."
@logger.log(Logger::INFO, message)
decide_reasons&.push(message)
return nil
decide_reasons.push(message)
return nil, decide_reasons
end

# continue bucketing if the user is bucketed into the experiment specified
message = "User '#{user_id}' is in experiment '#{experiment_key}' of group #{group_id}."
@logger.log(Logger::INFO, message)
decide_reasons&.push(message)
decide_reasons.push(message)
end
end

traffic_allocations = experiment['trafficAllocation']
variation_id = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations, decide_reasons)
variation_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations)
decide_reasons.push(*find_bucket_reasons)

if variation_id && variation_id != ''
variation = project_config.get_variation_from_id(experiment_key, variation_id)
return variation
return variation, decide_reasons
end

# Handle the case when the traffic range is empty due to sticky bucketing
if variation_id == ''
message = 'Bucketed into an empty traffic range. Returning nil.'
@logger.log(Logger::DEBUG, message)
decide_reasons&.push(message)
decide_reasons.push(message)
end

nil
[nil, decide_reasons]
end

def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations, decide_reasons = nil)
def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
# Helper function to find the matching entity ID for a given bucketing value in a list of traffic allocations.
#
# bucketing_id - String A customer-assigned value user to generate bucketing key
# user_id - String ID for user
# parent_id - String entity ID to use for bucketing ID
# traffic_allocations - Array of traffic allocations
#
# Returns entity ID corresponding to the provided bucket value or nil if no match is found.
# Returns and array of two values where first value is the entity ID corresponding to the provided bucket value
# or nil if no match is found. The second value contains the array of reasons stating how the deicision was taken
decide_reasons = []
bucketing_key = format(BUCKETING_ID_TEMPLATE, bucketing_id: bucketing_id, entity_id: parent_id)
bucket_value = generate_bucket_value(bucketing_key)

message = "Assigned bucket #{bucket_value} to user '#{user_id}' with bucketing ID: '#{bucketing_id}'."
@logger.log(Logger::DEBUG, message)
decide_reasons&.push(message)

traffic_allocations.each do |traffic_allocation|
current_end_of_range = traffic_allocation['endOfRange']
if bucket_value < current_end_of_range
entity_id = traffic_allocation['entityId']
return entity_id
return entity_id, decide_reasons
end
end

nil
[nil, decide_reasons]
end

private
Expand Down
Loading