From 53a96fb26b8835c196c7a02454ee664176aed9a6 Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Fri, 15 Sep 2023 14:38:17 +0100 Subject: [PATCH 1/2] fix-netodb-again --- db.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/db.js b/db.js index 61db535f..36e95351 100644 --- a/db.js +++ b/db.js @@ -1474,6 +1474,7 @@ module.exports.CreateDB = function (parent, func) { if ((err == null) && (docs[0].id)) { for (var i in event.ids) { if (event.ids[i] != '*') { + obj.pendingTransfer++; sqlDbQuery('INSERT INTO eventids VALUES ($1, $2)', [docs[0].id, event.ids[i]], function(){ if(func){ func(); } }); } } @@ -1889,7 +1890,14 @@ module.exports.CreateDB = function (parent, func) { obj.StoreEvent = function (event, func) { obj.dbCounters.eventsSet++; sqlDbQuery('INSERT INTO events VALUES (DEFAULT, $1, $2, $3, $4, $5, $6) RETURNING id', [event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, event], function (err, docs) { - if (docs.id) { for (var i in event.ids) { if (event.ids[i] != '*') { sqlDbQuery('INSERT INTO eventids VALUES ($1, $2)', [docs.id, event.ids[i]]); } } } + if (docs.id) { + for (var i in event.ids) { + if (event.ids[i] != '*') { + obj.pendingTransfer++; + sqlDbQuery('INSERT INTO eventids VALUES ($1, $2)', [docs.id, event.ids[i]], function(){ if(func){ func(); } }); + } + } + } }); }; obj.GetEvents = function (ids, domain, func) { @@ -3225,16 +3233,16 @@ module.exports.CreateDB = function (parent, func) { var eventRecordsTransferCount = 0; var powerRecordsTransferCount = 0; var statsRecordsTransferCount = 0; - var pendingTransfer = 0; + obj.pendingTransfer = 0; // Transfer the data from main database nedbfile.find({}, function (err, docs) { if ((err == null) && (docs.length > 0)) { performTypedRecordDecrypt(docs) for (var i in docs) { - pendingTransfer++; + obj.pendingTransfer++; normalRecordsTransferCount++; - obj.Set(common.unEscapeLinksFieldName(docs[i]), function () { pendingTransfer--; }); + obj.Set(common.unEscapeLinksFieldName(docs[i]), function () { obj.pendingTransfer--; }); } } @@ -3242,10 +3250,10 @@ module.exports.CreateDB = function (parent, func) { nedbeventsfile.find({}, function (err, docs) { if ((err == null) && (docs.length > 0)) { for (var i in docs) { - pendingTransfer++; + obj.pendingTransfer++; eventRecordsTransferCount++; - for (var b in docs[i].ids) { if (docs[i].ids[b] != '*') { pendingTransfer++; } } - obj.StoreEvent(docs[i], function () { pendingTransfer--; }); + for (var b in docs[i].ids) { if (docs[i].ids[b] != '*') { obj.pendingTransfer++; } } + obj.StoreEvent(docs[i], function () { obj.pendingTransfer--; }); } } @@ -3253,9 +3261,9 @@ module.exports.CreateDB = function (parent, func) { nedbpowerfile.find({}, function (err, docs) { if ((err == null) && (docs.length > 0)) { for (var i in docs) { - pendingTransfer++; + obj.pendingTransfer++; powerRecordsTransferCount++; - obj.storePowerEvent(docs[i], null, function () { pendingTransfer--; }); + obj.storePowerEvent(docs[i], null, function () { obj.pendingTransfer--; }); } } @@ -3263,15 +3271,15 @@ module.exports.CreateDB = function (parent, func) { nedbserverstatsfile.find({}, function (err, docs) { if ((err == null) && (docs.length > 0)) { for (var i in docs) { - pendingTransfer++; + obj.pendingTransfer++; statsRecordsTransferCount++; - obj.SetServerStats(docs[i], function () { pendingTransfer--; }); + obj.SetServerStats(docs[i], function () { obj.pendingTransfer--; }); } } // Only exit when all the records are stored. setInterval(function () { - if (pendingTransfer == 0) { func("Done. " + normalRecordsTransferCount + " record(s), " + eventRecordsTransferCount + " event(s), " + powerRecordsTransferCount + " power change(s), " + statsRecordsTransferCount + " stat(s)."); } + if (obj.pendingTransfer == 0) { func("Done. " + normalRecordsTransferCount + " record(s), " + eventRecordsTransferCount + " event(s), " + powerRecordsTransferCount + " power change(s), " + statsRecordsTransferCount + " stat(s)."); } }, 200) }); }); From bca4f1c2179ee87b5f3ca13106efcf77bc8b4bc0 Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Fri, 15 Sep 2023 14:41:41 +0100 Subject: [PATCH 2/2] remove duplicate pendingTransfer++ --- db.js | 1 - 1 file changed, 1 deletion(-) diff --git a/db.js b/db.js index 36e95351..5a0dc6e5 100644 --- a/db.js +++ b/db.js @@ -3252,7 +3252,6 @@ module.exports.CreateDB = function (parent, func) { for (var i in docs) { obj.pendingTransfer++; eventRecordsTransferCount++; - for (var b in docs[i].ids) { if (docs[i].ids[b] != '*') { obj.pendingTransfer++; } } obj.StoreEvent(docs[i], function () { obj.pendingTransfer--; }); } }