From 8fa3698ca7b8f524859bc346c2d08237d9097477 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Thu, 26 May 2022 22:25:16 +0200 Subject: [PATCH] * (Apollon77) Fix several crash cases reported by Sentry --- README.md | 4 ++++ docs/en/README.md | 2 +- lib/aggregate.js | 5 +++++ main.js | 24 ++++++++++++++++++++++-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 12387b3..550fa1c 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ This adapter saves state history in a two-staged process. --> ## Changelog + +### __WORK IN PROGRESS__ +* (Apollon77) Fix several crash cases reported by Sentry + ### 2.0.1 (2022-05-11) * (Apollon77) BREAKING: Configuration is only working in the new Admin 5 UI! * (Apollon77) Did bigger adjustments to the recording logic and added a lot of new Features. Please refer to Changelog and Forum post for details. diff --git a/docs/en/README.md b/docs/en/README.md index a84ed13..f4750ea 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -78,7 +78,7 @@ Possible options: - **ignoreNull** - if null values should be included (false), replaced by last not null value (true) or replaced with 0 (0) - **removeBorderValues** - By default additional border values are returned to optimize charting. Set this option to true if this is not wanted (e.g. for script data processing) - **returnNewestEntries** - The returned data are always sorted by timestamp ascending. When using aggregate "none" and also providing "count" or "limit" this means that normally the oldest entries are returned (unless no start data is provided). Set this option to true to get the newest entries instead. -- **aggregate** - aggregate method: +- **aggregate** - aggregate method (Default: 'average'): - *minmax* - used special algorithm. Splice the whole time range in small intervals and find for every interval max, min, start and end values. - *max* - Splice the whole time range in small intervals and find for every interval max value and use it for this interval (nulls will be ignored). - *min* - Same as max, but take minimal value. diff --git a/lib/aggregate.js b/lib/aggregate.js index 86a5970..9760934 100644 --- a/lib/aggregate.js +++ b/lib/aggregate.js @@ -142,6 +142,11 @@ function aggregationLogic(data, index, options) { log = options.log || console.log; } + if (!options.result[index]) { + log(`${options.logId} Data index ${index} not initialized, ignore!`); + return; + } + if (options.aggregate !== 'minmax' && !options.result[index].val.ts) { options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step)); } diff --git a/main.js b/main.js index 060fadd..6f70f7b 100644 --- a/main.js +++ b/main.js @@ -963,7 +963,7 @@ function getOneCachedData(id, options, cache, addId) { continue; } const resEntry = Object.assign({}, res[i]); - if (resEntry.val !== null && isFinite(resEntry.val) && options.round) { + if (typeof resEntry.val === 'number' && isFinite(resEntry.val) && options.round) { resEntry.val = Math.round(resEntry.val * options.round) / options.round; } if (options.ack) { @@ -1053,7 +1053,7 @@ function getOneFileData(dayList, dayStart, dayEnd, id, options, data, addId) { _data[ii].ts *= 1000; } - if (_data[ii].val !== null && isFinite(_data[ii].val) && options.round) { + if (typeof _data[ii].val === 'number' && isFinite(_data[ii].val) && options.round) { _data[ii].val = Math.round(_data[ii].val * options.round) / options.round; } if (options.ack) { @@ -1180,6 +1180,26 @@ function getHistory(msg) { options.round = adapter.config.round; } + try { + if (options.start && typeof options.start !== 'number') { + options.start = new Date(options.start).getTime(); + } + } catch (err) { + return adapter.sendTo(msg.from, msg.command, { + error: 'Invalid call. Start date ' + JSON.stringify(options.start) + ' is not a valid date' + }, msg.callback); + } + + try { + if (options.end && typeof options.end !== 'number') { + options.end = new Date(options.end).getTime(); + } + } catch (err) { + return adapter.sendTo(msg.from, msg.command, { + error: 'Invalid call. End date ' + JSON.stringify(options.end) + ' is not a valid date' + }, msg.callback); + } + if (!options.start && options.count) { options.returnNewestEntries = true; }