Ejemplo n.º 1
0
  getForcedVariation: function(projectConfig, experimentKey, userId, logger) {
    var experimentToVariationMap = projectConfig.forcedVariationMap[userId];
    if (!experimentToVariationMap) {
      logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.USER_HAS_NO_FORCED_VARIATION, MODULE_NAME, userId));
      return null;
    }

    var experimentId;
    try {
      var experiment = this.getExperimentFromKey(projectConfig, experimentKey);
      if (experiment.hasOwnProperty('id')) {
        experimentId = experiment['id'];
      } else {
        // catching improperly formatted experiments
        logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.IMPROPERLY_FORMATTED_EXPERIMENT, MODULE_NAME, experimentKey));
        return null;
      }
    } catch (ex) {
      // catching experiment not in datafile
      logger.log(LOG_LEVEL.ERROR, ex.message);
      return null;
    }

    var variationId = experimentToVariationMap[experimentId];
    if (!variationId) {
      logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.USER_HAS_NO_FORCED_VARIATION_FOR_EXPERIMENT, MODULE_NAME, experimentKey, userId));
      return null;
    }

    var variationKey = this.getVariationKeyFromId(projectConfig, variationId);
    logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.USER_HAS_FORCED_VARIATION, MODULE_NAME, variationKey, experimentKey, userId));

    return variationKey;
  },
Ejemplo n.º 2
0
  removeForcedVariation: function(projectConfig, userId, experimentId, experimentKey, logger) {
    if (!userId) {
      throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_ID, MODULE_NAME));
    }

    if (projectConfig.forcedVariationMap.hasOwnProperty(userId)) {
      delete projectConfig.forcedVariationMap[userId][experimentId];
      logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.VARIATION_REMOVED_FOR_USER, MODULE_NAME, experimentKey, userId));
    } else {
      throw new Error(sprintf(ERROR_MESSAGES.USER_NOT_IN_FORCED_VARIATION, MODULE_NAME, userId));
    }
  },
Ejemplo n.º 3
0
    var evaluateAudience = function(audienceId) {
      var audience = audiencesById[audienceId];
      if (audience) {
        logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.EVALUATING_AUDIENCE, MODULE_NAME, audienceId, JSON.stringify(audience.conditions)));
        var result = conditionTreeEvaluator.evaluate(audience.conditions, evaluateConditionWithUserAttributes);
        var resultText = result === null ? 'UNKNOWN' : result.toString().toUpperCase();
        logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.AUDIENCE_EVALUATION_RESULT, MODULE_NAME, audienceId, resultText));
        return result;
      }

      return null;
    };
Ejemplo n.º 4
0
  getVariableForFeature: function(projectConfig, featureKey, variableKey, logger) {
    var feature = projectConfig.featureKeyMap[featureKey];
    if (!feature) {
      logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.FEATURE_NOT_IN_DATAFILE, MODULE_NAME, featureKey));
      return null;
    }

    var variable = feature.variableKeyMap[variableKey];
    if (!variable) {
      logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.VARIABLE_KEY_NOT_IN_DATAFILE, MODULE_NAME, variableKey, featureKey));
      return null;
    }

    return variable;
  },
Ejemplo n.º 5
0
 getTrafficAllocation: function(projectConfig, experimentKey) {
   var experiment = projectConfig.experimentKeyMap[experimentKey];
   if (fns.isEmpty(experiment)) {
     throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
   }
   return experiment.trafficAllocation;
 },
Ejemplo n.º 6
0
 getLayerId: function(projectConfig, experimentId) {
   var experiment = projectConfig.experimentIdMap[experimentId];
   if (fns.isEmpty(experiment)) {
     throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_ID, MODULE_NAME, experimentId));
   }
   return experiment.layerId;
 },
Ejemplo n.º 7
0
  getAttributeId: function(projectConfig, attributeKey, logger) {
    var attribute = projectConfig.attributeKeyMap[attributeKey];
    var hasReservedPrefix = attributeKey.indexOf(RESERVED_ATTRIBUTE_PREFIX) === 0;
    if (attribute) {
      if (hasReservedPrefix) {
        logger.log(LOG_LEVEL.WARN,
                   sprintf('Attribute %s unexpectedly has reserved prefix %s; using attribute ID instead of reserved attribute name.', attributeKey, RESERVED_ATTRIBUTE_PREFIX));
      }
      return attribute.id;
    } else if (hasReservedPrefix) {
      return attributeKey;
    }

    logger.log(LOG_LEVEL.DEBUG, sprintf(ERROR_MESSAGES.UNRECOGNIZED_ATTRIBUTE, MODULE_NAME, attributeKey));
    return null;
  },
Ejemplo n.º 8
0
 it('should complain if the provided logger is invalid', function() {
   assert.throws(function() {
     configValidator.validate({
       logger: {},
     });
   }, sprintf(ERROR_MESSAGES.INVALID_LOGGER, 'CONFIG_VALIDATOR'));
 });
Ejemplo n.º 9
0
 it('should complain if the provided event dispatcher is invalid', function() {
   assert.throws(function() {
     configValidator.validate({
       eventDispatcher: {},
     });
   }, sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, 'CONFIG_VALIDATOR'));
 });
Ejemplo n.º 10
0
 it('should complain if the provided error handler is invalid', function() {
   assert.throws(function() {
     configValidator.validate({
       errorHandler: {},
     });
   }, sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, 'CONFIG_VALIDATOR'));
 });
Ejemplo n.º 11
0
  setForcedVariation: function(projectConfig, experimentKey, userId, variationKey, logger) {
    if (variationKey != null && !stringValidator.validate(variationKey)) {
      logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_VARIATION_KEY, MODULE_NAME));
      return false;
    }

    var experimentId;
    try {
      var experiment = this.getExperimentFromKey(projectConfig, experimentKey);
      if (experiment.hasOwnProperty('id')) {
        experimentId = experiment['id'];
      } else {
        // catching improperly formatted experiments
        logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.IMPROPERLY_FORMATTED_EXPERIMENT, MODULE_NAME, experimentKey));
        return false;
      }
    } catch (ex) {
      // catching experiment not in datafile
      logger.log(LOG_LEVEL.ERROR, ex.message);
      return false;
    }

    if (variationKey == null) {
      try {
        this.removeForcedVariation(projectConfig, userId, experimentId, experimentKey, logger);
        return true;
      } catch (ex) {
        logger.log(LOG_LEVEL.ERROR, ex.message);
        return false;
      }
    }

    var variationId = this.getVariationIdFromExperimentAndVariationKey(projectConfig, experimentKey, variationKey);

    if (!variationId) {
      logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.NO_VARIATION_FOR_EXPERIMENT_KEY, MODULE_NAME, variationKey, experimentKey));
      return false;
    }

    try {
      this.setInForcedVariationMap(projectConfig, userId, experimentId, variationId, logger);
      return true;
    } catch (ex) {
      logger.log(LOG_LEVEL.ERROR, ex.message);
      return false;
    }
  },
Ejemplo n.º 12
0
  getExperimentAudienceConditions: function(projectConfig, experimentKey) {
    var experiment = projectConfig.experimentKeyMap[experimentKey];
    if (fns.isEmpty(experiment)) {
      throw new Error(sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
    }

    return experiment.audienceConditions || experiment.audienceIds;
  },
Ejemplo n.º 13
0
 it('should throw if the instance does not provide a \'save\' function', function() {
   var missingSaveFunction = {
     lookup: function() {}
   };
   assert.throws(function() {
     userProfileServiceValidator.validate(missingSaveFunction);
   }, sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, 'USER_PROFILE_SERVICE_VALIDATOR', 'Missing function \'save\''));
 });
Ejemplo n.º 14
0
 it('should throw if \'save\' is not a function', function() {
   var saveNotFunction = {
     lookup: function() {},
     save: 'notGonnaWork',
   };
   assert.throws(function() {
     userProfileServiceValidator.validate(saveNotFunction);
   }, sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, 'USER_PROFILE_SERVICE_VALIDATOR', 'Missing function \'save\''));
 });
Ejemplo n.º 15
0
  getExperimentFromKey: function(projectConfig, experimentKey) {
    if (projectConfig.experimentKeyMap.hasOwnProperty(experimentKey)) {
      var experiment = projectConfig.experimentKeyMap[experimentKey];
      if (!!experiment) {
        return experiment;
      }
    }

    throw new Error(sprintf(ERROR_MESSAGES.EXPERIMENT_KEY_NOT_IN_DATAFILE, MODULE_NAME, experimentKey));
  },
Ejemplo n.º 16
0
  setInForcedVariationMap: function(projectConfig, userId, experimentId, variationId, logger) {
    if (projectConfig.forcedVariationMap.hasOwnProperty(userId)) {
      projectConfig.forcedVariationMap[userId][experimentId] = variationId;
    } else {
      projectConfig.forcedVariationMap[userId] = {};
      projectConfig.forcedVariationMap[userId][experimentId] = variationId;
    }

    logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.USER_MAPPED_TO_FORCED_VARIATION, MODULE_NAME, variationId, experimentId, userId));
  },
Ejemplo n.º 17
0
  getExperimentFromId: function(projectConfig, experimentId, logger) {
    if (projectConfig.experimentIdMap.hasOwnProperty(experimentId)) {
      var experiment = projectConfig.experimentIdMap[experimentId];
      if (!!experiment) {
        return experiment;
      }
    }

    logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_ID, MODULE_NAME, experimentId));
    return null;
  },
Ejemplo n.º 18
0
  getFeatureFromKey: function(projectConfig, featureKey, logger) {
    if (projectConfig.featureKeyMap.hasOwnProperty(featureKey)) {
      var feature = projectConfig.featureKeyMap[featureKey];
      if (!!feature) {
        return feature;
      }
    }

    logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.FEATURE_NOT_IN_DATAFILE, MODULE_NAME, featureKey));
    return null;
  },
Ejemplo n.º 19
0
  getVariableValueForVariation: function(projectConfig, variable, variation, logger) {
    if (!variable || !variation) {
      return null;
    }

    if (!projectConfig.variationVariableUsageMap.hasOwnProperty(variation.id)) {
      logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT, MODULE_NAME, variation.id));
      return null;
    }

    var variableUsages = projectConfig.variationVariableUsageMap[variation.id];
    var variableUsage = variableUsages[variable.id];

    return variableUsage ? variableUsage.value : null;
  },
Ejemplo n.º 20
0
  getTypeCastValue: function(variableValue, variableType, logger) {
    var castValue;

    switch (variableType) {
      case FEATURE_VARIABLE_TYPES.BOOLEAN:
        if (variableValue !== 'true' && variableValue !== 'false') {
          logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
          castValue = null;
        } else {
          castValue = variableValue === 'true';
        }
        break;

      case FEATURE_VARIABLE_TYPES.INTEGER:
        castValue = parseInt(variableValue, 10);
        if (isNaN(castValue)) {
          logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
          castValue = null;
        }
        break;

      case FEATURE_VARIABLE_TYPES.DOUBLE:
        castValue = parseFloat(variableValue);
        if (isNaN(castValue)) {
          logger.log(LOG_LEVEL.ERROR, sprintf(ERROR_MESSAGES.UNABLE_TO_CAST_VALUE, MODULE_NAME, variableValue, variableType));
          castValue = null;
        }
        break;

      default: // type is STRING
        castValue = variableValue;
        break;
    }

    return castValue;
  },
Ejemplo n.º 21
0
 it('should complain if datafile version is not supported', function() {
   assert.throws(function() {
     configValidator.validateDatafile(JSON.stringify(testData.getUnsupportedVersionConfig()));
   }, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, 'CONFIG_VALIDATOR', '5'));
 });
Ejemplo n.º 22
0
 it('should complain if datafile is malformed', function() {
   assert.throws(function() {
     configValidator.validateDatafile('abc');
   }, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'CONFIG_VALIDATOR'));
 });
Ejemplo n.º 23
0
 it('should complain if datafile is not provided', function() {
   assert.throws(function() {
     configValidator.validateDatafile();
   }, sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, 'CONFIG_VALIDATOR'));
 });