use of com.optimizely.ab.config.audience.Audience in project java-sdk by optimizely.
the class ProjectConfigTestUtils method verifyAudiences.
/**
* Asserts that the provided audience configs are equivalent.
*/
private static void verifyAudiences(List<Audience> actual, List<Audience> expected) {
assertThat(actual.size(), is(expected.size()));
for (int i = 0; i < actual.size(); i++) {
Audience actualAudience = actual.get(i);
Audience expectedAudience = expected.get(i);
assertThat(actualAudience.getId(), is(expectedAudience.getId()));
assertThat(actualAudience.getKey(), is(expectedAudience.getKey()));
assertThat(actualAudience.getConditions(), is(expectedAudience.getConditions()));
assertThat(actualAudience.getConditions(), is(expectedAudience.getConditions()));
}
}
use of com.optimizely.ab.config.audience.Audience in project java-sdk by optimizely.
the class ProjectConfigTestUtils method generateValidProjectConfigV3.
private static ProjectConfig generateValidProjectConfigV3() {
List<LiveVariableUsageInstance> variationVtag1VariableUsageInstances = asList(new LiveVariableUsageInstance("6", "True"), new LiveVariableUsageInstance("2", "10"), new LiveVariableUsageInstance("3", "string_var_vtag1"), new LiveVariableUsageInstance("4", "5.3"));
List<LiveVariableUsageInstance> variationVtag2VariableUsageInstances = asList(new LiveVariableUsageInstance("6", "False"), new LiveVariableUsageInstance("2", "20"), new LiveVariableUsageInstance("3", "string_var_vtag2"), new LiveVariableUsageInstance("4", "6.3"));
List<Experiment> experiments = asList(new Experiment("223", "etag1", "Running", "1", singletonList("100"), asList(new Variation("276", "vtag1", variationVtag1VariableUsageInstances), new Variation("277", "vtag2", variationVtag2VariableUsageInstances)), Collections.singletonMap("testUser1", "vtag1"), asList(new TrafficAllocation("276", 3500), new TrafficAllocation("277", 9000)), ""), new Experiment("118", "etag2", "Not started", "2", singletonList("100"), asList(new Variation("278", "vtag3", Collections.<LiveVariableUsageInstance>emptyList()), new Variation("279", "vtag4", Collections.<LiveVariableUsageInstance>emptyList())), Collections.singletonMap("testUser3", "vtag3"), asList(new TrafficAllocation("278", 4500), new TrafficAllocation("279", 9000)), ""));
List<Attribute> attributes = singletonList(new Attribute("134", "browser_type"));
List<String> singleExperimentId = singletonList("223");
List<String> multipleExperimentIds = asList("118", "223");
List<EventType> events = asList(new EventType("971", "clicked_cart", singleExperimentId), new EventType("098", "Total Revenue", singleExperimentId), new EventType("099", "clicked_purchase", multipleExperimentIds), new EventType("100", "no_running_experiments", singletonList("118")));
List<Condition> userAttributes = new ArrayList<Condition>();
userAttributes.add(new UserAttribute("browser_type", "custom_dimension", "firefox"));
OrCondition orInner = new OrCondition(userAttributes);
NotCondition notCondition = new NotCondition(orInner);
List<Condition> outerOrList = new ArrayList<Condition>();
outerOrList.add(notCondition);
OrCondition orOuter = new OrCondition(outerOrList);
List<Condition> andList = new ArrayList<Condition>();
andList.add(orOuter);
AndCondition andCondition = new AndCondition(andList);
List<Audience> audiences = singletonList(new Audience("100", "not_firefox_users", andCondition));
Map<String, String> userIdToVariationKeyMap = new HashMap<String, String>();
userIdToVariationKeyMap.put("testUser1", "e1_vtag1");
userIdToVariationKeyMap.put("testUser2", "e1_vtag2");
List<Experiment> randomGroupExperiments = asList(new Experiment("301", "group_etag2", "Running", "3", singletonList("100"), asList(new Variation("282", "e2_vtag1", Collections.<LiveVariableUsageInstance>emptyList()), new Variation("283", "e2_vtag2", Collections.<LiveVariableUsageInstance>emptyList())), Collections.<String, String>emptyMap(), asList(new TrafficAllocation("282", 5000), new TrafficAllocation("283", 10000)), "42"), new Experiment("300", "group_etag1", "Running", "4", singletonList("100"), asList(new Variation("280", "e1_vtag1", Collections.singletonList(new LiveVariableUsageInstance("7", "True"))), new Variation("281", "e1_vtag2", Collections.singletonList(new LiveVariableUsageInstance("7", "False")))), userIdToVariationKeyMap, asList(new TrafficAllocation("280", 3000), new TrafficAllocation("281", 10000)), "42"));
List<Experiment> overlappingGroupExperiments = asList(new Experiment("302", "overlapping_etag1", "Running", "5", singletonList("100"), asList(new Variation("284", "e1_vtag1", Collections.<LiveVariableUsageInstance>emptyList()), new Variation("285", "e1_vtag2", Collections.<LiveVariableUsageInstance>emptyList())), userIdToVariationKeyMap, asList(new TrafficAllocation("284", 1500), new TrafficAllocation("285", 3000)), "43"));
Group randomPolicyGroup = new Group("42", "random", randomGroupExperiments, asList(new TrafficAllocation("300", 3000), new TrafficAllocation("301", 9000), new TrafficAllocation("", 10000)));
Group overlappingPolicyGroup = new Group("43", "overlapping", overlappingGroupExperiments, Collections.<TrafficAllocation>emptyList());
List<Group> groups = asList(randomPolicyGroup, overlappingPolicyGroup);
List<LiveVariable> liveVariables = asList(new LiveVariable("1", "boolean_variable", "False", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.BOOLEAN), new LiveVariable("2", "integer_variable", "5", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.INTEGER), new LiveVariable("3", "string_variable", "string_live_variable", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.STRING), new LiveVariable("4", "double_variable", "13.37", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.DOUBLE), new LiveVariable("5", "archived_variable", "True", LiveVariable.VariableStatus.ARCHIVED, LiveVariable.VariableType.BOOLEAN), new LiveVariable("6", "etag1_variable", "False", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.BOOLEAN), new LiveVariable("7", "group_etag1_variable", "False", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.BOOLEAN), new LiveVariable("8", "unused_string_variable", "unused_variable", LiveVariable.VariableStatus.ACTIVE, LiveVariable.VariableType.STRING));
return new ProjectConfig("789", "1234", "3", "42", groups, experiments, attributes, events, audiences, true, liveVariables);
}
use of com.optimizely.ab.config.audience.Audience in project java-sdk by optimizely.
the class JsonConfigParser method parseAudiences.
private List<Audience> parseAudiences(JSONArray audienceJson) {
List<Audience> audiences = new ArrayList<Audience>(audienceJson.length());
for (Object obj : audienceJson) {
JSONObject audienceObject = (JSONObject) obj;
String id = audienceObject.getString("id");
String key = audienceObject.getString("name");
String conditionString = audienceObject.getString("conditions");
JSONArray conditionJson = new JSONArray(conditionString);
Condition conditions = parseConditions(conditionJson);
audiences.add(new Audience(id, key, conditions));
}
return audiences;
}
use of com.optimizely.ab.config.audience.Audience in project java-sdk by optimizely.
the class AudienceJacksonDeserializer method deserialize.
@Override
public Audience deserialize(JsonParser parser, DeserializationContext context) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = parser.getCodec().readTree(parser);
String id = node.get("id").textValue();
String name = node.get("name").textValue();
List<Object> rawObjectList = (List<Object>) mapper.readValue(node.get("conditions").textValue(), List.class);
Condition conditions = parseConditions(rawObjectList);
return new Audience(id, name, conditions);
}
use of com.optimizely.ab.config.audience.Audience in project java-sdk by optimizely.
the class DecisionService method getVariationForFeatureInRollout.
/**
* Try to bucket the user into a rollout rule.
* Evaluate the user for rules in priority order by seeing if the user satisfies the audience.
* Fall back onto the everyone else rule if the user is ever excluded from a rule due to traffic allocation.
* @param featureFlag The feature flag the user wants to access.
* @param userId User Identifier
* @param filteredAttributes A map of filtered attributes.
* @return {@link FeatureDecision}
*/
@Nonnull
FeatureDecision getVariationForFeatureInRollout(@Nonnull FeatureFlag featureFlag, @Nonnull String userId, @Nonnull Map<String, String> filteredAttributes) {
// use rollout to get variation for feature
if (featureFlag.getRolloutId().isEmpty()) {
logger.info("The feature flag \"{}\" is not used in a rollout.", featureFlag.getKey());
return new FeatureDecision(null, null, null);
}
Rollout rollout = projectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
if (rollout == null) {
logger.error("The rollout with id \"{}\" was not found in the datafile for feature flag \"{}\".", featureFlag.getRolloutId(), featureFlag.getKey());
return new FeatureDecision(null, null, null);
}
// for all rules before the everyone else rule
int rolloutRulesLength = rollout.getExperiments().size();
String bucketingId = userId;
if (filteredAttributes.containsKey(BUCKETING_ATTRIBUTE)) {
bucketingId = filteredAttributes.get(BUCKETING_ATTRIBUTE);
}
Variation variation;
for (int i = 0; i < rolloutRulesLength - 1; i++) {
Experiment rolloutRule = rollout.getExperiments().get(i);
Audience audience = projectConfig.getAudienceIdMapping().get(rolloutRule.getAudienceIds().get(0));
if (ExperimentUtils.isUserInExperiment(projectConfig, rolloutRule, filteredAttributes)) {
variation = bucketer.bucket(rolloutRule, bucketingId);
if (variation == null) {
break;
}
return new FeatureDecision(rolloutRule, variation, FeatureDecision.DecisionSource.ROLLOUT);
} else {
logger.debug("User \"{}\" did not meet the conditions to be in rollout rule for audience \"{}\".", userId, audience.getName());
}
}
// get last rule which is the fall back rule
Experiment finalRule = rollout.getExperiments().get(rolloutRulesLength - 1);
if (ExperimentUtils.isUserInExperiment(projectConfig, finalRule, filteredAttributes)) {
variation = bucketer.bucket(finalRule, bucketingId);
if (variation != null) {
return new FeatureDecision(finalRule, variation, FeatureDecision.DecisionSource.ROLLOUT);
}
}
return new FeatureDecision(null, null, null);
}
Aggregations