This repository has been archived by the owner on May 7, 2020. It is now read-only.
Fix averageSince calculation #2405
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01e9ba1
Fix averageSince calculation
J-N-K 07b4465
next try to fix averageSince. This includes the new function averageS…
J-N-K 4969e2a
remove unwanted functions
J-N-K 5244ba8
fix persistence test compiling still does not work
J-N-K 5ddb15f
finally fix tests
J-N-K 86adc24
add missing import
J-N-K cd18c70
include reviewer requests
J-N-K File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,8 @@ private static String getDefaultServiceId() { | |
.warn("There is no default persistence service configured!"); | ||
} | ||
} else { | ||
LoggerFactory.getLogger(PersistenceExtensions.class).warn("PersistenceServiceRegistryImpl is not available!"); | ||
LoggerFactory.getLogger(PersistenceExtensions.class) | ||
.warn("PersistenceServiceRegistryImpl is not available!"); | ||
} | ||
return null; | ||
} | ||
|
@@ -376,6 +377,7 @@ public String getName() { | |
* | ||
* @param item the item to get the average state value for | ||
* @param timestamp the point in time from which to search for the average state value | ||
* @param includeCurrentValue include the current value in the calculation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line should be removed again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @return the average state values since <code>timestamp</code>, <code>null</code> if the default persistence | ||
* service is not available, or the state of the given <code>item</code> if no previous states could be | ||
* found or if the default persistence service does not refer to an available | ||
|
@@ -400,24 +402,51 @@ public static DecimalType averageSince(Item item, AbstractInstant timestamp, Str | |
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId); | ||
Iterator<HistoricItem> it = result.iterator(); | ||
|
||
DecimalType value = (DecimalType) item.getStateAs(DecimalType.class); | ||
if (value == null) { | ||
value = DecimalType.ZERO; | ||
} | ||
BigDecimal total = BigDecimal.ZERO; | ||
|
||
BigDecimal avgValue, timeSpan; | ||
DecimalType lastState = null, thisState = null; | ||
BigDecimal lastTimestamp = null, thisTimestamp = null; | ||
BigDecimal firstTimestamp = null; | ||
|
||
BigDecimal total = value.toBigDecimal(); | ||
int quantity = 1; | ||
while (it.hasNext()) { | ||
State state = it.next().getState(); | ||
HistoricItem thisItem = it.next(); | ||
State state = thisItem.getState(); | ||
|
||
if (state instanceof DecimalType) { | ||
value = (DecimalType) state; | ||
total = total.add(value.toBigDecimal()); | ||
quantity++; | ||
thisState = (DecimalType) state; | ||
thisTimestamp = BigDecimal.valueOf(thisItem.getTimestamp().getTime()); | ||
if (firstTimestamp == null) { | ||
firstTimestamp = thisTimestamp; | ||
} else { | ||
avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), | ||
MathContext.DECIMAL64); | ||
timeSpan = thisTimestamp.subtract(lastTimestamp); | ||
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64)); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove this empty line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
lastTimestamp = thisTimestamp; | ||
lastState = thisState; | ||
} | ||
} | ||
BigDecimal average = total.divide(BigDecimal.valueOf(quantity), MathContext.DECIMAL64); | ||
|
||
return new DecimalType(average); | ||
if (lastState != null) { | ||
thisState = (DecimalType) item.getStateAs(DecimalType.class); | ||
thisTimestamp = BigDecimal.valueOf((new DateTime()).getMillis()); | ||
avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), | ||
MathContext.DECIMAL64); | ||
timeSpan = thisTimestamp.subtract(lastTimestamp); | ||
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64)); | ||
} | ||
|
||
if (thisTimestamp != null) { | ||
timeSpan = thisTimestamp.subtract(firstTimestamp, MathContext.DECIMAL64); | ||
|
||
BigDecimal average = total.divide(timeSpan, MathContext.DECIMAL64); | ||
return new DecimalType(average); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this test stable enough? If the execution is slow, the average might be calculated with a much bigger value than recentInterval already and the results might differ, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have adjusted it to 0.01 which should be good enough even for every slow execution. In my tests the difference was always around 0.0001, so a factor of 100 better than the value I have set now.