Search in sources :

Example 6 with TrafficAllocation

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;
}
Also used : TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 7 with 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);
}
Also used : JsonArray(com.google.gson.JsonArray) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) JsonElement(com.google.gson.JsonElement) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Variation(com.optimizely.ab.config.Variation)

Example 8 with TrafficAllocation

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;
}
Also used : TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Variation(com.optimizely.ab.config.Variation)

Example 9 with TrafficAllocation

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);
}
Also used : JsonArray(com.google.gson.JsonArray) Group(com.optimizely.ab.config.Group) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 10 with TrafficAllocation

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;
}
Also used : Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) JSONObject(org.json.JSONObject) Variation(com.optimizely.ab.config.Variation)

Aggregations

TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)15 Experiment (com.optimizely.ab.config.Experiment)11 ArrayList (java.util.ArrayList)11 Variation (com.optimizely.ab.config.Variation)8 Group (com.optimizely.ab.config.Group)4 JSONObject (org.json.JSONObject)3 JSONObject (org.json.simple.JSONObject)3 Test (org.junit.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JsonElement (com.google.gson.JsonElement)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 JSONArray (org.json.simple.JSONArray)1 Matchers.anyString (org.mockito.Matchers.anyString)1