Skip to content

Commit

Permalink
Handle out of bound history entries + fix last known entry (#251)
Browse files Browse the repository at this point in the history
* Make sure values out of bound are inserted into res[0] (caused by Math.abs)

* Only keep the latest out of bound entry
  • Loading branch information
kalkih authored Jan 25, 2020
1 parent fbf1960 commit d13a0df
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export default class Graph {

const coords = this._history.reduce((res, item) => this._reducer(res, item), []);

// drop potential out of bound entry's except one
if (coords[0] && coords[0].length) coords[0] = [coords[0][coords[0].length - 1]];

// extend length to fill missing history
const requiredNumOfPoints = Math.ceil(this.hours * this.points);
coords.length = requiredNumOfPoints;
Expand All @@ -60,7 +63,7 @@ export default class Graph {
_reducer(res, item) {
const age = this._endTime - new Date(item.last_changed).getTime();
const interval = (age / ONE_HOUR * this.points) - this.hours * this.points;
const key = Math.floor(Math.abs(interval));
const key = interval < 0 ? Math.floor(Math.abs(interval)) : 0;
if (!res[key]) res[key] = [];
res[key].push(item);
return res;
Expand Down

0 comments on commit d13a0df

Please sign in to comment.