Skip to content

Commit

Permalink
fix: drop out-of-bound coords in reducer (#881)
Browse files Browse the repository at this point in the history
The fix from #251 introduced a logic that
1. allocated all "too-old" coords to the first group coords[0]
2. deleted all but one entries from that first group.

This caused aggregators to stop calculating proper results for that first group.

The new logic drops out-of-bounds entries already in the reducer.
This allows the first group to have multiple entries.
Aggregate functions now work again.

Also clean up reducer code with no funtional change.
  • Loading branch information
akloeckner committed Apr 22, 2023
1 parent 7576fe6 commit 527f005
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/graph.js
Expand Up @@ -54,11 +54,6 @@ export default class Graph {

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

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

// extend length to fill missing history
const requiredNumOfPoints = Math.ceil(this.hours * this.points);
histGroups.length = requiredNumOfPoints;
Expand All @@ -71,29 +66,30 @@ 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 = interval < 0 ? Math.floor(Math.abs(interval)) : 0;
if (!res[key]) res[key] = [];
res[key].push(item);
if (interval < 0) {
const key = Math.floor(Math.abs(interval));
if (!res[key]) res[key] = [];
res[key].push(item);
}
return res;
}

_calcPoints(history) {
const coords = [];
let xRatio = this.width / (this.hours * this.points - 1);
xRatio = Number.isFinite(xRatio) ? xRatio : this.width;

const first = history.filter(Boolean)[0];
let last = [this._calcPoint(first), this._lastValue(first)];
const getCoords = (item, i) => {
const x = xRatio * i + this.margin[X];
if (item)
last = [this._calcPoint(item), this._lastValue(item)];
return coords.push([x, 0, item ? last[0] : last[1]]);
};

for (let i = 0; i < history.length; i += 1)
getCoords(history[i], i);

const coords = [];
let last = history.filter(Boolean)[0];
let x;
for (let i = 0; i < history.length; i += 1) {
x = xRatio * i + this.margin[X];
if (history[i]) {
last = history[i];
coords.push([x, 0, this._calcPoint(last)]);
} else {
coords.push([x, 0, this._lastValue(last)]);
}
}
return coords;
}

Expand Down

0 comments on commit 527f005

Please sign in to comment.