Skip to content

Commit 761bc43

Browse files
update: Refactor bucketing logic to handle empty traffic ranges and improve logging
1 parent 8ca3aee commit 761bc43

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/optimizely/bucketer.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ def bucket(project_config, experiment, bucketing_id, user_id)
4444
# user_id - String ID for user.
4545
#
4646
# Returns variation in which visitor with ID user_id has been placed. Nil if no variation.
47+
48+
variation_id, decide_reasons = bucket_to_entity_id(project_config, experiment, bucketing_id, user_id)
49+
if variation_id && variation_id != ''
50+
experiment_id = experiment['id']
51+
variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)
52+
return variation, decide_reasons
53+
end
54+
55+
# Handle the case when the traffic range is empty due to sticky bucketing
56+
if variation_id == ''
57+
message = 'Bucketed into an empty traffic range. Returning nil.'
58+
@logger.log(Logger::DEBUG, message)
59+
decide_reasons.push(message)
60+
end
61+
62+
[nil, decide_reasons]
63+
end
64+
65+
def bucket_to_entity_id(project_config, experiment, bucketing_id, user_id)
4766
return nil, [] if experiment.nil?
4867

4968
decide_reasons = []
@@ -87,19 +106,7 @@ def bucket(project_config, experiment, bucketing_id, user_id)
87106
variation_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations)
88107
decide_reasons.push(*find_bucket_reasons)
89108

90-
if variation_id && variation_id != ''
91-
variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)
92-
return variation, decide_reasons
93-
end
94-
95-
# Handle the case when the traffic range is empty due to sticky bucketing
96-
if variation_id == ''
97-
message = 'Bucketed into an empty traffic range. Returning nil.'
98-
@logger.log(Logger::DEBUG, message)
99-
decide_reasons.push(message)
100-
end
101-
102-
[nil, decide_reasons]
109+
[variation_id, decide_reasons]
103110
end
104111

105112
def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)

0 commit comments

Comments
 (0)