Fixes & improvements: * use `.child` instead of string concats on paths * remove unnecessary `limit(999999)` for queries with filters * use `.get` instead of `.query` to get all data * using `skip` and `limit` args of `GetAllTypeNoTypeFieldMeshFiltered` * use `==` instead of `in` operator in `RemoveSMBIOS` and `GetSMBIOS` * use `.count` insetad of `.get` in `getDbStats` * use `.get` instead of `.query` in `getPlugins`, custom sort function

Fixes & improvements: * use `.child` instead of string concats on paths * remove unnecessary `limit(999999)` for queries with filters * use `.get` instead of `.query` to get all data * using `skip` and `limit` args of `GetAllTypeNoTypeFieldMeshFiltered` * use `==` instead of `in` operator in `RemoveSMBIOS` and `GetSMBIOS` * use `.count` insetad of `.get` in `getDbStats` * use `.get` instead of `.query` in `getPlugins`, custom sort function

Ewout Stortenbeker committed Sep 16, 2022 at 22:38 UTC c745a5e31e1472fef6a0f35461b387f721a935f5
1 file changed +73 -48
db.js
+73 -48
@@ -1582,25 +1582,27 @@ module.exports.CreateDB = function (parent, func) {
1582 data = common.escapeLinksFieldNameEx(data);
1583 var xdata = performTypedRecordEncrypt(data);
1584 obj.dbCounters.fileSet++;
1585 - obj.file.ref('meshcentral/' + encodeURIComponent(xdata._id)).set(common.aceEscapeFieldNames(xdata)).then(function (ref) { if (func) { func(); } })
1585 + obj.file.ref('meshcentral').child(encodeURIComponent(xdata._id)).set(common.aceEscapeFieldNames(xdata)).then(function (ref) { if (func) { func(); } })
1586 };
1587 obj.Get = function (id, func) {
1588 - obj.file.ref('meshcentral/' + encodeURIComponent(id)).get(function (snapshot) {
1588 + obj.file.ref('meshcentral').child(encodeURIComponent(id)).get(function (snapshot) {
1589 if (snapshot.exists()) { func(null, performTypedRecordDecrypt([common.aceUnEscapeFieldNames(snapshot.val())])); } else { func(null, []); }
1590 });
1591 };
1592 obj.GetAll = function (func) {
1593 - obj.file.query('meshcentral').get(function (snapshots) {
1594 - const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, common.aceUnEscapeAllFieldNames(docs));
1593 + obj.file.ref('meshcentral').get(function(snapshot) {
1594 + const val = snapshot.val();
1595 + const docs = Object.keys(val).map(function(key) { return val[key]; });
1596 + func(null, common.aceUnEscapeAllFieldNames(docs));
1597 });
1598 };
1599 obj.GetHash = function (id, func) {
1598 - obj.file.ref('meshcentral/' + encodeURIComponent(id)).get({ include: ['hash'] }, function (snapshot) {
1600 + obj.file.ref('meshcentral').child(encodeURIComponent(id)).get({ include: ['hash'] }, function (snapshot) {
1601 if (snapshot.exists()) { func(null, snapshot.val()); } else { func(null, null); }
1602 });
1603 };
1604 obj.GetAllTypeNoTypeField = function (type, domain, func) {
1603 - obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domain).get({ exclude: ['type'] }, function (snapshots) {
1605 + obj.file.query('meshcentral').filter('type', '==', type).filter('domain', '==', domain).get({ exclude: ['type'] }, function (snapshots) {
1606 const docs = [];
1607 for (var i in snapshots) { const x = snapshots[i].val(); docs.push(x); }
1608 func(null, common.aceUnEscapeAllFieldNames(docs));
@@ -1608,7 +1610,7 @@ module.exports.CreateDB = function (parent, func) {
1610 }
1611 obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, extrasids, domain, type, id, skip, limit, func) {
1612 if (meshes.length == 0) { func(null, []); return; }
1611 - var query = obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domain);
1613 + var query = obj.file.query('meshcentral').skip(skip).take(limit).filter('type', '==', type).filter('domain', '==', domain);
1614 if (id) { query = query.filter('_id', '==', id); }
1615 if (extrasids == null) {
1616 query = query.filter('meshid', 'in', meshes);
@@ -1623,34 +1625,40 @@ module.exports.CreateDB = function (parent, func) {
1625 }
1626 };
1627 obj.GetAllTypeNodeFiltered = function (nodes, domain, type, id, func) {
1626 - var query = obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domain).filter('nodeid', 'in', nodes);
1628 + var query = obj.file.query('meshcentral').filter('type', '==', type).filter('domain', '==', domain).filter('nodeid', 'in', nodes);
1629 if (id) { query = query.filter('_id', '==', id); }
1630 query.get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); });
1631 };
1632 obj.GetAllType = function (type, func) {
1631 - obj.file.query('meshcentral').take(999999).filter('type', '==', type).get(function (snapshots) {
1633 + obj.file.query('meshcentral').filter('type', '==', type).get(function (snapshots) {
1634 const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); }
1635 func(null, common.aceUnEscapeAllFieldNames(performTypedRecordDecrypt(docs)));
1636 });
1637 };
1636 - obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.query('meshcentral').take(999999).filter('_id', 'in', ids).filter('domain', '==', domain).filter('type', '==', type).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1637 - obj.GetUserWithEmail = function (domain, email, func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1638 - obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).filter('emailVerified', '==', true).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1639 - obj.Remove = function (id, func) { obj.file.ref('meshcentral/' + encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); };
1638 + obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.query('meshcentral').filter('_id', 'in', ids).filter('domain', '==', domain).filter('type', '==', type).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1639 + obj.GetUserWithEmail = function (domain, email, func) { obj.file.query('meshcentral').filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1640 + obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.query('meshcentral').filter('type', '==', 'user').filter('domain', '==', domain).filter('email', '==', email).filter('emailVerified', '==', true).get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1641 + obj.Remove = function (id, func) { obj.file.ref('meshcentral').child(encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); };
1642 obj.RemoveAll = function (func) { obj.file.query('meshcentral').remove().then(function () { if (func) { func(); } }); };
1643 obj.RemoveAllOfType = function (type, func) { obj.file.query('meshcentral').filter('type', '==', type).remove().then(function () { if (func) { func(); } }); };
1642 - obj.InsertMany = function (data, func) { var r = {}; for (var i in data) { const ref = obj.file.ref('meshcentral/' + encodeURIComponent(data[i]._id)); r[ref.key] = common.aceEscapeFieldNames(data[i]); } obj.file.ref('meshcentral').set(r).then(function (ref) { func(); }); }; // Insert records directly, no link escaping
1643 - obj.RemoveMeshDocuments = function (id) { obj.file.query('meshcentral').filter('meshid', '==', id).remove(); obj.file.ref('meshcentral/' + encodeURIComponent('nt' + id)).remove(); };
1644 + obj.InsertMany = function (data, func) { var r = {}; for (var i in data) { const ref = obj.file.ref('meshcentral').child(encodeURIComponent(data[i]._id)); r[ref.key] = common.aceEscapeFieldNames(data[i]); } obj.file.ref('meshcentral').set(r).then(function (ref) { func(); }); }; // Insert records directly, no link escaping
1645 + obj.RemoveMeshDocuments = function (id) { obj.file.query('meshcentral').filter('meshid', '==', id).remove(); obj.file.ref('meshcentral').child(encodeURIComponent('nt' + id)).remove(); };
1646 obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if ((err == null) && (docs.length == 1)) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
1647 obj.DeleteDomain = function (domain, func) { obj.file.query('meshcentral').filter('domain', '==', domain).remove().then(function () { if (func) { func(); } }); };
1648 obj.SetUser = function (user) { if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
1649 obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
1648 - obj.getLocalAmtNodes = function (func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'node').filter('host', 'exists').filter('host', '!=', null).filter('intelamt', 'exists').get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1649 - obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.query('meshcentral').take(999999).filter('type', '==', 'node').filter('domain', '==', domainid).filter('mtype', '!=', mtype).filter('intelamt.uuid', '==', uuid).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1650 - obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.query('meshcentral').take(999999).filter('type', '==', type).filter('domain', '==', domainid).get({ snapshots: false }, function (snapshots) { func((snapshots.length > max), snapshots.length); }); } }
1650 + obj.getLocalAmtNodes = function (func) { obj.file.query('meshcentral').filter('type', '==', 'node').filter('host', 'exists').filter('host', '!=', null).filter('intelamt', 'exists').get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1651 + obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.query('meshcentral').filter('type', '==', 'node').filter('domain', '==', domainid).filter('mtype', '!=', mtype).filter('intelamt.uuid', '==', uuid).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, performTypedRecordDecrypt(docs)); }); };
1652 + obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.query('meshcentral').filter('type', '==', type).filter('domain', '==', domainid).get({ snapshots: false }, function (snapshots) { func((snapshots.length > max), snapshots.length); }); } }
1653
1654 // Database actions on the events collection
1653 - obj.GetAllEvents = function (func) { obj.file.query('events').take(999999).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); };
1655 + obj.GetAllEvents = function (func) {
1656 + obj.file.ref('events').get(function (snapshot) {
1657 + const val = snapshot.val();
1658 + const docs = Object.keys(val).map(function(key) { return val[key]; });
1659 + func(null, docs);
1660 + })
1661 + };
1662 obj.StoreEvent = function (event, func) {
1663 if (typeof event.account == 'object') { event = Object.assign({}, event); event.account = common.aceEscapeFieldNames(event.account); }
1664 obj.dbCounters.eventsSet++;
@@ -1658,7 +1666,7 @@ module.exports.CreateDB = function (parent, func) {
1666 };
1667 obj.GetEvents = function (ids, domain, func) {
1668 // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
1661 - obj.file.query('events').filter('domain', '==', domain).take(999999).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1669 + obj.file.query('events').filter('domain', '==', domain).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1670 const docs = [];
1671 for (var i in snapshots) {
1672 const doc = snapshots[i].val();
@@ -1672,6 +1680,9 @@ module.exports.CreateDB = function (parent, func) {
1680 };
1681 obj.GetEventsWithLimit = function (ids, domain, limit, func) {
1682 // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
1683 + // TODO: Request a new AceBase feature for a 'array:contains-one-of' filter:
1684 + // obj.file.indexes.create('events', 'ids', { type: 'array' });
1685 + // db.query('events').filter('ids', 'array:contains-one-of', ids)
1686 obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1687 const docs = [];
1688 for (var i in snapshots) {
@@ -1685,16 +1696,16 @@ module.exports.CreateDB = function (parent, func) {
1696 });
1697 };
1698 obj.GetUserEvents = function (ids, domain, userid, func) {
1688 - obj.file.query('events').take(999999).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1699 + obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1700 };
1701 obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
1702 obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1703 };
1704 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
1694 - obj.file.query('events').take(999999).filter('domain', '==', domain).filter('ids', 'in', ids).filter('msgid', 'in', msgids).filter('time', 'between', [start, end]).sort('time', false).get({ exclude: ['type', '_id', 'domain', 'node'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1705 + obj.file.query('events').filter('domain', '==', domain).filter('ids', 'in', ids).filter('msgid', 'in', msgids).filter('time', 'between', [start, end]).sort('time', false).get({ exclude: ['type', '_id', 'domain', 'node'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1706 };
1707 obj.GetUserLoginEvents = function (domain, userid, func) {
1697 - obj.file.query('events').take(999999).filter('domain', '==', domain).filter('action', 'in', ['authfail', 'login']).filter('userid', '==', userid).filter('msgArgs', 'exists').sort('time', false).get({ include: ['action', 'time', 'msgid', 'msgArgs', 'tokenName'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1708 + obj.file.query('events').filter('domain', '==', domain).filter('action', 'in', ['authfail', 'login']).filter('userid', '==', userid).filter('msgArgs', 'exists').sort('time', false).get({ include: ['action', 'time', 'msgid', 'msgArgs', 'tokenName'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1709 };
1710 obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) {
1711 obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
@@ -1703,54 +1714,62 @@ module.exports.CreateDB = function (parent, func) {
1714 obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1715 };
1716 obj.RemoveAllEvents = function (domain) {
1706 - obj.file.query('events').take(999999).filter('domain', '==', domain).remove().then(function () { if (func) { func(); } });;
1717 + obj.file.query('events').filter('domain', '==', domain).remove().then(function () { if (func) { func(); } });;
1718 };
1719 obj.RemoveAllNodeEvents = function (domain, nodeid) {
1720 if ((domain == null) || (nodeid == null)) return;
1710 - obj.file.query('events').take(999999).filter('domain', '==', domain).filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });;
1721 + obj.file.query('events').filter('domain', '==', domain).filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });;
1722 };
1723 obj.RemoveAllUserEvents = function (domain, userid) {
1724 if ((domain == null) || (userid == null)) return;
1714 - obj.file.query('events').take(999999).filter('domain', '==', domain).filter('userid', '==', userid).remove().then(function () { if (func) { func(); } });;
1725 + obj.file.query('events').filter('domain', '==', domain).filter('userid', '==', userid).remove().then(function () { if (func) { func(); } });;
1726 };
1727 obj.GetFailedLoginCount = function (userid, domainid, lastlogin, func) {
1717 - obj.file.query('events').take(999999).filter('domain', '==', domainid).filter('userid', '==', userid).filter('time', '>', lastlogin).sort('time', false).get({ snapshots: false }, function (snapshots) { func(null, snapshots.length); });
1728 + obj.file.query('events').filter('domain', '==', domainid).filter('userid', '==', userid).filter('time', '>', lastlogin).sort('time', false).get({ snapshots: false }, function (snapshots) { func(null, snapshots.length); });
1729 }
1730
1731 // Database actions on the power collection
1732 obj.getAllPower = function (func) {
1722 - obj.file.query('power').take(999999).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1733 + obj.file.ref('power').get(function (snapshot) {
1734 + const val = snapshot.val();
1735 + const docs = Object.keys(val).map(function(key) { return val[key]; });
1736 + func(null, docs);
1737 + });
1738 };
1739 obj.storePowerEvent = function (event, multiServer, func) {
1740 if (multiServer != null) { event.server = multiServer.serverid; }
1741 obj.file.ref('power').push(event).then(function (userRef) { if (func) { func(); } });
1742 };
1743 obj.getPowerTimeline = function (nodeid, func) {
1729 - obj.file.query('power').take(999999).filter('nodeid', 'in', ['*', nodeid]).sort('time').get({ exclude: ['_id', 'nodeid', 's'] }, function (snapshots) {
1744 + obj.file.query('power').filter('nodeid', 'in', ['*', nodeid]).sort('time').get({ exclude: ['_id', 'nodeid', 's'] }, function (snapshots) {
1745 const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
1746 });
1747 };
1748 obj.removeAllPowerEvents = function () {
1734 - obj.file.query('power').take(999999).remove().then(function () { if (func) { func(); } });
1749 + obj.file.ref('power').remove().then(function () { if (func) { func(); } });
1750 };
1751 obj.removeAllPowerEventsForNode = function (nodeid) {
1752 if (nodeid == null) return;
1738 - obj.file.query('power').take(999999).filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });
1753 + obj.file.query('power').filter('nodeid', '==', nodeid).remove().then(function () { if (func) { func(); } });
1754 };
1755
1756 // Database actions on the SMBIOS collection
1757 if (obj.smbiosfile != null) {
1758 obj.GetAllSMBIOS = function (func) {
1744 - obj.file.query('smbios').take(999999).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1759 + obj.file.ref('smbios').get(function (snapshot) {
1760 + const val = snapshot.val();
1761 + const docs = Object.keys(val).map(function(key) { return val[key]; });
1762 + func(null, docs);
1763 + });
1764 };
1765 obj.SetSMBIOS = function (smbios, func) {
1766 obj.file.ref('meshcentral/' + encodeURIComponent(smbios._id)).set(smbios).then(function (ref) { if (func) { func(); } })
1767 };
1768 obj.RemoveSMBIOS = function (id) {
1750 - obj.file.query('smbios').filter('_id', 'in', id).take(999999).remove().then(function () { if (func) { func(); } });
1769 + obj.file.query('smbios').filter('_id', '==', id).remove().then(function () { if (func) { func(); } });
1770 };
1771 obj.GetSMBIOS = function (id, func) {
1753 - obj.file.query('smbios').filter('_id', 'in', id).take(1).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1772 + obj.file.query('smbios').filter('_id', '==', id).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); });
1773 };
1774 }
1775
@@ -1761,7 +1780,7 @@ module.exports.CreateDB = function (parent, func) {
1780 obj.GetServerStats = function (hours, func) {
1781 var t = new Date();
1782 t.setTime(t.getTime() - (60 * 60 * 1000 * hours));
1764 - obj.file.query('stats').take(999999).filter('time', '>', t).get({ exclude: ['_id', 'cpu'] }, function (snapshots) {
1783 + obj.file.query('stats').filter('time', '>', t).get({ exclude: ['_id', 'cpu'] }, function (snapshots) {
1784 const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
1785 });
1786 };
@@ -1774,14 +1793,14 @@ module.exports.CreateDB = function (parent, func) {
1793
1794 // List all configuration files
1795 obj.listConfigFiles = function (func) {
1777 - obj.file.query('meshcentral').take(999999).filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1796 + obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1797 const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs);
1798 });
1799 }
1800
1801 // Get all configuration files
1802 obj.getAllConfigFiles = function (password, func) {
1784 - obj.file.query('meshcentral').take(999999).filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1803 + obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1804 const docs = [];
1805 for (var i in snapshots) { docs.push(snapshots[i].val()); }
1806 var r = null;
@@ -1797,21 +1816,27 @@ module.exports.CreateDB = function (parent, func) {
1816 // Get database information
1817 obj.getDbStats = function (func) {
1818 obj.stats = { c: 5 };
1800 - obj.file.query('meshcentral').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.meshcentral = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1801 - obj.file.query('events').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.events = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1802 - obj.file.query('power').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.power = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1803 - obj.file.query('smbios').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.smbios = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1804 - obj.file.query('stats').take(999999).get({ snapshots: false }, function (snapshots) { obj.stats.serverstats = snapshots.length; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1819 + obj.file.ref('meshcentral').count().then(function (count) { obj.stats.meshcentral = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1820 + obj.file.ref('events').count().then(function (count) { obj.stats.events = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1821 + obj.file.ref('power').count().then(function (count) { obj.stats.power = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1822 + obj.file.ref('smbios').count().then(function (count) { obj.stats.smbios = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1823 + obj.file.ref('stats').count().then(function (count) { obj.stats.serverstats = count; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } });
1824 }
1825
1826 // Plugin operations
1827 if (obj.pluginsActive) {
1809 - obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.file.ref('plugin/' + encodeURIComponent(plugin._id)).set(plugin).then(function (ref) { if (func) { func(); } }) }; // Add a plugin
1810 - obj.getPlugins = function (func) { obj.file.query('plugin').take(999999).sort('name').get({ exclude: ['type'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; // Get all plugins
1811 - obj.getPlugin = function (id, func) { obj.file.query('plugin').take(999999).filter('_id', '==', id).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; // Get plugin
1812 - obj.deletePlugin = function (id, func) { obj.file.ref('plugin/' + encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); }; // Delete plugin
1813 - obj.setPluginStatus = function (id, status, func) { obj.file.ref('plugin/' + encodeURIComponent(id)).update(args).then(function (ref) { if (func) { func(); } }) };
1814 - obj.updatePlugin = function (id, args, func) { delete args._id; obj.file.ref('plugin/' + encodeURIComponent(id)).set(args).then(function (ref) { if (func) { func(); } }) };
1828 + obj.addPlugin = function (plugin, func) { plugin.type = 'plugin'; obj.file.ref('plugin').child(encodeURIComponent(plugin._id)).set(plugin).then(function (ref) { if (func) { func(); } }) }; // Add a plugin
1829 + obj.getPlugins = function (func) {
1830 + obj.file.ref('plugin').get({ exclude: ['type'] }, function (snapshot) {
1831 + const val = snapshot.val();
1832 + const docs = Object.keys(val).map(function(key) { return val[key]; }).sort(function(a, b) { return a.name < b.name ? -1 : 1 });
1833 + func(null, docs);
1834 + });
1835 + }; // Get all plugins
1836 + obj.getPlugin = function (id, func) { obj.file.query('plugin').filter('_id', '==', id).get(function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; // Get plugin
1837 + obj.deletePlugin = function (id, func) { obj.file.ref('plugin').child(encodeURIComponent(id)).remove().then(function () { if (func) { func(); } }); }; // Delete plugin
1838 + obj.setPluginStatus = function (id, status, func) { obj.file.ref('plugin'.child(encodeURIComponent(id)).update({ status: status }).then(function (ref) { if (func) { func(); } }) };
1839 + obj.updatePlugin = function (id, args, func) { delete args._id; obj.file.ref('plugin').child(encodeURIComponent(id)).set(args).then(function (ref) { if (func) { func(); } }) };
1840 }
1841 } else if (obj.databaseType == 6) {
1842 // Database actions on the main collection (Postgres)