11# frozen_string_literal: true
22
33#
4- # Copyright 2016-2017, Optimizely and contributors
4+ # Copyright 2016-2017, 2019 Optimizely and contributors
55#
66# Licensed under the Apache License, Version 2.0 (the "License");
77# you may not use this file except in compliance with the License.
@@ -28,18 +28,17 @@ class Bucketer
2828 MAX_TRAFFIC_VALUE = 10_000
2929 UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF
3030
31- def initialize ( config )
32- # Bucketer init method to set bucketing seed and project config data.
33- #
34- # config - ProjectConfig data to be used in making bucketing decisions.
35-
31+ def initialize ( logger )
32+ # Bucketer init method to set bucketing seed and logger.
33+ # logger - Optional component which provides a log method to log messages.
34+ @logger = logger
3635 @bucket_seed = HASH_SEED
37- @config = config
3836 end
3937
40- def bucket ( experiment , bucketing_id , user_id )
38+ def bucket ( project_config , experiment , bucketing_id , user_id )
4139 # Determines ID of variation to be shown for a given experiment key and user ID.
4240 #
41+ # project_config - Instance of ProjectConfig
4342 # experiment - Experiment for which visitor is to be bucketed.
4443 # bucketing_id - String A customer-assigned value used to generate the bucketing key
4544 # user_id - String ID for user.
@@ -52,27 +51,27 @@ def bucket(experiment, bucketing_id, user_id)
5251 experiment_key = experiment [ 'key' ]
5352 group_id = experiment [ 'groupId' ]
5453 if group_id
55- group = @config . group_key_map . fetch ( group_id )
54+ group = project_config . group_key_map . fetch ( group_id )
5655 if Helpers ::Group . random_policy? ( group )
5756 traffic_allocations = group . fetch ( 'trafficAllocation' )
5857 bucketed_experiment_id = find_bucket ( bucketing_id , user_id , group_id , traffic_allocations )
5958 # return if the user is not bucketed into any experiment
6059 unless bucketed_experiment_id
61- @config . logger . log ( Logger ::INFO , "User '#{ user_id } ' is in no experiment." )
60+ @logger . log ( Logger ::INFO , "User '#{ user_id } ' is in no experiment." )
6261 return nil
6362 end
6463
6564 # return if the user is bucketed into a different experiment than the one specified
6665 if bucketed_experiment_id != experiment_id
67- @config . logger . log (
66+ @logger . log (
6867 Logger ::INFO ,
6968 "User '#{ user_id } ' is not in experiment '#{ experiment_key } ' of group #{ group_id } ."
7069 )
7170 return nil
7271 end
7372
7473 # continue bucketing if the user is bucketed into the experiment specified
75- @config . logger . log (
74+ @logger . log (
7675 Logger ::INFO ,
7776 "User '#{ user_id } ' is in experiment '#{ experiment_key } ' of group #{ group_id } ."
7877 )
@@ -82,9 +81,9 @@ def bucket(experiment, bucketing_id, user_id)
8281 traffic_allocations = experiment [ 'trafficAllocation' ]
8382 variation_id = find_bucket ( bucketing_id , user_id , experiment_id , traffic_allocations )
8483 if variation_id && variation_id != ''
85- variation = @config . get_variation_from_id ( experiment_key , variation_id )
84+ variation = project_config . get_variation_from_id ( experiment_key , variation_id )
8685 variation_key = variation ? variation [ 'key' ] : nil
87- @config . logger . log (
86+ @logger . log (
8887 Logger ::INFO ,
8988 "User '#{ user_id } ' is in variation '#{ variation_key } ' of experiment '#{ experiment_key } '."
9089 )
@@ -93,13 +92,13 @@ def bucket(experiment, bucketing_id, user_id)
9392
9493 # Handle the case when the traffic range is empty due to sticky bucketing
9594 if variation_id == ''
96- @config . logger . log (
95+ @logger . log (
9796 Logger ::DEBUG ,
9897 'Bucketed into an empty traffic range. Returning nil.'
9998 )
10099 end
101100
102- @config . logger . log ( Logger ::INFO , "User '#{ user_id } ' is in no variation." )
101+ @logger . log ( Logger ::INFO , "User '#{ user_id } ' is in no variation." )
103102 nil
104103 end
105104
@@ -114,7 +113,7 @@ def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
114113 # Returns entity ID corresponding to the provided bucket value or nil if no match is found.
115114 bucketing_key = format ( BUCKETING_ID_TEMPLATE , bucketing_id : bucketing_id , entity_id : parent_id )
116115 bucket_value = generate_bucket_value ( bucketing_key )
117- @config . logger . log ( Logger ::DEBUG , "Assigned bucket #{ bucket_value } to user '#{ user_id } ' " \
116+ @logger . log ( Logger ::DEBUG , "Assigned bucket #{ bucket_value } to user '#{ user_id } ' " \
118117 "with bucketing ID: '#{ bucketing_id } '." )
119118
120119 traffic_allocations . each do |traffic_allocation |
0 commit comments