Skip to content

add features to project config #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ Double getVariableDouble(@Nonnull String variableKey,
return null;
}

//======== Feature APIs ========//
//======== FeatureFlag APIs ========//

/**
* Determine whether a boolean feature is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import java.util.List;

/**
* Represents a Feature definition at the project level
* Represents a FeatureFlag definition at the project level
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Feature implements IdKeyMapped{
public class FeatureFlag implements IdKeyMapped{

private final String id;
private final String key;
Expand All @@ -35,11 +35,11 @@ public class Feature implements IdKeyMapped{
private final List<LiveVariable> variables;

@JsonCreator
public Feature(@JsonProperty("id") String id,
@JsonProperty("key") String key,
@JsonProperty("layerId") String layerId,
@JsonProperty("experimentIds") List<String> experimentIds,
@JsonProperty("variables") List<LiveVariable> variables) {
public FeatureFlag(@JsonProperty("id") String id,
@JsonProperty("key") String key,
@JsonProperty("layerId") String layerId,
@JsonProperty("experimentIds") List<String> experimentIds,
@JsonProperty("variables") List<LiveVariable> variables) {
this.id = id;
this.key = key;
this.layerId = layerId;
Expand Down Expand Up @@ -69,7 +69,7 @@ public List<LiveVariable> getVariables() {

@Override
public String toString() {
return "Feature{" +
return "FeatureFlag{" +
"id='" + id + '\'' +
", key='" + key + '\'' +
", layerId='" + layerId + '\'' +
Expand Down
71 changes: 59 additions & 12 deletions core-api/src/main/java/com/optimizely/ab/config/ProjectConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class ProjectConfig {

public enum Version {
V2 ("2"),
V3 ("3");
V3 ("3"),
V4 ("4");

private final String version;

Expand All @@ -56,54 +57,100 @@ public String toString() {
private final String revision;
private final String version;
private final boolean anonymizeIP;
private final List<Group> groups;
private final List<Experiment> experiments;
private final List<Attribute> attributes;
private final List<EventType> events;
private final List<Audience> audiences;
private final List<EventType> events;
private final List<Experiment> experiments;
private final List<FeatureFlag> featureFlags;
private final List<Group> groups;
private final List<LiveVariable> liveVariables;

// convenience mappings for efficient lookup
private final Map<String, Experiment> experimentKeyMapping;
// key to entity mappings
private final Map<String, Attribute> attributeKeyMapping;
private final Map<String, LiveVariable> liveVariableKeyMapping;
private final Map<String, EventType> eventNameMapping;
private final Map<String, Experiment> experimentKeyMapping;
private final Map<String, FeatureFlag> featureKeyMapping;
private final Map<String, LiveVariable> liveVariableKeyMapping;

// id to entity mappings
private final Map<String, Audience> audienceIdMapping;
private final Map<String, Experiment> experimentIdMapping;
private final Map<String, Group> groupIdMapping;

// other mappings
private final Map<String, List<Experiment>> liveVariableIdToExperimentsMapping;
private final Map<String, Map<String, LiveVariableUsageInstance>> variationToLiveVariableUsageInstanceMapping;

// v2 constructor
public ProjectConfig(String accountId, String projectId, String version, String revision, List<Group> groups,
List<Experiment> experiments, List<Attribute> attributes, List<EventType> eventType,
List<Audience> audiences) {
this(accountId, projectId, version, revision, groups, experiments, attributes, eventType, audiences, false,
null);
}

// v3 constructor
public ProjectConfig(String accountId, String projectId, String version, String revision, List<Group> groups,
List<Experiment> experiments, List<Attribute> attributes, List<EventType> eventType,
List<Audience> audiences, boolean anonymizeIP, List<LiveVariable> liveVariables) {
this(
accountId,
anonymizeIP,
projectId,
revision,
version,
attributes,
audiences,
eventType,
experiments,
null,
groups,
liveVariables
);
}

// v4 constructor
public ProjectConfig(String accountId,
boolean anonymizeIP,
String projectId,
String revision,
String version,
List<Attribute> attributes,
List<Audience> audiences,
List<EventType> events,
List<Experiment> experiments,
List<FeatureFlag> featureFlags,
List<Group> groups,
List<LiveVariable> liveVariables) {

this.accountId = accountId;
this.projectId = projectId;
this.version = version;
this.revision = revision;
this.anonymizeIP = anonymizeIP;

this.attributes = Collections.unmodifiableList(attributes);
this.audiences = Collections.unmodifiableList(audiences);
this.events = Collections.unmodifiableList(events);
if (featureFlags == null) {
this.featureFlags = Collections.emptyList();
}
else {
this.featureFlags = Collections.unmodifiableList(featureFlags);
}

this.groups = Collections.unmodifiableList(groups);

List<Experiment> allExperiments = new ArrayList<Experiment>();
allExperiments.addAll(experiments);
allExperiments.addAll(aggregateGroupExperiments(groups));
this.experiments = Collections.unmodifiableList(allExperiments);
this.attributes = Collections.unmodifiableList(attributes);
this.events = Collections.unmodifiableList(eventType);
this.audiences = Collections.unmodifiableList(audiences);

// generate the name mappers
this.experimentKeyMapping = ProjectConfigUtils.generateNameMapping(this.experiments);
this.attributeKeyMapping = ProjectConfigUtils.generateNameMapping(attributes);
this.eventNameMapping = ProjectConfigUtils.generateNameMapping(events);
this.eventNameMapping = ProjectConfigUtils.generateNameMapping(this.events);
this.experimentKeyMapping = ProjectConfigUtils.generateNameMapping(this.experiments);
this.featureKeyMapping = ProjectConfigUtils.generateNameMapping(this.featureFlags);

// generate audience id to audience mapping
this.audienceIdMapping = ProjectConfigUtils.generateIdMapping(audiences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public int hashCode() {

@Override
public String toString() {
return "Feature{" +
return "FeatureFlag{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", type='" + type + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -618,4 +619,26 @@ private static void verifyLiveVariableInstances(List<LiveVariableUsageInstance>
}
}
}

public static <T> List<T> createListOfObjects(T ... elements) {
ArrayList<T> list = new ArrayList<T>(elements.length);
for (T element : elements) {
list.add(element);
}
return list;
}

public static <K, V> Map<K, V> createMapOfObjects(List<K>keys, List<V>values) {
HashMap<K, V> map = new HashMap<K, V>(keys.size());
if (keys.size() == values.size()) {
Iterator<K> keysIterator = keys.iterator();
Iterator<V> valuesIterator = values.iterator();
while (keysIterator.hasNext() && valuesIterator.hasNext()) {
K key = keysIterator.next();
V value = valuesIterator.next();
map.put(key, value);
}
}
return map;
}
}
23 changes: 22 additions & 1 deletion core-api/src/test/resources/config/valid-project-config-v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@
"forcedVariations": {
"Harry Potter": "Control"
}
},
{
"id": "3072915611",
"key": "launched_experiment",
"layerId": "3587821424",
"status": "Launched",
"variations": [
{
"id": "1647582435",
"key": "launch_control",
"variables": []
}
],
"trafficAllocation": [
{
"entityId": "1647582435",
"endOfRang": 8000
}
],
"audienceIds": [],
"forcedVariations": {}
}
],
"groups": [
Expand Down Expand Up @@ -281,7 +302,7 @@
]
}
],
"features": [
"featureFlags": [
{
"id": "4195505407",
"key": "boolean_feature",
Expand Down