Skip to content

Commit

Permalink
[basicprofiles] Fix division-by-zero error in $DELTA_PERCENT state fi…
Browse files Browse the repository at this point in the history
…lter

Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Jan 24, 2025
1 parent de951f5 commit 9eea794
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ public boolean check(State input) {
if (rhsState == null) {
rhsItem = getItemOrNull(rhsString);
} else if (rhsState instanceof FunctionType rhsFunction) {
if (acceptedState == UnDefType.UNDEF && (rhsFunction.getType() == FunctionType.Function.DELTA
|| rhsFunction.getType() == FunctionType.Function.DELTA_PERCENT)) {
if (rhsFunction.alwaysAccept()) {
return true;
}
rhsItem = getLinkedItem();
Expand Down Expand Up @@ -377,8 +376,7 @@ public boolean check(State input) {
}

if (lhsState instanceof FunctionType lhsFunction) {
if (acceptedState == UnDefType.UNDEF && (lhsFunction.getType() == FunctionType.Function.DELTA
|| lhsFunction.getType() == FunctionType.Function.DELTA_PERCENT)) {
if (lhsFunction.alwaysAccept()) {
return true;
}
lhsItem = getLinkedItem();
Expand Down Expand Up @@ -553,6 +551,22 @@ public FunctionType(Function type, Optional<Integer> windowSize) {
};
}

public boolean alwaysAccept() {
if ((type == Function.DELTA || type == Function.DELTA_PERCENT) && acceptedState == UnDefType.UNDEF) {
return true;
}
if (type == Function.DELTA_PERCENT) {
// avoid division by zero
if (acceptedState instanceof QuantityType base) {
return base.toBigDecimal().compareTo(BigDecimal.ZERO) == 0;
}
if (acceptedState instanceof DecimalType base) {
return base.toBigDecimal().compareTo(BigDecimal.ZERO) == 0;
}
}
return false;
}

@Override
public <T extends State> @Nullable T as(@Nullable Class<T> target) {
if (target == DecimalType.class || target == QuantityType.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,14 @@ public static Stream<Arguments> testFunctions() {
Arguments.of(decimalItem, "< 10%", decimals, DecimalType.valueOf("0.91"), true), //
Arguments.of(decimalItem, "< 10%", decimals, DecimalType.valueOf("0.89"), false), //

// Check against possible division-by-zero errors in $DELTA_PERCENT
Arguments.of(decimalItem, "> 10%", List.of(DecimalType.ZERO), DecimalType.valueOf("1"), true), //
Arguments.of(decimalItem, "< 10%", List.of(DecimalType.ZERO), DecimalType.valueOf("1"), true), //

// Check against possible division-by-zero errors in $DELTA_PERCENT
Arguments.of(decimalItem, "> 10%", List.of(DecimalType.ZERO), DecimalType.valueOf("1"), true), //
Arguments.of(decimalItem, "< 10%", List.of(DecimalType.ZERO), DecimalType.valueOf("1"), true), //

// Contrast a simple comparison against a Percent QuantityType vs delta percent check
Arguments.of(percentItem, "> 5%", percentQuantities, QuantityType.valueOf("5.1 %"), true), //
Arguments.of(percentItem, "$DELTA_PERCENT > 5", percentQuantities, QuantityType.valueOf("5.1 %"),
Expand Down

0 comments on commit 9eea794

Please sign in to comment.