|
| 1 | +import math |
| 2 | +try: |
| 3 | + import mmh3 |
| 4 | +except ImportError: |
| 5 | + from .lib import pymmh3 as mmh3 |
| 6 | + |
| 7 | +from .helpers import enums |
| 8 | +from . import exceptions |
| 9 | + |
| 10 | +MAX_TRAFFIC_VALUE = 10000 |
| 11 | +UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF |
| 12 | +MAX_HASH_VALUE = math.pow(2, 32) |
| 13 | +HASH_SEED = 1 |
| 14 | +BUCKETING_ID_TEMPLATE = '{user_id}{parent_id}' |
| 15 | +GROUP_POLICIES = ['random'] |
| 16 | + |
| 17 | + |
| 18 | +class Bucketer(object): |
| 19 | + """ Optimizely bucketing algorithm that evenly distributes visitors. """ |
| 20 | + |
| 21 | + def __init__(self, project_config): |
| 22 | + """ Bucketer init method to set bucketing seed and project config data. |
| 23 | +
|
| 24 | + Args: |
| 25 | + project_config: Project config data to be used in making bucketing decisions. |
| 26 | + """ |
| 27 | + |
| 28 | + self.bucket_seed = HASH_SEED |
| 29 | + self.config = project_config |
| 30 | + |
| 31 | + def _generate_unsigned_hash_code_32_bit(self, bucketing_id): |
| 32 | + """ Helper method to retrieve hash code. |
| 33 | +
|
| 34 | + Args: |
| 35 | + bucketing_id: ID for bucketing. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + Hash code which is a 32 bit unsigned integer. |
| 39 | + """ |
| 40 | + |
| 41 | + # Adjusting MurmurHash code to be unsigned |
| 42 | + return (mmh3.hash(bucketing_id, self.bucket_seed) & UNSIGNED_MAX_32_BIT_VALUE) |
| 43 | + |
| 44 | + def _generate_bucket_value(self, bucketing_id): |
| 45 | + """ Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE). |
| 46 | +
|
| 47 | + Args: |
| 48 | + bucketing_id: ID for bucketing. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Bucket value corresponding to the provided bucketing ID. |
| 52 | + """ |
| 53 | + |
| 54 | + ratio = float(self._generate_unsigned_hash_code_32_bit(bucketing_id)) / MAX_HASH_VALUE |
| 55 | + return math.floor(ratio * MAX_TRAFFIC_VALUE) |
| 56 | + |
| 57 | + def _find_bucket(self, user_id, parent_id, traffic_allocations): |
| 58 | + """ Determine entity based on bucket value and traffic allocations. |
| 59 | +
|
| 60 | + Args: |
| 61 | + user_id: ID for user. |
| 62 | + parent_id: ID representing group or experiment. |
| 63 | + traffic_allocations: Traffic allocations representing traffic allotted to experiments or variations. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Entity ID which may represent experiment or group. |
| 67 | + """ |
| 68 | + |
| 69 | + bucketing_id = BUCKETING_ID_TEMPLATE.format(user_id=user_id, parent_id=parent_id) |
| 70 | + bucketing_number = self._generate_bucket_value(bucketing_id) |
| 71 | + self.config.logger.log(enums.LogLevels.DEBUG, 'Assigned bucket %s to user "%s".' % (bucketing_number, user_id)) |
| 72 | + |
| 73 | + for traffic_allocation in traffic_allocations: |
| 74 | + current_end_of_range = traffic_allocation.get('endOfRange') |
| 75 | + if bucketing_number < current_end_of_range: |
| 76 | + return traffic_allocation.get('entityId') |
| 77 | + |
| 78 | + return None |
| 79 | + |
| 80 | + def bucket(self, experiment_key, user_id): |
| 81 | + """ For a given experiment key and bucketing ID determines ID of variation to be shown to visitor. |
| 82 | +
|
| 83 | + Args: |
| 84 | + experiment_key: Key representing experiment for which visitor is to be bucketed. |
| 85 | + user_id: ID for user. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + Variation ID for variation in which the visitor with ID user_id will be put in. None if no variation. |
| 89 | + """ |
| 90 | + |
| 91 | + # Check if user is white-listed for a variation |
| 92 | + forced_variations = self.config.get_experiment_forced_variations(experiment_key) |
| 93 | + if forced_variations and user_id in forced_variations: |
| 94 | + variation_key = forced_variations.get(user_id) |
| 95 | + variation_id = self.config.get_variation_id(experiment_key, variation_key) |
| 96 | + if variation_id: |
| 97 | + self.config.logger.log(enums.LogLevels.INFO, |
| 98 | + 'User "%s" is forced in variation "%s".' % (user_id, variation_key)) |
| 99 | + return variation_id |
| 100 | + |
| 101 | + # Determine experiment ID |
| 102 | + experiment_id = self.config.get_experiment_id(experiment_key) |
| 103 | + if not experiment_id: |
| 104 | + return None |
| 105 | + |
| 106 | + # Determine if experiment is in a mutually exclusive group |
| 107 | + group_policy = self.config.get_experiment_group_policy(experiment_key) |
| 108 | + if group_policy in GROUP_POLICIES: |
| 109 | + group_id = self.config.get_experiment_group_id(experiment_key) |
| 110 | + |
| 111 | + if not group_id: |
| 112 | + return None |
| 113 | + |
| 114 | + group_traffic_allocations = self.config.get_traffic_allocation(self.config.group_id_map, group_id) |
| 115 | + |
| 116 | + if not group_traffic_allocations: |
| 117 | + self.config.logger.log(enums.LogLevels.ERROR, 'Group ID "%s" is not in datafile.' % group_id) |
| 118 | + self.config.error_handler.handle_error( |
| 119 | + exceptions.InvalidExperimentException(enums.Errors.INVALID_GROUP_ID_ERROR) |
| 120 | + ) |
| 121 | + return None |
| 122 | + |
| 123 | + user_experiment_id = self._find_bucket(user_id, group_id, group_traffic_allocations) |
| 124 | + if not user_experiment_id: |
| 125 | + self.config.logger.log(enums.LogLevels.INFO, 'User "%s" is in no experiment.' % user_id) |
| 126 | + return None |
| 127 | + |
| 128 | + if user_experiment_id != experiment_id: |
| 129 | + self.config.logger.log(enums.LogLevels.INFO, 'User "%s" is not in experiment "%s" of group %s.' % |
| 130 | + (user_id, experiment_key, group_id)) |
| 131 | + return None |
| 132 | + |
| 133 | + self.config.logger.log(enums.LogLevels.INFO, 'User "%s" is in experiment %s of group %s.' % |
| 134 | + (user_id, experiment_key, group_id)) |
| 135 | + |
| 136 | + # Bucket user if not in white-list and in group (if any) |
| 137 | + experiment_traffic_allocations = self.config.get_traffic_allocation(self.config.experiment_key_map, experiment_key) |
| 138 | + if not experiment_traffic_allocations: |
| 139 | + self.config.logger.log(enums.LogLevels.ERROR, 'Experiment key "%s" is not in datafile.' % experiment_key) |
| 140 | + self.config.error_handler.handle_error( |
| 141 | + exceptions.InvalidExperimentException(enums.Errors.INVALID_EXPERIMENT_KEY_ERROR) |
| 142 | + ) |
| 143 | + return None |
| 144 | + |
| 145 | + variation_id = self._find_bucket(user_id, experiment_id, experiment_traffic_allocations) |
| 146 | + if variation_id: |
| 147 | + variation_key = self.config.get_variation_key_from_id(experiment_key, variation_id) |
| 148 | + self.config.logger.log(enums.LogLevels.INFO, 'User "%s" is in variation "%s" of experiment %s.' % |
| 149 | + (user_id, variation_key, experiment_key)) |
| 150 | + return variation_id |
| 151 | + |
| 152 | + self.config.logger.log(enums.LogLevels.INFO, 'User "%s" is in no variation.' % user_id) |
| 153 | + return None |
0 commit comments