Skip to content
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
14 changes: 7 additions & 7 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = n
return
end

@decision_service = DecisionService.new(@config, @user_profile_service)
@event_builder = EventBuilder.new(@config, @logger)
@decision_service = DecisionService.new(@logger, @user_profile_service)
@event_builder = EventBuilder.new(@logger)
@notification_center = NotificationCenter.new(@logger, @error_handler)
end

Expand Down Expand Up @@ -145,7 +145,7 @@ def get_variation(experiment_key, user_id, attributes = nil)
return nil
end

variation_id = @decision_service.get_variation(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 @@ -219,7 +219,7 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
return nil
end

conversion_event = @event_builder.create_conversion_event(event, user_id, attributes, event_tags)
conversion_event = @event_builder.create_conversion_event(@config, event, user_id, attributes, event_tags)
@config.logger.log(Logger::INFO, "Tracking event '#{event_key}' for user '#{user_id}'.")
@logger.log(Logger::INFO,
"Dispatching conversion event to URL #{conversion_event.url} with params #{conversion_event.params}.")
Expand Down Expand Up @@ -268,7 +268,7 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
return false
end

decision = @decision_service.get_variation_for_feature(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 @@ -496,7 +496,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
return nil
else
source_string = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
decision = @decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
decision = @decision_service.get_variation_for_feature(@config, feature_flag, user_id, attributes)
variable_value = variable['defaultValue']
if decision
variation = decision['variation']
Expand Down Expand Up @@ -592,7 +592,7 @@ def validate_instantiation_options(datafile, skip_json_validation)
def send_impression(experiment, variation_key, user_id, attributes = nil)
experiment_key = experiment['key']
variation_id = @config.get_variation_id_from_key(experiment_key, variation_key)
impression_event = @event_builder.create_impression_event(experiment, variation_id, user_id, attributes)
impression_event = @event_builder.create_impression_event(@config, experiment, variation_id, user_id, attributes)
@logger.log(Logger::INFO,
"Dispatching impression event to URL #{impression_event.url} with params #{impression_event.params}.")
begin
Expand Down
33 changes: 16 additions & 17 deletions lib/optimizely/bucketer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2017, Optimizely and contributors
# Copyright 2016-2017, 2019 Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -28,18 +28,17 @@ class Bucketer
MAX_TRAFFIC_VALUE = 10_000
UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF

def initialize(config)
# Bucketer init method to set bucketing seed and project config data.
#
# config - ProjectConfig data to be used in making bucketing decisions.

def initialize(logger)
# Bucketer init method to set bucketing seed and logger.
# logger - Optional component which provides a log method to log messages.
@logger = logger
@bucket_seed = HASH_SEED
@config = config
end

def bucket(experiment, bucketing_id, user_id)
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
# experiment - Experiment for which visitor is to be bucketed.
# bucketing_id - String A customer-assigned value used to generate the bucketing key
# user_id - String ID for user.
Expand All @@ -52,27 +51,27 @@ def bucket(experiment, bucketing_id, user_id)
experiment_key = experiment['key']
group_id = experiment['groupId']
if group_id
group = @config.group_key_map.fetch(group_id)
group = project_config.group_key_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)
# return if the user is not bucketed into any experiment
unless bucketed_experiment_id
@config.logger.log(Logger::INFO, "User '#{user_id}' is in no experiment.")
@logger.log(Logger::INFO, "User '#{user_id}' is in no experiment.")
return nil
end

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

# continue bucketing if the user is bucketed into the experiment specified
@config.logger.log(
@logger.log(
Logger::INFO,
"User '#{user_id}' is in experiment '#{experiment_key}' of group #{group_id}."
)
Expand All @@ -82,9 +81,9 @@ def bucket(experiment, bucketing_id, user_id)
traffic_allocations = experiment['trafficAllocation']
variation_id = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations)
if variation_id && variation_id != ''
variation = @config.get_variation_from_id(experiment_key, variation_id)
variation = project_config.get_variation_from_id(experiment_key, variation_id)
variation_key = variation ? variation['key'] : nil
@config.logger.log(
@logger.log(
Logger::INFO,
"User '#{user_id}' is in variation '#{variation_key}' of experiment '#{experiment_key}'."
)
Expand All @@ -93,13 +92,13 @@ def bucket(experiment, bucketing_id, user_id)

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

@config.logger.log(Logger::INFO, "User '#{user_id}' is in no variation.")
@logger.log(Logger::INFO, "User '#{user_id}' is in no variation.")
nil
end

Expand All @@ -114,7 +113,7 @@ def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
# Returns entity ID corresponding to the provided bucket value or nil if no match is found.
bucketing_key = format(BUCKETING_ID_TEMPLATE, bucketing_id: bucketing_id, entity_id: parent_id)
bucket_value = generate_bucket_value(bucketing_key)
@config.logger.log(Logger::DEBUG, "Assigned bucket #{bucket_value} to user '#{user_id}' "\
@logger.log(Logger::DEBUG, "Assigned bucket #{bucket_value} to user '#{user_id}' "\
"with bucketing ID: '#{bucketing_id}'.")

traffic_allocations.each do |traffic_allocation|
Expand Down
Loading