Skip to content
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

[basicprofiles] Fix division-by-zero error in $DELTA_PERCENT state filter #18089

Merged
merged 5 commits into from
Feb 9, 2025
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 @@ -103,7 +103,7 @@ public class StateFilterProfile implements StateProfile {
private @Nullable Item linkedItem = null;

private State newState = UnDefType.UNDEF;
private State acceptedState = UnDefType.UNDEF;
private Optional<State> acceptedState = Optional.empty();
private LinkedList<State> previousStates = new LinkedList<>();

private final int windowSize;
Expand Down Expand Up @@ -230,7 +230,7 @@ private State checkCondition(State state) {
}

if (conditions.stream().allMatch(c -> c.check(state))) {
acceptedState = state;
acceptedState = Optional.of(state);
return state;
} else {
return configMismatchState;
Expand Down Expand Up @@ -339,8 +339,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 @@ -378,8 +377,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 @@ -567,6 +565,30 @@ public FunctionType(Function type, Optional<Integer> windowSize) {
};
}

/**
* If the profile uses the DELTA or DELTA_PERCENT functions, the new state value will always be accepted if the
* 'acceptedState' (prior state) has not yet been initialised, or -- in the case of the DELTA_PERCENT function
* only -- if 'acceptedState' has a zero value. This ensures that 'acceptedState' is always initialised. And it
* also ensures that the DELTA_PERCENT function cannot cause a divide by zero error.
*
* @return true if the new state value shall be accepted
*/
public boolean alwaysAccept() {
if ((type == Function.DELTA || type == Function.DELTA_PERCENT) && acceptedState.isEmpty()) {
return true;
}
if (type == Function.DELTA_PERCENT) {
// avoid division by zero
if (acceptedState.get() instanceof QuantityType base) {
return base.toBigDecimal().compareTo(BigDecimal.ZERO) == 0;
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
}
if (acceptedState.get() 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 Expand Up @@ -692,27 +714,33 @@ public String toString() {
}

private @Nullable State calculateDelta() {
if (acceptedState.isEmpty()) {
return null;
}
if (newState instanceof QuantityType newStateQuantity) {
QuantityType result = newStateQuantity.subtract((QuantityType) acceptedState);
QuantityType result = newStateQuantity.subtract((QuantityType) acceptedState.get());
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
return result.toBigDecimal().compareTo(BigDecimal.ZERO) < 0 ? result.negate() : result;
}
BigDecimal result = ((DecimalType) newState).toBigDecimal()
.subtract(((DecimalType) acceptedState).toBigDecimal()) //
.subtract(((DecimalType) acceptedState.get()).toBigDecimal()) //
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
.abs();
return new DecimalType(result);
}

private @Nullable State calculateDeltaPercent() {
if (acceptedState.isEmpty()) {
return null;
}
State calculatedDelta = calculateDelta();
BigDecimal bdDelta;
BigDecimal bdBase;
if (acceptedState instanceof QuantityType acceptedStateQuantity) {
if (acceptedState.get() instanceof QuantityType acceptedStateQuantity) {
// Assume that delta and base are in the same unit
bdDelta = ((QuantityType) calculatedDelta).toBigDecimal();
bdBase = acceptedStateQuantity.toBigDecimal();
} else {
bdDelta = ((DecimalType) calculatedDelta).toBigDecimal();
bdBase = ((DecimalType) acceptedState).toBigDecimal();
bdBase = ((DecimalType) acceptedState.get()).toBigDecimal();
}
bdBase = bdBase.abs();
BigDecimal percent = bdDelta.multiply(BigDecimal.valueOf(100)).divide(bdBase, 2, RoundingMode.HALF_EVEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,17 @@ public static Stream<Arguments> testFunctions() {
Arguments.of(decimalItem, "$DELTA_PERCENT < 10", decimals, DecimalType.valueOf("0.89"), false), //

Arguments.of(decimalItem, "$DELTA_PERCENT < 10", negativeDecimals, DecimalType.valueOf("0"), false),
//
Arguments.of(decimalItem, "10 > $DELTA_PERCENT", negativeDecimals, DecimalType.valueOf("0"), false),
//

Arguments.of(decimalItem, "< 10%", decimals, DecimalType.valueOf("1.09"), true), //
Arguments.of(decimalItem, "< 10%", decimals, DecimalType.valueOf("1.11"), false), //
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), //

// 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