Skip to content

Commit

Permalink
* (Apollon77) Fix crash cases reported by Sentry
Browse files Browse the repository at this point in the history
* sync aggregate.js
  • Loading branch information
Apollon77 committed Aug 12, 2022
1 parent 3c9ecaf commit 0fb3aa0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ This adapter saves state history in a two-staged process.
-->

## Changelog

### __WORK IN PROGRESS__
* (Apollon77) Fix crash cases reported by Sentry

### 2.2.0 (2022-07-22)
* (Apollon77) make sure getHistory works for all cases
* (Bluefox/Apollon77) Add option to add comment and user info to results
Expand Down
2 changes: 1 addition & 1 deletion converter/history2db.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function processFile() {

//console.log(' File ' + j +': ' + id + ' --> ' + fileData.length);

if (fileData && fileData[fileData.length - 1].ts >= earliestDBValue[id]) {
if (fileData && fileData.length > 1 && fileData[fileData.length - 1].ts >= earliestDBValue[id]) {
let k;
for (k = 0; k < fileData.length; k++) {
if (fileData[k].ts >= earliestDBValue[id]) break;
Expand Down
16 changes: 9 additions & 7 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,29 +860,31 @@ function sendResponseCounter(adapter, msg, options, data, startTime) {
data.unshift({ts: options.start, val, i: true});
}

if (data[data.length - 2].ts === options.end) {
if (data[data.length - 2] !== undefined && data[data.length - 2].ts === options.end) {
data.length--;
}

const veryLast = data[data.length - 1];
const beforeLast = data[data.length - 2];

// interpolate from end time to last
if (options.end < veryLast.ts) {
if (veryLast !== undefined && beforeLast !== undefined && options.end < veryLast.ts) {
const val = beforeLast.val + (veryLast.val - beforeLast.val) * ((options.end - beforeLast.ts) / (veryLast.ts - beforeLast.ts));
data.length--;
data.push({ts: options.end, val, i: true});
}

// at this point we expect [6.5, 9, 1, 4]
// at this point we expect [150, 200, 500, 0, 400, 0, 50]
let val = data[data.length - 1].val;
let sum = 0;
for (let i = data.length - 2; i >= 0; i--) {
if (data[i].val < val) {
sum += val - data[i].val;
if (data.length > 1) {
let val = data[data.length - 1].val;
for (let i = data.length - 2; i >= 0; i--) {
if (data[i].val < val) {
sum += val - data[i].val;
}
val = data[i].val;
}
val = data[i].val;
}

adapter.sendTo(msg.from, msg.command, {
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ function processMessage(msg) {
function processStartValues() {
if (tasksStart && tasksStart.length) {
const task = tasksStart.shift();
if (history[task.id][adapter.namespace].changesOnly) {
if (history[task.id] && history[task.id][adapter.namespace] && history[task.id][adapter.namespace].changesOnly) {
adapter.getForeignState(history[task.id].realId, (err, state) => {
const now = task.now || Date.now();
pushHistory(task.id, {
Expand Down

0 comments on commit 0fb3aa0

Please sign in to comment.