MariaDB improvements.
Ylian Saint-Hilaire committed
Feb 2, 2020 at 15:31 UTC
ab57626a0347fb54f0f9dc059c9105361412bbc6
2 files changed
+26
-15
db.js
+25
-15
@@ -332,18 +332,28 @@ module.exports.CreateDB = function (parent, func) {
332
if (err == null) { setupFunctions(func); } else
333
mariaDbBatchExec([
334
'CREATE DATABASE meshcentral',
335
+ // Main table
336
'CREATE TABLE meshcentral.main (id VARCHAR(256) NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
337
'CREATE INDEX ndxtypedomainextra ON meshcentral.main (type, domain, extra)',
338
'CREATE INDEX ndxextra ON meshcentral.main (extra)',
339
'CREATE INDEX ndxextraex ON meshcentral.main (extraex)',
340
+ // Events table
341
+ 'CREATE TABLE meshcentral.events (id INT NOT NULL AUTO_INCREMENT, time DATETIME, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
342
+ 'CREATE INDEX ndxeventstime ON meshcentral.events (time)',
343
+ 'CREATE INDEX ndxeventsusername ON meshcentral.events (domain, userid, time)',
344
+ 'CREATE INDEX ndxeventsdomainnodeidtime ON meshcentral.events (domain, nodeid, time)',
345
+ // Server stats table
346
'CREATE TABLE meshcentral.serverstats (time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(time), CHECK (json_valid(doc)))',
347
'CREATE INDEX ndxserverstattime ON meshcentral.serverstats (time)',
348
'CREATE INDEX ndxserverstatexpire ON meshcentral.serverstats (expire)',
349
+ // Power events table
350
'CREATE TABLE meshcentral.power (id INT NOT NULL AUTO_INCREMENT, time DATETIME, nodeid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
351
'CREATE INDEX ndxpowernodeidtime ON meshcentral.power (nodeid, time)',
352
+ // SMBIOS table
353
'CREATE TABLE meshcentral.smbios (id CHAR(255), time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
354
'CREATE INDEX ndxsmbiostime ON meshcentral.smbios (time)',
355
'CREATE INDEX ndxsmbiosexpire ON meshcentral.smbios (expire)',
356
+ // Plugins table
357
'CREATE TABLE meshcentral.plugin (id INT NOT NULL AUTO_INCREMENT, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))'
358
], function (err) { if (err != null) { console.log(err); } setupFunctions(func); });
359
});
@@ -438,13 +448,13 @@ module.exports.CreateDB = function (parent, func) {
448
obj.eventsfile.createIndex({ username: 1 }, { sparse: 1, name: 'Username1' });
449
obj.eventsfile.createIndex({ domain: 1, nodeid: 1, time: -1 }, { sparse: 1, name: 'DomainNodeTime1' });
450
obj.eventsfile.createIndex({ ids: 1, time: -1 }, { sparse: 1, name: 'IdsAndTime1' });
441
- obj.eventsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
451
+ obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
452
});
453
} else if (indexesByName['ExpireTime1'].expireAfterSeconds != expireEventsSeconds) {
454
// Reset the timeout index
455
console.log("Resetting events expire index...");
456
obj.eventsfile.dropIndex('ExpireTime1', function (err) {
447
- obj.eventsfile.createIndex({ 'time': 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
457
+ obj.eventsfile.createIndex({ time: 1 }, { expireAfterSeconds: expireEventsSeconds, name: 'ExpireTime1' });
458
});
459
}
460
});
@@ -759,23 +769,23 @@ module.exports.CreateDB = function (parent, func) {
769
obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { mariaDbExec('SELECT COUNT(id) FROM meshcentral.main WHERE domain = ? AND type = ?', [domainid, type], function (err, response) { func((response['COUNT(id)'] == null) || (response['COUNT(id)'] > max), response['COUNT(id)']) }); } }
770
771
// Database actions on the events collection
762
- obj.GetAllEvents = function (func) { console.log('TODO:GetAllEvents'); };
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'); };
767
- obj.GetUserEventsWithLimit = function (ids, domain, username, limit, func) { console.log('TODO:GetUserEventsWithLimit'); };
768
- obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { console.log('TODO:GetNodeEventsWithLimit'); };
769
- obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { console.log('TODO:GetNodeEventsSelfWithLimit'); };
770
- obj.RemoveAllEvents = function (domain) { console.log('TODO:RemoveAllEvents'); };
771
- obj.RemoveAllNodeEvents = function (domain, nodeid) { console.log('TODO:RemoveAllNodeEvents'); };
772
- obj.RemoveAllUserEvents = function (domain, userid) { console.log('TODO:RemoveAllUserEvents'); };
773
- obj.GetFailedLoginCount = function (username, domainid, lastlogin, func) { console.log('TODO:GetFailedLoginCount'); }
772
+ obj.GetAllEvents = function (func) { mariaDbQuery('SELECT doc FROM meshcentral.events', null, func); };
773
+ obj.StoreEvent = function (event) { mariaDbQuery('INSERT INTO meshcentral.events VALUE (?, ?, ?, ?, ?, ?, ?)', [null, event.time, ((typeof event.domain == 'string') ? event.domain : null), event.action, event.nodeid ? event.nodeid : null, event.userid ? event.userid : null, JSON.stringify(event)], function (err, docs) { }); };
774
+ obj.GetEvents = function (ids, domain, func) { console.log('TODO:GetEvents'); func(null, []); };
775
+ obj.GetEventsWithLimit = function (ids, domain, limit, func) { console.log('TODO:GetEventsWithLimit'); func(null, []); };
776
+ obj.GetUserEvents = function (ids, domain, username, func) { console.log('TODO:GetUserEvents'); func(null, []); };
777
+ obj.GetUserEventsWithLimit = function (ids, domain, username, limit, func) { console.log('TODO:GetUserEventsWithLimit'); func(null, []); };
778
+ obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { mariaDbQuery('SELECT doc FROM meshcentral.events WHERE (nodeid = ?) AND (domain = ?) ORDER BY time DESC LIMIT ?', [nodeid, domain, limit], func); };
779
+ obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { mariaDbQuery('SELECT doc FROM meshcentral.events WHERE (nodeid = ?) AND (domain = ?) AND ((userid = ?) OR (userid IS NULL)) ORDER BY time DESC LIMIT ?', [nodeid, domain, userid, limit], func); };
780
+ obj.RemoveAllEvents = function (domain) { mariaDbQuery('DELETE FROM meshcentral.events', null, function (err, docs) { }); };
781
+ obj.RemoveAllNodeEvents = function (domain, nodeid) { mariaDbQuery('DELETE FROM meshcentral.events WHERE domain = ? AND nodeid = ?', [domain, nodeid], function (err, docs) { }); };
782
+ obj.RemoveAllUserEvents = function (domain, userid) { mariaDbQuery('DELETE FROM meshcentral.events WHERE domain = ? AND userid = ?', [domain, userid], function (err, docs) { }); };
783
+ obj.GetFailedLoginCount = function (username, domainid, lastlogin, func) { mariaDbExec('SELECT COUNT(id) FROM meshcentral.events WHERE action = "authfail" AND domain = ? AND userid = ? AND time > ?', [domainid, 'user/' + domainid + '/' + username.toLowerCase(), lastlogin], function (err, response) { obj.stats.meshcentral = response['COUNT(id)']; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }); }
784
785
// Database actions on the power collection
786
obj.getAllPower = function (func) { mariaDbQuery('SELECT doc FROM meshcentral.power', null, func); };
787
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); };
778
- obj.getPowerTimeline = function (nodeid, func) { mariaDbQuery('SELECT doc FROM meshcentral.power WHERE ((nodeid = ?) OR (nodeid = "*")) ORDER BY time', [nodeid], func); };
788
+ obj.getPowerTimeline = function (nodeid, func) { mariaDbQuery('SELECT doc FROM meshcentral.power WHERE ((nodeid = ?) OR (nodeid = "*")) ORDER BY time DESC', [nodeid], func); };
789
obj.removeAllPowerEvents = function () { mariaDbQuery('DELETE FROM meshcentral.power', null, function (err, docs) { }); };
790
obj.removeAllPowerEventsForNode = function (nodeid) { mariaDbQuery('DELETE FROM meshcentral.power WHERE nodeid = ?', [nodeid], function (err, docs) { }); };
791
views/default.handlebars
+1
@@ -4980,6 +4980,7 @@
4980
function writeDeviceEvent(nodeid) {
4981
if (xxdialogMode) return;
4982
setDialogMode(2, "Add Device Event", 3, writeDeviceEventEx, '<textarea id=d2devEvent style=background-color:#fcf3cf;width:100%;height:200px;resize:none;overflow-y:scroll></textarea><span style=font-size:10px>' + "This will add an entry to this device\'s event log." + '<span>', nodeid);
4983
+ Q('d2devEvent').focus();
4984
}
4985
4986
function writeDeviceEventEx(buttons, tag) { meshserver.send({ action: 'setDeviceEvent', nodeid: decodeURIComponent(tag), msg: encodeURIComponent(Q('d2devEvent').value) }); }