Skip to content

Commit

Permalink
feat: Change fractional custom op from percentage-based to relative w…
Browse files Browse the repository at this point in the history
…eighting. #828

Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>
  • Loading branch information
aepfli committed Jun 19, 2024
1 parent cf77d56 commit 68f77f8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,16 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx

final List<FractionProperty> propertyList = new ArrayList<>();

double distribution = 0;
try {
for (Object dist : distibutions) {
FractionProperty fractionProperty = new FractionProperty(dist);
propertyList.add(fractionProperty);
distribution += fractionProperty.getPercentage();
}
} catch (JsonLogicException e) {
log.debug("Error parsing fractional targeting rule", e);
return null;
}

if (distribution != 100) {
log.debug("Fractional properties do not sum to 100");
return null;
}

// find distribution
return distributeValue(bucketBy, propertyList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void targetingBackedFractional() throws JsonLogicEvaluationException {


@Test
void invalidRuleSumNot100() throws JsonLogicEvaluationException {
void invalidRuleSumGreater100() throws JsonLogicEvaluationException {
// given
Fractional fractional = new Fractional();

Expand Down Expand Up @@ -189,7 +189,49 @@ void invalidRuleSumNot100() throws JsonLogicEvaluationException {
Object evaluate = fractional.evaluate(rule, data);

// then
assertNull(evaluate);
assertEquals("blue", evaluate);
}

@Test
void invalidRuleSumlower100() throws JsonLogicEvaluationException {
// given
Fractional fractional = new Fractional();

/* Rule
* [
* [
* "blue",
* 50
* ],
* [
* "green",
* 30
* ]
* ]
* */

final List<Object> rule = new ArrayList<>();

final List<Object> bucket1 = new ArrayList<>();
bucket1.add("blue");
bucket1.add(50);

final List<Object> bucket2 = new ArrayList<>();
bucket2.add("green");
bucket2.add(70);

rule.add(bucket1);
rule.add(bucket2);

Map<String, String> data = new HashMap<>();
data.put(FLAG_KEY, "headerColor");
data.put(TARGET_KEY, "foo@foo.com");

// when
Object evaluate = fractional.evaluate(rule, data);

// then
assertEquals("blue", evaluate);
}

@Test
Expand Down Expand Up @@ -266,4 +308,4 @@ void invalidRule() throws JsonLogicEvaluationException {
assertNull(evaluate);
}

}
}

0 comments on commit 68f77f8

Please sign in to comment.