optimizely-sdk 5.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +202 -202
  3. data/lib/optimizely/audience.rb +127 -127
  4. data/lib/optimizely/bucketer.rb +156 -156
  5. data/lib/optimizely/condition_tree_evaluator.rb +123 -123
  6. data/lib/optimizely/config/datafile_project_config.rb +558 -558
  7. data/lib/optimizely/config/proxy_config.rb +34 -34
  8. data/lib/optimizely/config_manager/async_scheduler.rb +95 -95
  9. data/lib/optimizely/config_manager/http_project_config_manager.rb +340 -340
  10. data/lib/optimizely/config_manager/project_config_manager.rb +25 -25
  11. data/lib/optimizely/config_manager/static_project_config_manager.rb +55 -55
  12. data/lib/optimizely/decide/optimizely_decide_option.rb +28 -28
  13. data/lib/optimizely/decide/optimizely_decision.rb +60 -60
  14. data/lib/optimizely/decide/optimizely_decision_message.rb +26 -26
  15. data/lib/optimizely/decision_service.rb +563 -563
  16. data/lib/optimizely/error_handler.rb +39 -39
  17. data/lib/optimizely/event/batch_event_processor.rb +235 -235
  18. data/lib/optimizely/event/entity/conversion_event.rb +44 -44
  19. data/lib/optimizely/event/entity/decision.rb +38 -38
  20. data/lib/optimizely/event/entity/event_batch.rb +86 -86
  21. data/lib/optimizely/event/entity/event_context.rb +50 -50
  22. data/lib/optimizely/event/entity/impression_event.rb +48 -48
  23. data/lib/optimizely/event/entity/snapshot.rb +33 -33
  24. data/lib/optimizely/event/entity/snapshot_event.rb +48 -48
  25. data/lib/optimizely/event/entity/user_event.rb +22 -22
  26. data/lib/optimizely/event/entity/visitor.rb +36 -36
  27. data/lib/optimizely/event/entity/visitor_attribute.rb +38 -38
  28. data/lib/optimizely/event/event_factory.rb +156 -156
  29. data/lib/optimizely/event/event_processor.rb +25 -25
  30. data/lib/optimizely/event/forwarding_event_processor.rb +44 -44
  31. data/lib/optimizely/event/user_event_factory.rb +88 -88
  32. data/lib/optimizely/event_builder.rb +221 -221
  33. data/lib/optimizely/event_dispatcher.rb +69 -69
  34. data/lib/optimizely/exceptions.rb +193 -193
  35. data/lib/optimizely/helpers/constants.rb +459 -459
  36. data/lib/optimizely/helpers/date_time_utils.rb +30 -30
  37. data/lib/optimizely/helpers/event_tag_utils.rb +132 -132
  38. data/lib/optimizely/helpers/group.rb +31 -31
  39. data/lib/optimizely/helpers/http_utils.rb +68 -68
  40. data/lib/optimizely/helpers/sdk_settings.rb +61 -61
  41. data/lib/optimizely/helpers/validator.rb +236 -236
  42. data/lib/optimizely/helpers/variable_type.rb +67 -67
  43. data/lib/optimizely/logger.rb +46 -46
  44. data/lib/optimizely/notification_center.rb +174 -174
  45. data/lib/optimizely/notification_center_registry.rb +71 -71
  46. data/lib/optimizely/odp/lru_cache.rb +114 -114
  47. data/lib/optimizely/odp/odp_config.rb +102 -102
  48. data/lib/optimizely/odp/odp_event.rb +75 -75
  49. data/lib/optimizely/odp/odp_event_api_manager.rb +70 -70
  50. data/lib/optimizely/odp/odp_event_manager.rb +286 -286
  51. data/lib/optimizely/odp/odp_manager.rb +159 -159
  52. data/lib/optimizely/odp/odp_segment_api_manager.rb +122 -122
  53. data/lib/optimizely/odp/odp_segment_manager.rb +97 -97
  54. data/lib/optimizely/optimizely_config.rb +273 -273
  55. data/lib/optimizely/optimizely_factory.rb +184 -184
  56. data/lib/optimizely/optimizely_user_context.rb +238 -238
  57. data/lib/optimizely/params.rb +31 -31
  58. data/lib/optimizely/project_config.rb +99 -99
  59. data/lib/optimizely/semantic_version.rb +166 -166
  60. data/lib/optimizely/user_condition_evaluator.rb +391 -391
  61. data/lib/optimizely/user_profile_service.rb +35 -35
  62. data/lib/optimizely/version.rb +21 -21
  63. data/lib/optimizely.rb +1262 -1262
  64. metadata +7 -5
@@ -1,127 +1,127 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Copyright 2016-2017, 2019-2020, 2023, Optimizely and contributors
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- require 'json'
19
- require_relative './user_condition_evaluator'
20
- require_relative 'condition_tree_evaluator'
21
- require_relative 'helpers/constants'
22
-
23
- module Optimizely
24
- module Audience
25
- module_function
26
-
27
- def user_meets_audience_conditions?(config, experiment, user_context, logger, logging_hash = nil, logging_key = nil)
28
- # Determine for given experiment/rollout rule if user satisfies the audience conditions.
29
- #
30
- # config - Representation of the Optimizely project config.
31
- # experiment - Experiment/Rollout rule in which user is to be bucketed.
32
- # user_context - Optimizely user context instance
33
- # logger - Provides a logger instance.
34
- # logging_hash - Optional string representing logs hash inside Helpers::Constants.
35
- # This defaults to 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'.
36
- # logging_key - Optional string to be logged as an identifier of experiment under evaluation.
37
- # This defaults to experiment['key'].
38
- #
39
- # Returns boolean representing if user satisfies audience conditions for the audiences or not.
40
- decide_reasons = []
41
- logging_hash ||= 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'
42
- logging_key ||= experiment['key']
43
-
44
- logs_hash = Object.const_get "Optimizely::Helpers::Constants::#{logging_hash}"
45
-
46
- audience_conditions = experiment['audienceConditions'] || experiment['audienceIds']
47
-
48
- message = format(logs_hash['EVALUATING_AUDIENCES_COMBINED'], logging_key, audience_conditions)
49
- logger.log(Logger::DEBUG, message)
50
-
51
- # Return true if there are no audiences
52
- if audience_conditions.empty?
53
- message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, 'TRUE')
54
- logger.log(Logger::INFO, message)
55
- decide_reasons.push(message)
56
- return true, decide_reasons
57
- end
58
-
59
- user_condition_evaluator = UserConditionEvaluator.new(user_context, logger)
60
-
61
- evaluate_user_conditions = lambda do |condition|
62
- user_condition_evaluator.evaluate(condition)
63
- end
64
-
65
- evaluate_audience = lambda do |audience_id|
66
- audience = config.get_audience_from_id(audience_id)
67
- return nil unless audience
68
-
69
- audience_conditions = audience['conditions']
70
- message = format(logs_hash['EVALUATING_AUDIENCE'], audience_id, audience_conditions)
71
- logger.log(Logger::DEBUG, message)
72
- decide_reasons.push(message)
73
-
74
- audience_conditions = JSON.parse(audience_conditions) if audience_conditions.is_a?(String)
75
- result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_user_conditions)
76
- result_str = result.nil? ? 'UNKNOWN' : result.to_s.upcase
77
- message = format(logs_hash['AUDIENCE_EVALUATION_RESULT'], audience_id, result_str)
78
- logger.log(Logger::DEBUG, message)
79
- decide_reasons.push(message)
80
-
81
- result
82
- end
83
-
84
- eval_result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_audience)
85
- eval_result ||= false
86
-
87
- message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, eval_result.to_s.upcase)
88
- logger.log(Logger::INFO, message)
89
- decide_reasons.push(message)
90
-
91
- [eval_result, decide_reasons]
92
- end
93
-
94
- def get_segments(conditions)
95
- # Return any audience segments from provided conditions.
96
- #
97
- # conditions - Nested array of and/or conditions.
98
- # Example: ['and', operand_1, ['or', operand_2, operand_3]]
99
- #
100
- # Returns unique array of segment names.
101
- conditions = JSON.parse(conditions) if conditions.is_a?(String)
102
- @parse_segments.call(conditions).uniq
103
- end
104
-
105
- @parse_segments = lambda { |conditions|
106
- # Return any audience segments from provided conditions.
107
- # Helper function for get_segments.
108
- #
109
- # conditions - Nested array of and/or conditions.
110
- # Example: ['and', operand_1, ['or', operand_2, operand_3]]
111
- #
112
- # Returns array of segment names.
113
- segments = []
114
-
115
- case conditions
116
- when Hash
117
- segments.push(conditions['value']) if conditions.fetch('match', nil) == 'qualified'
118
- when Array
119
- conditions.each do |condition|
120
- segments.concat @parse_segments.call(condition)
121
- end
122
- end
123
-
124
- segments
125
- }
126
- end
127
- end
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2016-2017, 2019-2020, 2023, Optimizely and contributors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'json'
19
+ require_relative './user_condition_evaluator'
20
+ require_relative 'condition_tree_evaluator'
21
+ require_relative 'helpers/constants'
22
+
23
+ module Optimizely
24
+ module Audience
25
+ module_function
26
+
27
+ def user_meets_audience_conditions?(config, experiment, user_context, logger, logging_hash = nil, logging_key = nil)
28
+ # Determine for given experiment/rollout rule if user satisfies the audience conditions.
29
+ #
30
+ # config - Representation of the Optimizely project config.
31
+ # experiment - Experiment/Rollout rule in which user is to be bucketed.
32
+ # user_context - Optimizely user context instance
33
+ # logger - Provides a logger instance.
34
+ # logging_hash - Optional string representing logs hash inside Helpers::Constants.
35
+ # This defaults to 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'.
36
+ # logging_key - Optional string to be logged as an identifier of experiment under evaluation.
37
+ # This defaults to experiment['key'].
38
+ #
39
+ # Returns boolean representing if user satisfies audience conditions for the audiences or not.
40
+ decide_reasons = []
41
+ logging_hash ||= 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'
42
+ logging_key ||= experiment['key']
43
+
44
+ logs_hash = Object.const_get "Optimizely::Helpers::Constants::#{logging_hash}"
45
+
46
+ audience_conditions = experiment['audienceConditions'] || experiment['audienceIds']
47
+
48
+ message = format(logs_hash['EVALUATING_AUDIENCES_COMBINED'], logging_key, audience_conditions)
49
+ logger.log(Logger::DEBUG, message)
50
+
51
+ # Return true if there are no audiences
52
+ if audience_conditions.empty?
53
+ message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, 'TRUE')
54
+ logger.log(Logger::INFO, message)
55
+ decide_reasons.push(message)
56
+ return true, decide_reasons
57
+ end
58
+
59
+ user_condition_evaluator = UserConditionEvaluator.new(user_context, logger)
60
+
61
+ evaluate_user_conditions = lambda do |condition|
62
+ user_condition_evaluator.evaluate(condition)
63
+ end
64
+
65
+ evaluate_audience = lambda do |audience_id|
66
+ audience = config.get_audience_from_id(audience_id)
67
+ return nil unless audience
68
+
69
+ audience_conditions = audience['conditions']
70
+ message = format(logs_hash['EVALUATING_AUDIENCE'], audience_id, audience_conditions)
71
+ logger.log(Logger::DEBUG, message)
72
+ decide_reasons.push(message)
73
+
74
+ audience_conditions = JSON.parse(audience_conditions) if audience_conditions.is_a?(String)
75
+ result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_user_conditions)
76
+ result_str = result.nil? ? 'UNKNOWN' : result.to_s.upcase
77
+ message = format(logs_hash['AUDIENCE_EVALUATION_RESULT'], audience_id, result_str)
78
+ logger.log(Logger::DEBUG, message)
79
+ decide_reasons.push(message)
80
+
81
+ result
82
+ end
83
+
84
+ eval_result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_audience)
85
+ eval_result ||= false
86
+
87
+ message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, eval_result.to_s.upcase)
88
+ logger.log(Logger::INFO, message)
89
+ decide_reasons.push(message)
90
+
91
+ [eval_result, decide_reasons]
92
+ end
93
+
94
+ def get_segments(conditions)
95
+ # Return any audience segments from provided conditions.
96
+ #
97
+ # conditions - Nested array of and/or conditions.
98
+ # Example: ['and', operand_1, ['or', operand_2, operand_3]]
99
+ #
100
+ # Returns unique array of segment names.
101
+ conditions = JSON.parse(conditions) if conditions.is_a?(String)
102
+ @parse_segments.call(conditions).uniq
103
+ end
104
+
105
+ @parse_segments = lambda { |conditions|
106
+ # Return any audience segments from provided conditions.
107
+ # Helper function for get_segments.
108
+ #
109
+ # conditions - Nested array of and/or conditions.
110
+ # Example: ['and', operand_1, ['or', operand_2, operand_3]]
111
+ #
112
+ # Returns array of segment names.
113
+ segments = []
114
+
115
+ case conditions
116
+ when Hash
117
+ segments.push(conditions['value']) if conditions.fetch('match', nil) == 'qualified'
118
+ when Array
119
+ conditions.each do |condition|
120
+ segments.concat @parse_segments.call(condition)
121
+ end
122
+ end
123
+
124
+ segments
125
+ }
126
+ end
127
+ end
@@ -1,156 +1,156 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Copyright 2016-2017, 2019-2021 Optimizely and contributors
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- require 'murmurhash3'
19
- require_relative 'helpers/group'
20
-
21
- module Optimizely
22
- class Bucketer
23
- # Optimizely bucketing algorithm that evenly distributes visitors.
24
-
25
- BUCKETING_ID_TEMPLATE = '%<bucketing_id>s%<entity_id>s'
26
- HASH_SEED = 1
27
- MAX_HASH_VALUE = 2**32
28
- MAX_TRAFFIC_VALUE = 10_000
29
- UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF
30
-
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
35
- @bucket_seed = HASH_SEED
36
- end
37
-
38
- def bucket(project_config, experiment, bucketing_id, user_id)
39
- # Determines ID of variation to be shown for a given experiment key and user ID.
40
- #
41
- # project_config - Instance of ProjectConfig
42
- # experiment - Experiment or Rollout rule for which visitor is to be bucketed.
43
- # bucketing_id - String A customer-assigned value used to generate the bucketing key
44
- # user_id - String ID for user.
45
- #
46
- # Returns variation in which visitor with ID user_id has been placed. Nil if no variation.
47
- return nil, [] if experiment.nil?
48
-
49
- decide_reasons = []
50
-
51
- # check if experiment is in a group; if so, check if user is bucketed into specified experiment
52
- # this will not affect evaluation of rollout rules.
53
- experiment_id = experiment['id']
54
- experiment_key = experiment['key']
55
- group_id = experiment['groupId']
56
- if group_id
57
- group = project_config.group_id_map.fetch(group_id)
58
- if Helpers::Group.random_policy?(group)
59
- traffic_allocations = group.fetch('trafficAllocation')
60
- bucketed_experiment_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, group_id, traffic_allocations)
61
- decide_reasons.push(*find_bucket_reasons)
62
-
63
- # return if the user is not bucketed into any experiment
64
- unless bucketed_experiment_id
65
- message = "User '#{user_id}' is in no experiment."
66
- @logger.log(Logger::INFO, message)
67
- decide_reasons.push(message)
68
- return nil, decide_reasons
69
- end
70
-
71
- # return if the user is bucketed into a different experiment than the one specified
72
- if bucketed_experiment_id != experiment_id
73
- message = "User '#{user_id}' is not in experiment '#{experiment_key}' of group #{group_id}."
74
- @logger.log(Logger::INFO, message)
75
- decide_reasons.push(message)
76
- return nil, decide_reasons
77
- end
78
-
79
- # continue bucketing if the user is bucketed into the experiment specified
80
- message = "User '#{user_id}' is in experiment '#{experiment_key}' of group #{group_id}."
81
- @logger.log(Logger::INFO, message)
82
- decide_reasons.push(message)
83
- end
84
- end
85
-
86
- traffic_allocations = experiment['trafficAllocation']
87
- variation_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations)
88
- decide_reasons.push(*find_bucket_reasons)
89
-
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]
103
- end
104
-
105
- def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
106
- # Helper function to find the matching entity ID for a given bucketing value in a list of traffic allocations.
107
- #
108
- # bucketing_id - String A customer-assigned value user to generate bucketing key
109
- # user_id - String ID for user
110
- # parent_id - String entity ID to use for bucketing ID
111
- # traffic_allocations - Array of traffic allocations
112
- #
113
- # Returns an array of two values where first value is the entity ID corresponding to the provided bucket value
114
- # or nil if no match is found. The second value contains the array of reasons stating how the decision was taken
115
- decide_reasons = []
116
- bucketing_key = format(BUCKETING_ID_TEMPLATE, bucketing_id: bucketing_id, entity_id: parent_id)
117
- bucket_value = generate_bucket_value(bucketing_key)
118
-
119
- message = "Assigned bucket #{bucket_value} to user '#{user_id}' with bucketing ID: '#{bucketing_id}'."
120
- @logger.log(Logger::DEBUG, message)
121
-
122
- traffic_allocations.each do |traffic_allocation|
123
- current_end_of_range = traffic_allocation['endOfRange']
124
- if bucket_value < current_end_of_range
125
- entity_id = traffic_allocation['entityId']
126
- return entity_id, decide_reasons
127
- end
128
- end
129
-
130
- [nil, decide_reasons]
131
- end
132
-
133
- private
134
-
135
- def generate_bucket_value(bucketing_key)
136
- # Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE).
137
- #
138
- # bucketing_key - String - Value used to generate bucket value
139
- #
140
- # Returns bucket value corresponding to the provided bucketing key.
141
-
142
- ratio = generate_unsigned_hash_code_32_bit(bucketing_key).to_f / MAX_HASH_VALUE
143
- (ratio * MAX_TRAFFIC_VALUE).to_i
144
- end
145
-
146
- def generate_unsigned_hash_code_32_bit(bucketing_key)
147
- # Helper function to retreive hash code
148
- #
149
- # bucketing_key - String - Value used for the key of the murmur hash
150
- #
151
- # Returns hash code which is a 32 bit unsigned integer.
152
-
153
- MurmurHash3::V32.str_hash(bucketing_key, @bucket_seed) & UNSIGNED_MAX_32_BIT_VALUE
154
- end
155
- end
156
- end
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2016-2017, 2019-2021 Optimizely and contributors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'murmurhash3'
19
+ require_relative 'helpers/group'
20
+
21
+ module Optimizely
22
+ class Bucketer
23
+ # Optimizely bucketing algorithm that evenly distributes visitors.
24
+
25
+ BUCKETING_ID_TEMPLATE = '%<bucketing_id>s%<entity_id>s'
26
+ HASH_SEED = 1
27
+ MAX_HASH_VALUE = 2**32
28
+ MAX_TRAFFIC_VALUE = 10_000
29
+ UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF
30
+
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
35
+ @bucket_seed = HASH_SEED
36
+ end
37
+
38
+ def bucket(project_config, experiment, bucketing_id, user_id)
39
+ # Determines ID of variation to be shown for a given experiment key and user ID.
40
+ #
41
+ # project_config - Instance of ProjectConfig
42
+ # experiment - Experiment or Rollout rule for which visitor is to be bucketed.
43
+ # bucketing_id - String A customer-assigned value used to generate the bucketing key
44
+ # user_id - String ID for user.
45
+ #
46
+ # Returns variation in which visitor with ID user_id has been placed. Nil if no variation.
47
+ return nil, [] if experiment.nil?
48
+
49
+ decide_reasons = []
50
+
51
+ # check if experiment is in a group; if so, check if user is bucketed into specified experiment
52
+ # this will not affect evaluation of rollout rules.
53
+ experiment_id = experiment['id']
54
+ experiment_key = experiment['key']
55
+ group_id = experiment['groupId']
56
+ if group_id
57
+ group = project_config.group_id_map.fetch(group_id)
58
+ if Helpers::Group.random_policy?(group)
59
+ traffic_allocations = group.fetch('trafficAllocation')
60
+ bucketed_experiment_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, group_id, traffic_allocations)
61
+ decide_reasons.push(*find_bucket_reasons)
62
+
63
+ # return if the user is not bucketed into any experiment
64
+ unless bucketed_experiment_id
65
+ message = "User '#{user_id}' is in no experiment."
66
+ @logger.log(Logger::INFO, message)
67
+ decide_reasons.push(message)
68
+ return nil, decide_reasons
69
+ end
70
+
71
+ # return if the user is bucketed into a different experiment than the one specified
72
+ if bucketed_experiment_id != experiment_id
73
+ message = "User '#{user_id}' is not in experiment '#{experiment_key}' of group #{group_id}."
74
+ @logger.log(Logger::INFO, message)
75
+ decide_reasons.push(message)
76
+ return nil, decide_reasons
77
+ end
78
+
79
+ # continue bucketing if the user is bucketed into the experiment specified
80
+ message = "User '#{user_id}' is in experiment '#{experiment_key}' of group #{group_id}."
81
+ @logger.log(Logger::INFO, message)
82
+ decide_reasons.push(message)
83
+ end
84
+ end
85
+
86
+ traffic_allocations = experiment['trafficAllocation']
87
+ variation_id, find_bucket_reasons = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations)
88
+ decide_reasons.push(*find_bucket_reasons)
89
+
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]
103
+ end
104
+
105
+ def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
106
+ # Helper function to find the matching entity ID for a given bucketing value in a list of traffic allocations.
107
+ #
108
+ # bucketing_id - String A customer-assigned value user to generate bucketing key
109
+ # user_id - String ID for user
110
+ # parent_id - String entity ID to use for bucketing ID
111
+ # traffic_allocations - Array of traffic allocations
112
+ #
113
+ # Returns an array of two values where first value is the entity ID corresponding to the provided bucket value
114
+ # or nil if no match is found. The second value contains the array of reasons stating how the decision was taken
115
+ decide_reasons = []
116
+ bucketing_key = format(BUCKETING_ID_TEMPLATE, bucketing_id: bucketing_id, entity_id: parent_id)
117
+ bucket_value = generate_bucket_value(bucketing_key)
118
+
119
+ message = "Assigned bucket #{bucket_value} to user '#{user_id}' with bucketing ID: '#{bucketing_id}'."
120
+ @logger.log(Logger::DEBUG, message)
121
+
122
+ traffic_allocations.each do |traffic_allocation|
123
+ current_end_of_range = traffic_allocation['endOfRange']
124
+ if bucket_value < current_end_of_range
125
+ entity_id = traffic_allocation['entityId']
126
+ return entity_id, decide_reasons
127
+ end
128
+ end
129
+
130
+ [nil, decide_reasons]
131
+ end
132
+
133
+ private
134
+
135
+ def generate_bucket_value(bucketing_key)
136
+ # Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE).
137
+ #
138
+ # bucketing_key - String - Value used to generate bucket value
139
+ #
140
+ # Returns bucket value corresponding to the provided bucketing key.
141
+
142
+ ratio = generate_unsigned_hash_code_32_bit(bucketing_key).to_f / MAX_HASH_VALUE
143
+ (ratio * MAX_TRAFFIC_VALUE).to_i
144
+ end
145
+
146
+ def generate_unsigned_hash_code_32_bit(bucketing_key)
147
+ # Helper function to retreive hash code
148
+ #
149
+ # bucketing_key - String - Value used for the key of the murmur hash
150
+ #
151
+ # Returns hash code which is a 32 bit unsigned integer.
152
+
153
+ MurmurHash3::V32.str_hash(bucketing_key, @bucket_seed) & UNSIGNED_MAX_32_BIT_VALUE
154
+ end
155
+ end
156
+ end