Always put value in first bucket if force is set

For a last function, the only values that matters is the last
one so always add it to the first window. This avoid the problem
where an OK value may be in a future window but the current window
has an older alarm value.

Change-Id: I20f8e91da266fdb917dd36ded60d1a5c608b74d0
Closes-Bug: #1618641
This commit is contained in:
Craig Bryant 2016-08-30 16:59:39 -06:00
parent 1ad8b00ae2
commit 06c23a1ade
2 changed files with 12 additions and 7 deletions

View File

@ -98,20 +98,21 @@ public class SlidingWindowStats {
/** /**
* Adds the {@code value} to the statistics for the slot associated with the {@code timestamp} and * Adds the {@code value} to the statistics for the slot associated with the {@code timestamp} and
* returns true, else returns false if the {@code timestamp} is outside of the window and * returns true, else returns false if the {@code timestamp} is outside of the window and
* {@code force} is false. If {@code force} is true, always add value * {@code force} is false. If {@code force} is true, always add value to the first window
* *
* @param value to add * @param value to add
* @param timestamp to add value for * @param timestamp to add value for
* @param force if true, add value to first window even if timestamp is outside of all windows * @param force if true, add value to first window
* @return true if the value was added else false if it the {@code timestamp} was outside the * @return true if the value was added else false if it the {@code timestamp} was outside the
* window and force was false * window and force was false
*/ */
public boolean addValue(double value, long timestamp, boolean force) { public boolean addValue(double value, long timestamp, boolean force) {
int index = indexOfTime(timescale.adjust(timestamp)); final int index;
if (index == -1) { if (force) {
if (force) { index = 0;
index = 0; } else {
} else { index = indexOfTime(timescale.adjust(timestamp));
if (index == -1) {
return false; return false;
} }
} }

View File

@ -170,6 +170,8 @@ public class SlidingWindowStatsTest {
TimeResolution.ABSOLUTE, 3, 1, 2, 9); TimeResolution.ABSOLUTE, 3, 1, 2, 9);
window.addValue(999, 3, true); window.addValue(999, 3, true);
assertEquals(window.getViewValues(), new double[] { 999 }); assertEquals(window.getViewValues(), new double[] { 999 });
window.addValue(899, 10, true);
assertEquals(window.getViewValues(), new double[] { 899 });
} }
public void shouldNotAddOutOfWindowValueWithoutForce() { public void shouldNotAddOutOfWindowValueWithoutForce() {
@ -177,6 +179,8 @@ public class SlidingWindowStatsTest {
TimeResolution.ABSOLUTE, 3, 1, 2, 9); TimeResolution.ABSOLUTE, 3, 1, 2, 9);
window.addValue(999, 3, false); window.addValue(999, 3, false);
assertEquals(window.getViewValues(), new double[] { Double.NaN }); assertEquals(window.getViewValues(), new double[] { Double.NaN });
window.addValue(899, 10, false);
assertEquals(window.getViewValues(), new double[] { Double.NaN });
} }
public void shouldIgnoreOutOfOrderValue() { public void shouldIgnoreOutOfOrderValue() {