1
1
# frozen_string_literal: true
2
2
3
3
#
4
- # Copyright 2016-2017, Optimizely and contributors
4
+ # Copyright 2016-2017, 2019 Optimizely and contributors
5
5
#
6
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
7
# you may not use this file except in compliance with the License.
@@ -28,18 +28,17 @@ class Bucketer
28
28
MAX_TRAFFIC_VALUE = 10_000
29
29
UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF
30
30
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
36
35
@bucket_seed = HASH_SEED
37
- @config = config
38
36
end
39
37
40
- def bucket ( experiment , bucketing_id , user_id )
38
+ def bucket ( project_config , experiment , bucketing_id , user_id )
41
39
# Determines ID of variation to be shown for a given experiment key and user ID.
42
40
#
41
+ # project_config - Instance of ProjectConfig
43
42
# experiment - Experiment for which visitor is to be bucketed.
44
43
# bucketing_id - String A customer-assigned value used to generate the bucketing key
45
44
# user_id - String ID for user.
@@ -52,27 +51,27 @@ def bucket(experiment, bucketing_id, user_id)
52
51
experiment_key = experiment [ 'key' ]
53
52
group_id = experiment [ 'groupId' ]
54
53
if group_id
55
- group = @config . group_key_map . fetch ( group_id )
54
+ group = project_config . group_key_map . fetch ( group_id )
56
55
if Helpers ::Group . random_policy? ( group )
57
56
traffic_allocations = group . fetch ( 'trafficAllocation' )
58
57
bucketed_experiment_id = find_bucket ( bucketing_id , user_id , group_id , traffic_allocations )
59
58
# return if the user is not bucketed into any experiment
60
59
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." )
62
61
return nil
63
62
end
64
63
65
64
# return if the user is bucketed into a different experiment than the one specified
66
65
if bucketed_experiment_id != experiment_id
67
- @config . logger . log (
66
+ @logger . log (
68
67
Logger ::INFO ,
69
68
"User '#{ user_id } ' is not in experiment '#{ experiment_key } ' of group #{ group_id } ."
70
69
)
71
70
return nil
72
71
end
73
72
74
73
# continue bucketing if the user is bucketed into the experiment specified
75
- @config . logger . log (
74
+ @logger . log (
76
75
Logger ::INFO ,
77
76
"User '#{ user_id } ' is in experiment '#{ experiment_key } ' of group #{ group_id } ."
78
77
)
@@ -82,9 +81,9 @@ def bucket(experiment, bucketing_id, user_id)
82
81
traffic_allocations = experiment [ 'trafficAllocation' ]
83
82
variation_id = find_bucket ( bucketing_id , user_id , experiment_id , traffic_allocations )
84
83
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 )
86
85
variation_key = variation ? variation [ 'key' ] : nil
87
- @config . logger . log (
86
+ @logger . log (
88
87
Logger ::INFO ,
89
88
"User '#{ user_id } ' is in variation '#{ variation_key } ' of experiment '#{ experiment_key } '."
90
89
)
@@ -93,13 +92,13 @@ def bucket(experiment, bucketing_id, user_id)
93
92
94
93
# Handle the case when the traffic range is empty due to sticky bucketing
95
94
if variation_id == ''
96
- @config . logger . log (
95
+ @logger . log (
97
96
Logger ::DEBUG ,
98
97
'Bucketed into an empty traffic range. Returning nil.'
99
98
)
100
99
end
101
100
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." )
103
102
nil
104
103
end
105
104
@@ -114,7 +113,7 @@ def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
114
113
# Returns entity ID corresponding to the provided bucket value or nil if no match is found.
115
114
bucketing_key = format ( BUCKETING_ID_TEMPLATE , bucketing_id : bucketing_id , entity_id : parent_id )
116
115
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 } ' " \
118
117
"with bucketing ID: '#{ bucketing_id } '." )
119
118
120
119
traffic_allocations . each do |traffic_allocation |
0 commit comments