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
Changes from 1 commit
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 @@ -566,15 +566,15 @@ public FunctionType(Function type, Optional<Integer> windowSize) {
}

public boolean alwaysAccept() {
if ((type == Function.DELTA || type == Function.DELTA_PERCENT) && acceptedState == UnDefType.UNDEF) {
if ((type == Function.DELTA || type == Function.DELTA_PERCENT) && acceptedState.isEmpty()) {
return true;
}
if (type == Function.DELTA_PERCENT) {
// avoid division by zero
if (acceptedState instanceof QuantityType base) {
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 instanceof DecimalType base) {
if (acceptedState.get() instanceof DecimalType base) {
return base.toBigDecimal().compareTo(BigDecimal.ZERO) == 0;
}
}
Expand Down Expand Up @@ -707,11 +707,11 @@ public String toString() {

private @Nullable State calculateDelta() {
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);
}
Expand All @@ -720,13 +720,13 @@ public String toString() {
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