use of com.optimizely.ab.config.TrafficAllocation in project java-sdk by optimizely.
the class GsonHelpers method parseTrafficAllocation.
static List<TrafficAllocation> parseTrafficAllocation(JsonArray trafficAllocationJson) {
List<TrafficAllocation> trafficAllocation = new ArrayList<TrafficAllocation>(trafficAllocationJson.size());
for (Object obj : trafficAllocationJson) {
JsonObject allocationObject = (JsonObject) obj;
String entityId = allocationObject.get("entityId").getAsString();
int endOfRange = allocationObject.get("endOfRange").getAsInt();
trafficAllocation.add(new TrafficAllocation(entityId, endOfRange));
}
return trafficAllocation;
}
use of com.optimizely.ab.config.TrafficAllocation in project java-sdk by optimizely.
the class GsonHelpers method parseExperiment.
static Experiment parseExperiment(JsonObject experimentJson, String groupId, JsonDeserializationContext context) {
String id = experimentJson.get("id").getAsString();
String key = experimentJson.get("key").getAsString();
JsonElement experimentStatusJson = experimentJson.get("status");
String status = experimentStatusJson.isJsonNull() ? ExperimentStatus.NOT_STARTED.toString() : experimentStatusJson.getAsString();
JsonElement layerIdJson = experimentJson.get("layerId");
String layerId = layerIdJson == null ? null : layerIdJson.getAsString();
JsonArray audienceIdsJson = experimentJson.getAsJsonArray("audienceIds");
List<String> audienceIds = new ArrayList<String>(audienceIdsJson.size());
for (JsonElement audienceIdObj : audienceIdsJson) {
audienceIds.add(audienceIdObj.getAsString());
}
// parse the child objects
List<Variation> variations = parseVariations(experimentJson.getAsJsonArray("variations"), context);
Map<String, String> userIdToVariationKeyMap = parseForcedVariations(experimentJson.getAsJsonObject("forcedVariations"));
List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(experimentJson.getAsJsonArray("trafficAllocation"));
return new Experiment(id, key, status, layerId, audienceIds, variations, userIdToVariationKeyMap, trafficAllocations, groupId);
}
use of com.optimizely.ab.config.TrafficAllocation in project java-sdk by optimizely.
the class Bucketer method bucketToVariation.
private Variation bucketToVariation(@Nonnull Experiment experiment, @Nonnull String bucketingId) {
// "salt" the bucket id using the experiment id
String experimentId = experiment.getId();
String experimentKey = experiment.getKey();
String combinedBucketId = bucketingId + experimentId;
List<TrafficAllocation> trafficAllocations = experiment.getTrafficAllocation();
int hashCode = MurmurHash3.murmurhash3_x86_32(combinedBucketId, 0, combinedBucketId.length(), MURMUR_HASH_SEED);
int bucketValue = generateBucketValue(hashCode);
logger.debug("Assigned bucket {} to user with bucketingId \"{}\" when bucketing to a variation.", bucketValue, bucketingId);
String bucketedVariationId = bucketToEntity(bucketValue, trafficAllocations);
if (bucketedVariationId != null) {
Variation bucketedVariation = experiment.getVariationIdToVariationMap().get(bucketedVariationId);
String variationKey = bucketedVariation.getKey();
logger.info("User with bucketingId \"{}\" is in variation \"{}\" of experiment \"{}\".", bucketingId, variationKey, experimentKey);
return bucketedVariation;
}
// user was not bucketed to a variation
logger.info("User with bucketingId \"{}\" is not in any variation of experiment \"{}\".", bucketingId, experimentKey);
return null;
}
use of com.optimizely.ab.config.TrafficAllocation in project java-sdk by optimizely.
the class GroupGsonDeserializer method deserialize.
@Override
public Group deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String id = jsonObject.get("id").getAsString();
String policy = jsonObject.get("policy").getAsString();
List<Experiment> experiments = new ArrayList<Experiment>();
JsonArray experimentsJson = jsonObject.getAsJsonArray("experiments");
for (Object obj : experimentsJson) {
JsonObject experimentObj = (JsonObject) obj;
experiments.add(GsonHelpers.parseExperiment(experimentObj, id, context));
}
List<TrafficAllocation> trafficAllocations = GsonHelpers.parseTrafficAllocation(jsonObject.getAsJsonArray("trafficAllocation"));
return new Group(id, policy, experiments, trafficAllocations);
}
use of com.optimizely.ab.config.TrafficAllocation in project java-sdk by optimizely.
the class JsonConfigParser method parseExperiments.
private List<Experiment> parseExperiments(JSONArray experimentJson, String groupId) {
List<Experiment> experiments = new ArrayList<Experiment>(experimentJson.length());
for (Object obj : experimentJson) {
JSONObject experimentObject = (JSONObject) obj;
String id = experimentObject.getString("id");
String key = experimentObject.getString("key");
String status = experimentObject.isNull("status") ? ExperimentStatus.NOT_STARTED.toString() : experimentObject.getString("status");
String layerId = experimentObject.has("layerId") ? experimentObject.getString("layerId") : null;
JSONArray audienceIdsJson = experimentObject.getJSONArray("audienceIds");
List<String> audienceIds = new ArrayList<String>(audienceIdsJson.length());
for (Object audienceIdObj : audienceIdsJson) {
audienceIds.add((String) audienceIdObj);
}
// parse the child objects
List<Variation> variations = parseVariations(experimentObject.getJSONArray("variations"));
Map<String, String> userIdToVariationKeyMap = parseForcedVariations(experimentObject.getJSONObject("forcedVariations"));
List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(experimentObject.getJSONArray("trafficAllocation"));
experiments.add(new Experiment(id, key, status, layerId, audienceIds, variations, userIdToVariationKeyMap, trafficAllocations, groupId));
}
return experiments;
}
Aggregations