More MariaDB improvements.

Ylian Saint-Hilaire committed Feb 2, 2020 at 14:02 UTC 33202e9e1cc83fff083ac42dd334fa518201d9e2
1 file changed +21 -7
db.js
+21 -7
@@ -343,7 +343,8 @@ module.exports.CreateDB = function (parent, func) {
343 'CREATE INDEX ndxpowernodeidtime ON meshcentral.power (nodeid, time)',
344 'CREATE TABLE meshcentral.smbios (id CHAR(255), time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
345 'CREATE INDEX ndxsmbiostime ON meshcentral.smbios (time)',
346 - 'CREATE INDEX ndxsmbiosexpire ON meshcentral.smbios (expire)'
346 + 'CREATE INDEX ndxsmbiosexpire ON meshcentral.smbios (expire)',
347 + 'CREATE TABLE meshcentral.plugin (id INT NOT NULL AUTO_INCREMENT, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))'
348 ], function (err) { if (err != null) { console.log(err); } setupFunctions(func); });
349 });
350 } else if (parent.args.mongodb) {
@@ -759,7 +760,7 @@ module.exports.CreateDB = function (parent, func) {
760
761 // Database actions on the events collection
762 obj.GetAllEvents = function (func) { console.log('TODO:GetAllEvents'); };
762 - obj.StoreEvent = function (event) { console.log('TODO:StoreEvent'); };
763 + obj.StoreEvent = function (event) { console.log('TODO:StoreEvent', event); };
764 obj.GetEvents = function (ids, domain, func) { console.log('TODO:GetEvents'); };
765 obj.GetEventsWithLimit = function (ids, domain, limit, func) { console.log('TODO:GetEventsWithLimit'); };
766 obj.GetUserEvents = function (ids, domain, username, func) { console.log('TODO:GetUserEvents'); };
@@ -811,11 +812,24 @@ module.exports.CreateDB = function (parent, func) {
812 });
813 }
814
814 - // Get database information
815 - obj.getDbStats = function (func) { console.log('TODO:getDbStats'); }
815 + // Get database information (TODO: Complete this)
816 + obj.getDbStats = function (func) {
817 + obj.stats = { c: 4 };
818 + mariaDbExec('SELECT COUNT(id) FROM meshcentral.main', null, function (err, response) { obj.stats.meshcentral = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
819 + mariaDbExec('SELECT COUNT(time) FROM meshcentral.serverstats', null, function (err, response) { obj.stats.serverstats = response['COUNT(time)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
820 + mariaDbExec('SELECT COUNT(id) FROM meshcentral.power', null, function (err, response) { obj.stats.power = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
821 + mariaDbExec('SELECT COUNT(id) FROM meshcentral.smbios', null, function (err, response) { obj.stats.smbios = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
822 + }
823
824 // Plugin operations
818 - //if (parent.config.settings.plugins != null) {}
825 + if (parent.config.settings.plugins != null) {
826 + obj.addPlugin = function (plugin, func) { mariaDbQuery('INSERT INTO meshcentral.plugin VALUE (?, ?)', [null, JSON.stringify(value)], func); }; // Add a plugin
827 + obj.getPlugins = function (func) { mariaDbQuery('SELECT doc FROM meshcentral.plugin', null, func); }; // Get all plugins
828 + obj.getPlugin = function (id, func) { mariaDbQuery('SELECT doc FROM meshcentral.plugin WHERE id = ?', [id], func); }; // Get plugin
829 + obj.deletePlugin = function (id, func) { mariaDbQuery('DELETE FROM meshcentral.plugin WHERE id = ?', [id], func); }; // Delete plugin
830 + obj.setPluginStatus = function (id, status, func) { obj.getPlugin(id, function (err, docs) { if (docs.length == 1) { docs[0].status = status; obj.updatePlugin(id, docs[0], func); } }); };
831 + obj.updatePlugin = function (id, args, func) { delete args._id; mariaDbQuery('REPLACE INTO meshcentral.plugin VALUE (?, ?)', [id, JSON.stringify(args)], func); };
832 + }
833 } else if (obj.databaseType == 3) {
834 // Database actions on the main collection (MongoDB)
835 obj.Set = function (data, func) { obj.file.replaceOne({ _id: data._id }, performTypedRecordEncrypt(data), { upsert: true }, func); };
@@ -940,8 +954,8 @@ module.exports.CreateDB = function (parent, func) {
954
955 // Plugin operations
956 if (parent.config.settings.plugins != null) {
943 - obj.addPlugin = function (plugin, func) { plugin.type = "plugin"; obj.pluginsfile.insertOne(plugin, func); }; // Add a plugin
944 - obj.getPlugins = function (func) { obj.pluginsfile.find({ "type": "plugin" }).project({ "type": 0 }).sort({ name: 1 }).toArray(func); }; // Get all plugins
957 + obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.pluginsfile.insertOne(plugin, func); }; // Add a plugin
958 + obj.getPlugins = function (func) { obj.pluginsfile.find({ type: 'plugin' }).project({ type: 0 }).sort({ name: 1 }).toArray(func); }; // Get all plugins
959 obj.getPlugin = function (id, func) { id = require('mongodb').ObjectID(id); obj.pluginsfile.find({ _id: id }).sort({ name: 1 }).toArray(func); }; // Get plugin
960 obj.deletePlugin = function (id, func) { id = require('mongodb').ObjectID(id); obj.pluginsfile.deleteOne({ _id: id }, func); }; // Delete plugin
961 obj.setPluginStatus = function (id, status, func) { id = require('mongodb').ObjectID(id); obj.pluginsfile.updateOne({ _id: id }, { $set: { status: status } }, func); };