Fixed PostgreSQL getDbStats(), #3487

Ylian Saint-Hilaire committed Jan 19, 2022 at 12:11 UTC c8ef09f94c07f07ecd3e5b4fc006292f6f031582
1 file changed +15 -6
db.js
+15 -6
@@ -423,7 +423,16 @@ module.exports.CreateDB = function (parent, func) {
423 // Get the number of records in the database for various types, this is the slow NeDB way.
424 // WARNING: This is a terrible query for database performance. Only do this when needed. This query will look at almost every document in the database.
425 obj.getStats = function (func) {
426 - if (obj.databaseType == 3) {
426 + if (obj.databaseType == 6) {
427 + // PostgreSQL
428 + // TODO
429 + } else if (obj.databaseType == 5) {
430 + // MySQL
431 + // TODO
432 + } else if (obj.databaseType == 4) {
433 + // MariaDB
434 + // TODO
435 + } else if (obj.databaseType == 3) {
436 // MongoDB
437 obj.file.aggregate([{ "$group": { _id: "$type", count: { $sum: 1 } } }]).toArray(function (err, docs) {
438 var counters = {}, totalCount = 0;
@@ -1260,7 +1269,7 @@ module.exports.CreateDB = function (parent, func) {
1269 obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
1270 obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND nodeid = $2', [domain, nodeid], function (err, docs) { }); };
1271 obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND userid = $2', [domain, userid], function (err, docs) { }); };
1263 - obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { sqlDbQuery('SELECT COUNT(*) FROM events WHERE action = \'authfail\' AND domain = $1 AND userid = $2 AND time > $3', [domainid, userid, lastlogin], function (err, response, raw) { func(err == null ? raw.rows[0].count : 0); }); }
1272 + obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) { sqlDbQuery('SELECT COUNT(*) FROM events WHERE action = \'authfail\' AND domain = $1 AND userid = $2 AND time > $3', [domainid, userid, lastlogin], function (err, response, raw) { func(err == null ? parseInt(raw.rows[0].count) : 0); }); }
1273
1274 // Database actions on the power collection
1275 obj.getAllPower = function (func) { sqlDbQuery('SELECT doc FROM power', null, func); };
@@ -1305,10 +1314,10 @@ module.exports.CreateDB = function (parent, func) {
1314 // Get database information (TODO: Complete this)
1315 obj.getDbStats = function (func) {
1316 obj.stats = { c: 4 };
1308 - sqlDbExec('SELECT COUNT(id) FROM main', null, function (err, response) { obj.stats.meshcentral = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1309 - sqlDbExec('SELECT COUNT(time) FROM serverstats', null, function (err, response) { obj.stats.serverstats = response['COUNT(time)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1310 - sqlDbExec('SELECT COUNT(id) FROM power', null, function (err, response) { obj.stats.power = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1311 - sqlDbExec('SELECT COUNT(id) FROM smbios', null, function (err, response) { obj.stats.smbios = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1317 + sqlDbQuery('SELECT COUNT(*) FROM main', null, function (err, response, raw) { obj.stats.meshcentral = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1318 + sqlDbQuery('SELECT COUNT(*) FROM serverstats', null, function (err, response, raw) { obj.stats.serverstats = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1319 + sqlDbQuery('SELECT COUNT(*) FROM power', null, function (err, response, raw) { obj.stats.power = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1320 + sqlDbQuery('SELECT COUNT(*) FROM smbios', null, function (err, response, raw) { obj.stats.smbios = (err == null ? parseInt(raw.rows[0].count) : 0); if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1321 }
1322
1323 // Plugin operations