|
| 1 | +/** |
| 2 | +* Copyright 2016, 2019-2020, Optimizely |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | +* Bucketer API for determining the variation id from the specified parameters |
| 19 | +*/ |
| 20 | +import { |
| 21 | +sprintf |
| 22 | +} from '@optimizely/js-sdk-utils'; |
| 23 | +import murmurhash from 'murmurhash'; |
| 24 | + |
| 25 | +import { |
| 26 | +ERROR_MESSAGES, |
| 27 | +LOG_LEVEL, |
| 28 | +LOG_MESSAGES, |
| 29 | +} from '../../utils/enums'; |
| 30 | + |
| 31 | +var HASH_SEED = 1; |
| 32 | +var MAX_HASH_VALUE = Math.pow(2, 32); |
| 33 | +var MAX_TRAFFIC_VALUE = 10000; |
| 34 | +var MODULE_NAME = 'BUCKETER'; |
| 35 | +var RANDOM_POLICY = 'random'; |
| 36 | + |
| 37 | +/** |
| 38 | +* Determines ID of variation to be shown for the given input params |
| 39 | +* @param {Object} bucketerParams |
| 40 | +* @param {string} bucketerParams.experimentId |
| 41 | +* @param {string} bucketerParams.experimentKey |
| 42 | +* @param {string} bucketerParams.userId |
| 43 | +* @param {Object[]} bucketerParams.trafficAllocationConfig |
| 44 | +* @param {Array} bucketerParams.experimentKeyMap |
| 45 | +* @param {Object} bucketerParams.groupIdMap |
| 46 | +* @param {Object} bucketerParams.variationIdMap |
| 47 | +* @param {string} bucketerParams.varationIdMap[].key |
| 48 | +* @param {Object} bucketerParams.logger |
| 49 | +* @param {string} bucketerParams.bucketingId |
| 50 | +* @return Variation ID that user has been bucketed into, null if user is not bucketed into any experiment |
| 51 | +*/ |
| 52 | +export var bucket = function(bucketerParams) { |
| 53 | +// Check if user is in a random group; if so, check if user is bucketed into a specific experiment |
| 54 | +var experiment = bucketerParams.experimentKeyMap[bucketerParams.experimentKey]; |
| 55 | +var groupId = experiment['groupId']; |
| 56 | +if (groupId) { |
| 57 | +var group = bucketerParams.groupIdMap[groupId]; |
| 58 | +if (!group) { |
| 59 | +throw new Error(sprintf(ERROR_MESSAGES.INVALID_GROUP_ID, MODULE_NAME, groupId)); |
| 60 | +} |
| 61 | +if (group.policy === RANDOM_POLICY) { |
| 62 | +var bucketedExperimentId = this.bucketUserIntoExperiment( |
| 63 | +group, |
| 64 | +bucketerParams.bucketingId, |
| 65 | +bucketerParams.userId, |
| 66 | +bucketerParams.logger |
| 67 | +); |
| 68 | + |
| 69 | +// Return if user is not bucketed into any experiment |
| 70 | +if (bucketedExperimentId === null) { |
| 71 | +var notbucketedInAnyExperimentLogMessage = sprintf( |
| 72 | +LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, |
| 73 | +MODULE_NAME, |
| 74 | +bucketerParams.userId, |
| 75 | +groupId |
| 76 | +); |
| 77 | +bucketerParams.logger.log(LOG_LEVEL.INFO, notbucketedInAnyExperimentLogMessage); |
| 78 | +return null; |
| 79 | +} |
| 80 | + |
| 81 | +// Return if user is bucketed into a different experiment than the one specified |
| 82 | +if (bucketedExperimentId !== bucketerParams.experimentId) { |
| 83 | +var notBucketedIntoExperimentOfGroupLogMessage = sprintf( |
| 84 | +LOG_MESSAGES.USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP, |
| 85 | +MODULE_NAME, |
| 86 | +bucketerParams.userId, |
| 87 | +bucketerParams.experimentKey, |
| 88 | +groupId |
| 89 | +); |
| 90 | +bucketerParams.logger.log(LOG_LEVEL.INFO, notBucketedIntoExperimentOfGroupLogMessage); |
| 91 | +return null; |
| 92 | +} |
| 93 | + |
| 94 | +// Continue bucketing if user is bucketed into specified experiment |
| 95 | +var bucketedIntoExperimentOfGroupLogMessage = sprintf( |
| 96 | +LOG_MESSAGES.USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP, |
| 97 | +MODULE_NAME, |
| 98 | +bucketerParams.userId, |
| 99 | +bucketerParams.experimentKey, |
| 100 | +groupId |
| 101 | +); |
| 102 | +bucketerParams.logger.log(LOG_LEVEL.INFO, bucketedIntoExperimentOfGroupLogMessage); |
| 103 | +} |
| 104 | +} |
| 105 | +var bucketingId = sprintf('%s%s', bucketerParams.bucketingId, bucketerParams.experimentId); |
| 106 | +var bucketValue = this._generateBucketValue(bucketingId); |
| 107 | + |
| 108 | +var bucketedUserLogMessage = sprintf( |
| 109 | +LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, |
| 110 | +MODULE_NAME, |
| 111 | +bucketValue, |
| 112 | +bucketerParams.userId |
| 113 | +); |
| 114 | +bucketerParams.logger.log(LOG_LEVEL.DEBUG, bucketedUserLogMessage); |
| 115 | + |
| 116 | +var entityId = this._findBucket(bucketValue, bucketerParams.trafficAllocationConfig); |
| 117 | + |
| 118 | +if (!bucketerParams.variationIdMap.hasOwnProperty(entityId)) { |
| 119 | +if (entityId) { |
| 120 | +var invalidVariationIdLogMessage = sprintf(LOG_MESSAGES.INVALID_VARIATION_ID, MODULE_NAME); |
| 121 | +bucketerParams.logger.log(LOG_LEVEL.WARNING, invalidVariationIdLogMessage); |
| 122 | +} |
| 123 | +return null; |
| 124 | +} |
| 125 | + |
| 126 | +return entityId; |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | +* Returns bucketed experiment ID to compare against experiment user is being called into |
| 131 | +* @param {Object} group Group that experiment is in |
| 132 | +* @param {string} bucketingId Bucketing ID |
| 133 | +* @param {string} userId ID of user to be bucketed into experiment |
| 134 | +* @param {Object} logger Logger implementation |
| 135 | +* @return {string|null} ID of experiment if user is bucketed into experiment within the group, null otherwise |
| 136 | +*/ |
| 137 | +export var bucketUserIntoExperiment = function(group, bucketingId, userId, logger) { |
| 138 | +var bucketingKey = sprintf('%s%s', bucketingId, group.id); |
| 139 | +var bucketValue = this._generateBucketValue(bucketingKey); |
| 140 | +logger.log( |
| 141 | +LOG_LEVEL.DEBUG, |
| 142 | +sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, MODULE_NAME, bucketValue, userId) |
| 143 | +); |
| 144 | +var trafficAllocationConfig = group.trafficAllocation; |
| 145 | +var bucketedExperimentId = this._findBucket(bucketValue, trafficAllocationConfig); |
| 146 | +return bucketedExperimentId; |
| 147 | +}; |
| 148 | + |
| 149 | +/** |
| 150 | +* Returns entity ID associated with bucket value |
| 151 | +* @param {string} bucketValue |
| 152 | +* @param {Object[]} trafficAllocationConfig |
| 153 | +* @param {number} trafficAllocationConfig[].endOfRange |
| 154 | +* @param {number} trafficAllocationConfig[].entityId |
| 155 | +* @return {string|null} Entity ID for bucketing if bucket value is within traffic allocation boundaries, null otherwise |
| 156 | +*/ |
| 157 | +export var _findBucket = function(bucketValue, trafficAllocationConfig) { |
| 158 | +for (var i = 0; i < trafficAllocationConfig.length; i++) { |
| 159 | +if (bucketValue < trafficAllocationConfig[i].endOfRange) { |
| 160 | +return trafficAllocationConfig[i].entityId; |
| 161 | +} |
| 162 | +} |
| 163 | +return null; |
| 164 | +}; |
| 165 | + |
| 166 | +/** |
| 167 | +* Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE) |
| 168 | +* @param {string} bucketingKey String value for bucketing |
| 169 | +* @return {string} the generated bucket value |
| 170 | +* @throws If bucketing value is not a valid string |
| 171 | +*/ |
| 172 | +export var _generateBucketValue = function(bucketingKey) { |
| 173 | +try { |
| 174 | +// NOTE: the mmh library already does cast the hash value as an unsigned 32bit int |
| 175 | +// https://.com/perezd/node-murmurhash/blob/master/murmurhash.js#L115 |
| 176 | +var hashValue = murmurhash.v3(bucketingKey, HASH_SEED); |
| 177 | +var ratio = hashValue / MAX_HASH_VALUE; |
| 178 | +return parseInt(ratio * MAX_TRAFFIC_VALUE, 10); |
| 179 | +} catch (ex) { |
| 180 | +throw new Error(sprintf(ERROR_MESSAGES.INVALID_BUCKETING_ID, MODULE_NAME, bucketingKey, ex.message)); |
| 181 | +} |
| 182 | +}; |
| 183 | + |
| 184 | +export default { |
| 185 | +bucket: bucket, |
| 186 | +bucketUserIntoExperiment: bucketUserIntoExperiment, |
| 187 | +_findBucket: _findBucket, |
| 188 | +_generateBucketValue: _generateBucketValue, |
| 189 | +}; |
0 commit comments