From 72d8a7d61e2ce5e3c9d7c4b55bdcbc9ab376c7ba Mon Sep 17 00:00:00 2001 From: Christian Amann Date: Thu, 11 Aug 2022 13:32:55 +0200 Subject: [PATCH] Add fixes: - Clear database entries that are older than one month - Reduce number of data entries for each graph to a max value of 1000 --- httpd.js | 34 ++++++++++++++++++++++++++++++++++ stats.js | 10 ++++++++++ 2 files changed, 44 insertions(+) diff --git a/httpd.js b/httpd.js index 8af10fe..cabc822 100644 --- a/httpd.js +++ b/httpd.js @@ -16,6 +16,34 @@ var isValidZoom = function(s) { return /^(hour|day|week|month)$/.test(s); }; +var thinOutRows = function(rows, chart, maxNum) { + if (rows.length <= maxNum) { + return rows; + } + var factor = Math.ceil(rows.length / maxNum); + + var res = []; + var index = 0; + while (index < rows.length - factor) { + var subArr = rows.slice(index, index + factor) + var stat = {}; + + stat['ts'] = subArr[0].ts; + stat[chart] = subArr[0][chart] ?? 0; + + for (var i = 1; i < factor; ++i) { + var checkValue = subArr[i][chart] ?? 0; + if (checkValue > stat[chart]) { + stat['ts'] = subArr[i].ts; + stat[chart] = checkValue; + } + } + res.push(stat) + index += factor; + } + return res; +} + var getMinDate = function(zoom) { var now = moment(); now.subtract(1, zoom + 's'); @@ -44,6 +72,9 @@ var processNextContainer = function(result, preResult, containers, j, minDate, c result[0].push(name ? name : id); db.all("SELECT ts, "+chart+" FROM stats WHERE id = ? AND ts >= ? ORDER BY ts ASC", id, minDate, function(err, rows) { var prev = 0; + + rows = thinOutRows(rows, chart, 1000); + for (var i=0; i= ? ORDER BY ts ASC", req.params.id, minDate, function(err, rows) { var json = [['Time', chart == 'cpu' ? 'CPU %' : 'Bytes']]; var prev = 0; + + rows = thinOutRows(rows, chart, 1000); + for (var i=0; i