first, delta, range passing unit tests

Signed-off-by: Dirk le Roux <dirk.leroux@gmail.com>
This commit is contained in:
Dirk le Roux
2016-11-29 08:26:31 +02:00
committed by Dirk le Roux
parent f7e8e9c9af
commit 49dda7e5c1
3 changed files with 84 additions and 1 deletions
+26
View File
@@ -102,6 +102,9 @@ export default class TimeSeries {
this.stats.min = Number.MAX_VALUE;
this.stats.avg = null;
this.stats.current = null;
this.stats.first = null;
this.stats.delta = 0;
this.stats.range = null;
this.stats.timeStep = Number.MAX_VALUE;
this.allIsNull = true;
this.allIsZero = true;
@@ -112,6 +115,8 @@ export default class TimeSeries {
var currentValue;
var nonNulls = 0;
var previousTime;
var previousValue = 0;
var previousDeltaUp = true;
for (var i = 0; i < this.datapoints.length; i++) {
currentValue = this.datapoints[i][0];
@@ -148,6 +153,24 @@ export default class TimeSeries {
if (currentValue < this.stats.min) {
this.stats.min = currentValue;
}
if (this.stats.first === null){
this.stats.first = currentValue;
}else{
if (previousValue > currentValue) { // counter reset
previousDeltaUp = false;
if (i === this.datapoints.length-1) { // reset on last
this.stats.delta += currentValue;
}
}else{
if (previousDeltaUp) {
this.stats.delta += currentValue - previousValue; // normal increment
} else {
this.stats.delta += currentValue; // account for counter reset
}
previousDeltaUp = true;
}
}
previousValue = currentValue;
}
if (currentValue !== 0) {
@@ -167,6 +190,9 @@ export default class TimeSeries {
this.stats.current = result[result.length-2][1];
}
}
if (this.stats.max !== null && this.stats.min !== null) {
this.stats.range = this.stats.max - this.stats.min;
}
this.stats.count = result.length;
return result;