Added two more MariaDB tables (Power & SMBIOS)

Ylian Saint-Hilaire committed Feb 2, 2020 at 13:37 UTC 01676c39c79fe800edc9586803406a23df3f6e59
1 file changed +17 -12
db.js
+17 -12
@@ -338,7 +338,12 @@ module.exports.CreateDB = function (parent, func) {
338 'CREATE INDEX ndxextraex ON meshcentral.main (extraex)',
339 'CREATE TABLE meshcentral.serverstats (time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(time), CHECK (json_valid(doc)))',
340 'CREATE INDEX ndxserverstattime ON meshcentral.serverstats (time)',
341 - 'CREATE INDEX ndxserverstatexpire ON meshcentral.serverstats (expire)'
341 + 'CREATE INDEX ndxserverstatexpire ON meshcentral.serverstats (expire)',
342 + 'CREATE TABLE meshcentral.power (id INT NOT NULL AUTO_INCREMENT, time DATETIME, nodeid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
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)'
347 ], function (err) { if (err != null) { console.log(err); } setupFunctions(func); });
348 });
349 } else if (parent.args.mongodb) {
@@ -536,13 +541,13 @@ module.exports.CreateDB = function (parent, func) {
541 obj.eventsfile.createIndex({ username: 1 }, { sparse: 1, name: 'Username1' });
542 obj.eventsfile.createIndex({ domain: 1, nodeid: 1, time: -1 }, { sparse: 1, name: 'DomainNodeTime1' });
543 obj.eventsfile.createIndex({ ids: 1, time: -1 }, { sparse: 1, name: 'IdsAndTime1' });
539 - obj.eventsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
544 + obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
545 });
546 } else if (indexesByName['ExpireTime1'].expireAfterSeconds != expireEventsSeconds) {
547 // Reset the timeout index
548 console.log("Resetting events expire index...");
549 obj.eventsfile.dropIndex('ExpireTime1', function (err) {
545 - obj.eventsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
550 + obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
551 });
552 }
553 });
@@ -767,17 +772,17 @@ module.exports.CreateDB = function (parent, func) {
772 obj.GetFailedLoginCount = function (username, domainid, lastlogin, func) { console.log('TODO:GetFailedLoginCount'); }
773
774 // Database actions on the power collection
770 - obj.getAllPower = function (func) { console.log('TODO:getAllPower'); };
771 - obj.storePowerEvent = function (event, multiServer, func) { console.log('TODO:storePowerEvent'); };
772 - obj.getPowerTimeline = function (nodeid, func) { console.log('TODO:getPowerTimeline'); };
773 - obj.removeAllPowerEvents = function () { console.log('TODO:removeAllPowerEvents'); };
774 - obj.removeAllPowerEventsForNode = function (nodeid) { console.log('TODO:removeAllPowerEventsForNode'); };
775 + obj.getAllPower = function (func) { mariaDbQuery('SELECT doc FROM meshcentral.power', null, func); };
776 + obj.storePowerEvent = function (event, multiServer, func) { if (multiServer != null) { event.server = multiServer.serverid; } mariaDbQuery('INSERT INTO meshcentral.power VALUE (?, ?, ?, ?)', [null, event.time, event.nodeid ? event.nodeid : null, JSON.stringify(event)], func); };
777 + obj.getPowerTimeline = function (nodeid, func) { mariaDbQuery('SELECT doc FROM meshcentral.power WHERE ((nodeid = ?) OR (nodeid = "*")) ORDER BY time', [nodeid], func); };
778 + obj.removeAllPowerEvents = function () { mariaDbQuery('DELETE FROM meshcentral.power', null, function (err, docs) { }); };
779 + obj.removeAllPowerEventsForNode = function (nodeid) { mariaDbQuery('DELETE FROM meshcentral.power WHERE nodeid = ?', [nodeid], function (err, docs) { }); };
780
781 // Database actions on the SMBIOS collection
777 - obj.GetAllSMBIOS = function (func) { console.log('TODO:GetAllSMBIOS'); };
778 - obj.SetSMBIOS = function (smbios, func) { console.log('TODO:SetSMBIOS'); };
779 - obj.RemoveSMBIOS = function (id) { console.log('TODO:RemoveSMBIOS'); };
780 - obj.GetSMBIOS = function (id, func) { console.log('TODO:GetSMBIOS'); };
782 + obj.GetAllSMBIOS = function (func) { mariaDbQuery('SELECT doc FROM meshcentral.smbios', null, func); };
783 + obj.SetSMBIOS = function (smbios, func) { var expire = new Date(smbios.time); expire.setMonth(expire.getMonth() + 6); mariaDbQuery('REPLACE INTO meshcentral.smbios VALUE (?, ?, ?, ?)', [smbios._id, smbios.time, expire, JSON.stringify(smbios)], func); };
784 + obj.RemoveSMBIOS = function (id) { mariaDbQuery('DELETE FROM meshcentral.smbios WHERE id = ?', [id], function (err, docs) { }); };
785 + obj.GetSMBIOS = function (id, func) { mariaDbQuery('SELECT doc FROM meshcentral.smbios WHERE id = ?', [id], func); };
786
787 // Database actions on the Server Stats collection
788 obj.SetServerStats = function (data, func) { mariaDbQuery('REPLACE INTO meshcentral.serverstats VALUE (?, ?, ?)', [data.time, data.expire, JSON.stringify(data)], func); };