Skip to content

feat: Added support to get All segments used in the project #483

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 4 commits into from
Aug 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class DatafileProjectConfig implements ProjectConfig {
private final List<Group> groups;
private final List<Rollout> rollouts;
private final List<Integration> integrations;
private final Set<String> allSegments;

// key to entity mappings
private final Map<String, Attribute> attributeKeyMapping;
Expand Down Expand Up @@ -204,6 +205,15 @@ public DatafileProjectConfig(String accountId,
this.publicKeyForODP = publicKeyForODP;
this.hostForODP = hostForODP;

Set<String> allSegments = new HashSet<>();
if (typedAudiences != null) {
for(Audience audience: typedAudiences) {
allSegments.addAll(audience.getSegments());
}
}

this.allSegments = allSegments;

Map<String, Experiment> variationIdToExperimentMap = new HashMap<String, Experiment>();
for (Experiment experiment : this.experiments) {
for (Variation variation : experiment.getVariations()) {
Expand Down Expand Up @@ -424,6 +434,10 @@ public List<Experiment> getExperiments() {
return experiments;
}

public Set<String> getAllSegments() {
return this.allSegments;
}

@Override
public List<Experiment> getExperimentsForEventKey(String eventKey) {
EventType event = eventNameMapping.get(eventKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public AndCondition(@Nonnull List<Condition> conditions) {
this.conditions = conditions;
}

@Override
public List<Condition> getConditions() {
return conditions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2017, 2019, Optimizely and contributors
* Copyright 2016-2017, 2019, 2022, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,9 @@
import com.optimizely.ab.config.IdKeyMapped;

import javax.annotation.concurrent.Immutable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Represents the Optimizely Audience configuration.
Expand Down Expand Up @@ -69,4 +72,27 @@ public String toString() {
", conditions=" + conditions +
'}';
}

public Set<String> getSegments() {
return getSegments(conditions);
}

private static Set<String> getSegments(Condition conditions) {
List<Condition> nestedConditions = conditions.getConditions();
Set<String> segments = new HashSet<>();
if (nestedConditions != null) {
for (Condition nestedCondition : nestedConditions) {
Set<String> nestedSegments = getSegments(nestedCondition);
segments.addAll(nestedSegments);
}
} else {
if (conditions.getClass() == UserAttribute.class) {
UserAttribute userAttributeCondition = (UserAttribute) conditions;
if (UserAttribute.QUALIFIED.equals(userAttributeCondition.getMatch())) {
segments.add((String)userAttributeCondition.getValue());
}
}
}
return segments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -97,6 +98,11 @@ public boolean equals(Object o) {
(audienceId.equals(condition.audienceId)));
}

@Override
public List<Condition> getConditions() {
return null;
}

@Override
public int hashCode() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.optimizely.ab.config.ProjectConfig;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -33,4 +34,6 @@ public interface Condition<T> {
String toJson();

String getOperandOrId();

List<Condition> getConditions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import javax.annotation.Nullable;
import java.util.Map;

public class EmptyCondition<T> implements Condition<T> {
public class EmptyCondition<T> extends LeafCondition<T> {
@Nullable
@Override
public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2022, Optimizely Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.optimizely.ab.config.audience;

import java.util.List;

public abstract class LeafCondition<T> implements Condition<T> {

@Override
public List<Condition> getConditions() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.List;


/**
Expand All @@ -41,6 +43,11 @@ public Condition getCondition() {
return condition;
}

@Override
public List<Condition> getConditions() {
return Arrays.asList(condition);
}

@Nullable
public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
Boolean conditionEval = condition == null ? null : condition.evaluate(config, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import javax.annotation.Nullable;
import java.util.Map;

public class NullCondition<T> implements Condition<T> {
public class NullCondition<T> extends LeafCondition<T> {
@Nullable
@Override
public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public OrCondition(@Nonnull List<Condition> conditions) {
this.conditions = conditions;
}

@Override
public List<Condition> getConditions() {
return conditions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
@Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserAttribute<T> implements Condition<T> {
public class UserAttribute<T> extends LeafCondition<T> {
public static final String QUALIFIED = "qualified";

private static final Logger logger = LoggerFactory.getLogger(UserAttribute.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1840,4 +1840,40 @@ public void nullValueEvaluate() {
assertNull(nullValueAttribute.evaluate(null, OTUtils.user(Collections.singletonMap(attributeName, attributeValue))));
assertNull(nullValueAttribute.evaluate(null, OTUtils.user((Collections.singletonMap(attributeName, "")))));
}

@Test
public void getAllSegmentsFromAudience() {
Condition condition = createMultipleConditionAudienceAndOrODP();
Audience audience = new Audience("1", "testAudience", condition);
assertEquals(new HashSet<>(Arrays.asList("odp-segment-1", "odp-segment-2", "odp-segment-3", "odp-segment-4")), audience.getSegments());

// ["and", "1", "2"]
List<Condition> audience1And2 = new ArrayList<>();
audience1And2.add(new UserAttribute("odp.audiences", "third_party_dimension", "qualified", "odp-segment-1"));
audience1And2.add(new UserAttribute("odp.audiences", "third_party_dimension", "qualified", "odp-segment-2"));
AndCondition audienceCondition1 = new AndCondition(audience1And2);

// ["and", "3", "4"]
List<Condition> audience3And4 = new ArrayList<>();
audience3And4.add(new UserAttribute("odp.audiences", "third_party_dimension", "qualified", "odp-segment-3"));
audience3And4.add(new UserAttribute("odp.audiences", "third_party_dimension", "qualified", "odp-segment-4"));
AndCondition audienceCondition2 = new AndCondition(audience3And4);

// ["or", "5", "6"]
List<Condition> audience5And6 = new ArrayList<>();
audience5And6.add(new UserAttribute("odp.audiences", "third_party_dimension", "qualified", "odp-segment-5"));
audience5And6.add(new UserAttribute("odp.audiences", "third_party_dimension", "qualified", "odp-segment-6"));
OrCondition audienceCondition3 = new OrCondition(audience5And6);


//['or', '1', '2', '3']
List<Condition> conditions = new ArrayList<>();
conditions.add(audienceCondition1);
conditions.add(audienceCondition2);
conditions.add(audienceCondition3);

OrCondition implicitOr = new OrCondition(conditions);
audience = new Audience("1", "testAudience", implicitOr);
assertEquals(new HashSet<>(Arrays.asList("odp-segment-1", "odp-segment-2", "odp-segment-3", "odp-segment-4", "odp-segment-5", "odp-segment-6")), audience.getSegments());
}
}