add filter for events (#5975)

* add filter to node events * add filter to my events * add filter to user events * improve sql querys Signed-off-by: si458 <simonsmith5521@gmail.com>

Simon Smith committed Mar 31, 2024 at 13:50 UTC 95bbd7157f5f2b3945bf727f968008a8237147d9
3 files changed +514 -102
db.js
+441 -89
@@ -1494,33 +1494,91 @@ module.exports.CreateDB = function (parent, func) {
1494 }
1495 });
1496 };
1497 - obj.GetEvents = function (ids, domain, func) {
1497 + obj.GetEvents = function (ids, domain, filter, func) {
1498 + var query = "SELECT doc FROM events ";
1499 + var dataarray = [domain];
1500 if (ids.indexOf('*') >= 0) {
1499 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func);
1501 + query = query + "WHERE (domain = $1";
1502 + if (filter != null) {
1503 + query = query + " AND action = $2";
1504 + dataarray.push(filter);
1505 + }
1506 + query = query + ") ORDER BY time DESC";
1507 } else {
1501 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC', [domain], func);
1508 + query = query + 'JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))';
1509 + if (filter != null) {
1510 + query = query + " AND action = $2";
1511 + dataarray.push(filter);
1512 + }
1513 + query = query + ") GROUP BY id ORDER BY time DESC ";
1514 }
1515 + sqlDbQuery(query, dataarray, func);
1516 };
1504 - obj.GetEventsWithLimit = function (ids, domain, limit, func) {
1517 + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
1518 + var query = "SELECT doc FROM events ";
1519 + var dataarray = [domain];
1520 if (ids.indexOf('*') >= 0) {
1506 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func);
1521 + query = query + "WHERE (domain = $1";
1522 + if (filter != null) {
1523 + query = query + " AND action = $2) ORDER BY time DESC LIMIT $3";
1524 + dataarray.push(filter);
1525 + } else {
1526 + query = query + ") ORDER BY time DESC LIMIT $2";
1527 + }
1528 } else {
1508 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC LIMIT $2', [domain, limit], func);
1529 + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (" + dbMergeSqlArray(ids) + "))";
1530 + if (filter != null) {
1531 + query = query + " AND action = $2) GROUP BY id ORDER BY time DESC LIMIT $3";
1532 + dataarray.push(filter);
1533 + } else {
1534 + query = query + ") GROUP BY id ORDER BY time DESC LIMIT $2";
1535 + }
1536 }
1537 + dataarray.push(limit);
1538 + sqlDbQuery(query, dataarray, func);
1539 };
1511 - obj.GetUserEvents = function (ids, domain, userid, func) {
1540 + obj.GetUserEvents = function (ids, domain, userid, filter, func) {
1541 + var query = "SELECT doc FROM events ";
1542 + var dataarray = [domain, userid];
1543 if (ids.indexOf('*') >= 0) {
1513 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func);
1544 + query = query + "WHERE (domain = $1 AND userid = $2";
1545 + if (filter != null) {
1546 + query = query + " AND action = $3";
1547 + dataarray.push(filter);
1548 + }
1549 + query = query + ") ORDER BY time DESC";
1550 } else {
1515 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC', [domain, userid], func);
1551 + query = query + 'JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))';
1552 + if (filter != null) {
1553 + query = query + " AND action = $3";
1554 + dataarray.push(filter);
1555 + }
1556 + query = query + ") GROUP BY id ORDER BY time DESC";
1557 }
1558 + sqlDbQuery(query, dataarray, func);
1559 };
1518 - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
1560 + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
1561 + var query = "SELECT doc FROM events ";
1562 + var dataarray = [domain, userid];
1563 if (ids.indexOf('*') >= 0) {
1520 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func);
1564 + query = query + "WHERE (domain = $1 AND userid = $2";
1565 + if (filter != null) {
1566 + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
1567 + dataarray.push(filter);
1568 + } else {
1569 + query = query + ") ORDER BY time DESC LIMIT $3";
1570 + }
1571 } else {
1522 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, userid, limit], func);
1572 + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (" + dbMergeSqlArray(ids) + "))";
1573 + if (filter != null) {
1574 + query = query + " AND action = $3) GROUP BY id ORDER BY time DESC LIMIT $4";
1575 + dataarray.push(filter);
1576 + } else {
1577 + query = query + ") GROUP BY id ORDER BY time DESC LIMIT $3";
1578 + }
1579 }
1580 + dataarray.push(limit);
1581 + sqlDbQuery(query, dataarray, func);
1582 };
1583 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
1584 if (ids.indexOf('*') >= 0) {
@@ -1530,8 +1588,30 @@ module.exports.CreateDB = function (parent, func) {
1588 }
1589 };
1590 //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
1533 - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) ORDER BY time DESC LIMIT $3', [nodeid, domain, limit], func); };
1534 - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ORDER BY time DESC LIMIT $4', [nodeid, domain, userid, limit], func); };
1591 + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
1592 + var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2";
1593 + var dataarray = [nodeid, domain];
1594 + if (filter != null) {
1595 + query = query + "AND action = $3) ORDER BY time DESC LIMIT $4";
1596 + dataarray.push(filter);
1597 + } else {
1598 + query = query + ") ORDER BY time DESC LIMIT $3";
1599 + }
1600 + dataarray.push(limit);
1601 + sqlDbQuery(query, dataarray, func);
1602 + };
1603 + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
1604 + var query = "SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ";
1605 + var dataarray = [nodeid, domain, userid];
1606 + if (filter != null) {
1607 + query = query + "AND (action = $4) ORDER BY time DESC LIMIT $5";
1608 + dataarray.push(filter);
1609 + } else {
1610 + query = query + "ORDER BY time DESC LIMIT $4";
1611 + }
1612 + dataarray.push(limit);
1613 + sqlDbQuery(query, dataarray, func);
1614 + };
1615 obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
1616 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) { }); };
1617 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) { }); };
@@ -1669,42 +1749,78 @@ module.exports.CreateDB = function (parent, func) {
1749 obj.dbCounters.eventsSet++;
1750 obj.file.ref('events').push(event).then(function (userRef) { if (func) { func(); } });
1751 };
1672 - obj.GetEvents = function (ids, domain, func) {
1752 + obj.GetEvents = function (ids, domain, filter, func) {
1753 // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
1674 - obj.file.query('events').filter('domain', '==', domain).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1675 - const docs = [];
1676 - for (var i in snapshots) {
1677 - const doc = snapshots[i].val();
1678 - if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1679 - var found = false;
1680 - for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1681 - if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1682 - }
1683 - func(null, docs);
1684 - });
1754 + if (filter != null) {
1755 + obj.file.query('events').filter('domain', '==', domain).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1756 + const docs = [];
1757 + for (var i in snapshots) {
1758 + const doc = snapshots[i].val();
1759 + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1760 + var found = false;
1761 + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1762 + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1763 + }
1764 + func(null, docs);
1765 + });
1766 + } else {
1767 + obj.file.query('events').filter('domain', '==', domain).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1768 + const docs = [];
1769 + for (var i in snapshots) {
1770 + const doc = snapshots[i].val();
1771 + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1772 + var found = false;
1773 + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1774 + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1775 + }
1776 + func(null, docs);
1777 + });
1778 + }
1779 };
1686 - obj.GetEventsWithLimit = function (ids, domain, limit, func) {
1780 + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
1781 // This request is slow since we have not found a .filter() that will take two arrays and match a single item.
1782 // TODO: Request a new AceBase feature for a 'array:contains-one-of' filter:
1783 // obj.file.indexes.create('events', 'ids', { type: 'array' });
1784 // db.query('events').filter('ids', 'array:contains-one-of', ids)
1691 - obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1692 - const docs = [];
1693 - for (var i in snapshots) {
1694 - const doc = snapshots[i].val();
1695 - if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1696 - var found = false;
1697 - for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1698 - if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1699 - }
1700 - func(null, docs);
1701 - });
1785 + if (filter != null) {
1786 + obj.file.query('events').filter('domain', '==', domain).filter('action', '==', filter).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1787 + const docs = [];
1788 + for (var i in snapshots) {
1789 + const doc = snapshots[i].val();
1790 + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1791 + var found = false;
1792 + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1793 + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1794 + }
1795 + func(null, docs);
1796 + });
1797 + } else {
1798 + obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) {
1799 + const docs = [];
1800 + for (var i in snapshots) {
1801 + const doc = snapshots[i].val();
1802 + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue;
1803 + var found = false;
1804 + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches
1805 + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); }
1806 + }
1807 + func(null, docs);
1808 + });
1809 + }
1810 };
1703 - obj.GetUserEvents = function (ids, domain, userid, func) {
1704 - 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); });
1811 + obj.GetUserEvents = function (ids, domain, userid, filter, func) {
1812 + if (filter != null) {
1813 + obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).filter('action', '==', filter).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); });
1814 + } else {
1815 + 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); });
1816 + }
1817 };
1706 - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
1707 - 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); });
1818 + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
1819 + if (filter != null) {
1820 + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).filter('action', '==', filter).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); });
1821 + } else {
1822 + 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); });
1823 + }
1824 };
1825 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
1826 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); });
@@ -1712,10 +1828,19 @@ module.exports.CreateDB = function (parent, func) {
1828 obj.GetUserLoginEvents = function (domain, userid, func) {
1829 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); });
1830 };
1715 - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) {
1716 - 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); });
1831 + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
1832 + if (filter != null) {
1833 + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('action', '==', filter).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); });
1834 + } else {
1835 + 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); });
1836 + }
1837 };
1718 - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) {
1838 + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
1839 + if (filter != null) {
1840 + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).filter('action', '==', filter).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); });
1841 + } else {
1842 + 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); });
1843 + }
1844 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); });
1845 };
1846 obj.RemoveAllEvents = function (domain) {
@@ -1913,33 +2038,98 @@ module.exports.CreateDB = function (parent, func) {
2038 }
2039 });
2040 };
1916 - obj.GetEvents = function (ids, domain, func) {
2041 + obj.GetEvents = function (ids, domain, filter, func) {
2042 + var query = "SELECT doc FROM events ";
2043 + var dataarray = [domain];
2044 if (ids.indexOf('*') >= 0) {
1918 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func);
2045 + query = query + "WHERE (domain = $1";
2046 + if (filter != null) {
2047 + query = query + " AND action = $2";
2048 + dataarray.push(filter);
2049 + }
2050 + query = query + ") ORDER BY time DESC";
2051 } else {
1920 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))) GROUP BY id ORDER BY time DESC', [domain, ids], func);
2052 + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))";
2053 + dataarray.push(ids);
2054 + if (filter != null) {
2055 + query = query + " AND action = $3";
2056 + dataarray.push(filter);
2057 + }
2058 + query = query + ") GROUP BY id ORDER BY time DESC";
2059 }
2060 + sqlDbQuery(query, dataarray, func);
2061 };
1923 - obj.GetEventsWithLimit = function (ids, domain, limit, func) {
2062 + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
2063 + var query = "SELECT doc FROM events ";
2064 + var dataarray = [domain];
2065 if (ids.indexOf('*') >= 0) {
1925 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func);
2066 + query = query + "WHERE (domain = $1";
2067 + if (filter != null) {
2068 + query = query + " AND action = $2) ORDER BY time DESC LIMIT $3";
2069 + dataarray.push(filter);
2070 + } else {
2071 + query = query + ") ORDER BY time DESC LIMIT $2";
2072 + }
2073 } else {
1927 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, ids, limit], func);
2074 + if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2075 + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))";
2076 + dataarray.push(ids);
2077 + if (filter != null) {
2078 + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
2079 + dataarray.push(filter);
2080 + } else {
2081 + query = query + ") ORDER BY time DESC LIMIT $3";
2082 + }
2083 }
2084 + dataarray.push(limit);
2085 + sqlDbQuery(query, dataarray, func);
2086 };
1930 - obj.GetUserEvents = function (ids, domain, userid, func) {
2087 + obj.GetUserEvents = function (ids, domain, userid, filter, func) {
2088 + var query = "SELECT doc FROM events ";
2089 + var dataarray = [domain, userid];
2090 if (ids.indexOf('*') >= 0) {
1932 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func);
2091 + query = query + "WHERE (domain = $1 AND userid = $2";
2092 + if (filter != null) {
2093 + query = query + " AND action = $3";
2094 + dataarray.push(filter);
2095 + }
2096 + query = query + ") ORDER BY time DESC";
2097 } else {
1934 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func);
2098 + if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2099 + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))";
2100 + dataarray.push(ids);
2101 + if (filter != null) {
2102 + query = query + " AND action = $4";
2103 + dataarray.push(filter);
2104 + }
2105 + query = query + ") GROUP BY id ORDER BY time DESC";
2106 }
2107 + sqlDbQuery(query, dataarray, func);
2108 };
1937 - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
2109 + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
2110 + var query = "SELECT doc FROM events ";
2111 + var dataarray = [domain, userid];
2112 if (ids.indexOf('*') >= 0) {
1939 - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func);
2113 + query = query + "WHERE (domain = $1 AND userid = $2";
2114 + if (filter != null) {
2115 + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4 ";
2116 + dataarray.push(filter);
2117 + } else {
2118 + query = query + ") ORDER BY time DESC LIMIT $3";
2119 + }
2120 } else {
1941 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))) GROUP BY id ORDER BY time DESC LIMIT $4', [domain, userid, ids, limit], func);
2121 + if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2122 + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))";
2123 + dataarray.push(ids);
2124 + if (filter != null) {
2125 + query = query + " AND action = $4) GROUP BY id ORDER BY time DESC LIMIT $5";
2126 + dataarray.push(filter);
2127 + } else {
2128 + query = query + ") GROUP BY id ORDER BY time DESC LIMIT $4";
2129 + }
2130 }
2131 + dataarray.push(limit);
2132 + sqlDbQuery(query, dataarray, func);
2133 };
2134 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
2135 if (ids.indexOf('*') >= 0) {
@@ -1949,8 +2139,30 @@ module.exports.CreateDB = function (parent, func) {
2139 }
2140 };
2141 //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
1952 - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) ORDER BY time DESC LIMIT $3', [nodeid, domain, limit], func); };
1953 - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ORDER BY time DESC LIMIT $4', [nodeid, domain, userid, limit], func); };
2142 + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
2143 + var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2";
2144 + var dataarray = [nodeid, domain];
2145 + if (filter != null) {
2146 + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4";
2147 + dataarray.push(filter);
2148 + } else {
2149 + query = query + ") ORDER BY time DESC LIMIT $3";
2150 + }
2151 + dataarray.push(limit);
2152 + sqlDbQuery(query, dataarray, func);
2153 + };
2154 + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
2155 + var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2 AND ((userid = $3) OR (userid IS NULL))";
2156 + var dataarray = [nodeid, domain, userid];
2157 + if (filter != null) {
2158 + query = query + " AND action = $4) ORDER BY time DESC LIMIT $5";
2159 + dataarray.push(filter);
2160 + } else {
2161 + query = query + ") ORDER BY time DESC LIMIT $4";
2162 + }
2163 + dataarray.push(limit);
2164 + sqlDbQuery(query, dataarray, func);
2165 + };
2166 obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
2167 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) { }); };
2168 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) { }); };
@@ -2078,37 +2290,95 @@ module.exports.CreateDB = function (parent, func) {
2290 for (var i in event.ids) { if (event.ids[i] != '*') { batchQuery.push(['INSERT INTO eventids VALUE (LAST_INSERT_ID(), ?)', [event.ids[i]]]); } }
2291 sqlDbBatchExec(batchQuery, function (err, docs) { if (func != null) { func(err, docs); } });
2292 };
2081 - obj.GetEvents = function (ids, domain, func) {
2293 + obj.GetEvents = function (ids, domain, filter, func) {
2294 + var query = "SELECT doc FROM events ";
2295 + var dataarray = [domain];
2296 if (ids.indexOf('*') >= 0) {
2083 - sqlDbQuery('SELECT doc FROM events WHERE (domain = ?) ORDER BY time DESC', [domain], func);
2297 + query = query + "WHERE (domain = ?";
2298 + if (filter != null) {
2299 + query = query + " AND action = ?";
2300 + dataarray.push(filter);
2301 + }
2302 + query = query + ") ORDER BY time DESC";
2303 } else {
2304 if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2086 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)) GROUP BY id ORDER BY time DESC', [domain, ids], func);
2305 + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)";
2306 + dataarray.push(ids);
2307 + if (filter != null) {
2308 + query = query + " AND action = ?";
2309 + dataarray.push(filter);
2310 + }
2311 + query = query + ") GROUP BY id ORDER BY time DESC";
2312 }
2313 + sqlDbQuery(query, dataarray, func);
2314 };
2089 - obj.GetEventsWithLimit = function (ids, domain, limit, func) {
2315 + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
2316 + var query = "SELECT doc FROM events ";
2317 + var dataarray = [domain];
2318 if (ids.indexOf('*') >= 0) {
2091 - sqlDbQuery('SELECT doc FROM events WHERE (domain = ?) ORDER BY time DESC LIMIT ?', [domain, limit], func);
2319 + query = query + "WHERE (domain = ?";
2320 + if (filter != null) {
2321 + query = query + " AND action = ? ";
2322 + dataarray.push(filter);
2323 + }
2324 + query = query + ") ORDER BY time DESC LIMIT ?";
2325 } else {
2326 if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2094 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)) GROUP BY id ORDER BY time DESC LIMIT ?', [domain, ids, limit], func);
2327 + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)";
2328 + dataarray.push(ids);
2329 + if (filter != null) {
2330 + query = query + " AND action = ?";
2331 + dataarray.push(filter);
2332 + }
2333 + query = query + ") GROUP BY id ORDER BY time DESC LIMIT ?";
2334 }
2335 + dataarray.push(limit);
2336 + sqlDbQuery(query, dataarray, func);
2337 };
2097 - obj.GetUserEvents = function (ids, domain, userid, func) {
2338 + obj.GetUserEvents = function (ids, domain, userid, filter, func) {
2339 + var query = "SELECT doc FROM events ";
2340 + var dataarray = [domain, userid];
2341 if (ids.indexOf('*') >= 0) {
2099 - sqlDbQuery('SELECT doc FROM events WHERE (domain = ? AND userid = ?) ORDER BY time DESC', [domain, userid], func);
2342 + query = query + "WHERE (domain = ? AND userid = ?";
2343 + if (filter != null) {
2344 + query = query + " AND action = ?";
2345 + dataarray.push(filter);
2346 + }
2347 + query = query + ") ORDER BY time DESC";
2348 } else {
2349 if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2102 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func);
2350 + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)";
2351 + dataarray.push(ids);
2352 + if (filter != null) {
2353 + query = query + " AND action = ?";
2354 + dataarray.push(filter);
2355 + }
2356 + query = query + ") GROUP BY id ORDER BY time DESC";
2357 }
2358 + sqlDbQuery(query, dataarray, func);
2359 };
2105 - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
2360 + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
2361 + var query = "SELECT doc FROM events ";
2362 + var dataarray = [domain, userid];
2363 if (ids.indexOf('*') >= 0) {
2107 - sqlDbQuery('SELECT doc FROM events WHERE (domain = ? AND userid = ?) ORDER BY time DESC LIMIT ?', [domain, userid, limit], func);
2364 + query = query + "WHERE (domain = ? AND userid = ?";
2365 + if (filter != null) {
2366 + query = query + " AND action = ?";
2367 + dataarray.push(filter);
2368 + }
2369 + query = query + ") ORDER BY time DESC LIMIT ?";
2370 } else {
2371 if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead.
2110 - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)) GROUP BY id ORDER BY time DESC LIMIT ?', [domain, userid, ids, limit], func);
2372 + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)";
2373 + dataarray.push(ids);
2374 + if (filter != null) {
2375 + query = query + " AND action = ?";
2376 + dataarray.push(filter);
2377 + }
2378 + query = query + ") GROUP BY id ORDER BY time DESC LIMIT ?";
2379 }
2380 + dataarray.push(limit);
2381 + sqlDbQuery(query, dataarray, func);
2382 };
2383 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
2384 if (ids.indexOf('*') >= 0) {
@@ -2119,8 +2389,30 @@ module.exports.CreateDB = function (parent, func) {
2389 }
2390 };
2391 //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO
2122 - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = ?) AND (domain = ?) ORDER BY time DESC LIMIT ?', [nodeid, domain, limit], func); };
2123 - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = ?) AND (domain = ?) AND ((userid = ?) OR (userid IS NULL)) ORDER BY time DESC LIMIT ?', [nodeid, domain, userid, limit], func); };
2392 + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
2393 + var query = "SELECT doc FROM events WHERE (nodeid = ? AND domain = ?";
2394 + var dataarray = [nodeid, domain];
2395 + if (filter != null) {
2396 + query = query + " AND action = ?) ORDER BY time DESC LIMIT ?";
2397 + dataarray.push(filter);
2398 + } else {
2399 + query = query + ") ORDER BY time DESC LIMIT ?";
2400 + }
2401 + dataarray.push(limit);
2402 + sqlDbQuery(query, dataarray, func);
2403 + };
2404 + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
2405 + var query = "SELECT doc FROM events WHERE (nodeid = ? AND domain = ? AND ((userid = ?) OR (userid IS NULL))";
2406 + var dataarray = [nodeid, domain, userid];
2407 + if (filter != null) {
2408 + query = query + " AND action = ?) ORDER BY time DESC LIMIT ?";
2409 + dataarray.push(filter);
2410 + } else {
2411 + query = query + ") ORDER BY time DESC LIMIT ?";
2412 + }
2413 + dataarray.push(limit);
2414 + sqlDbQuery(query, dataarray, func);
2415 + };
2416 obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); };
2417 obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = ? AND nodeid = ?', [domain, nodeid], function (err, docs) { }); };
2418 obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = ? AND userid = ?', [domain, userid], function (err, docs) { }); };
@@ -2355,14 +2647,38 @@ module.exports.CreateDB = function (parent, func) {
2647 obj.StoreEvent = function (event, func) { obj.dbCounters.eventsSet++; obj.eventsfile.insertOne(event, func); };
2648 }
2649
2358 - obj.GetEvents = function (ids, domain, func) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func); };
2359 - obj.GetEventsWithLimit = function (ids, domain, limit, func) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func); };
2360 - obj.GetUserEvents = function (ids, domain, userid, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func); };
2361 - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func); };
2650 + obj.GetEvents = function (ids, domain, filter, func) {
2651 + var finddata = { domain: domain, ids: { $in: ids } };
2652 + if (filter != null) finddata.action = filter;
2653 + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func);
2654 + };
2655 + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
2656 + var finddata = { domain: domain, ids: { $in: ids } };
2657 + if (filter != null) finddata.action = filter;
2658 + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
2659 + };
2660 + obj.GetUserEvents = function (ids, domain, userid, filter, func) {
2661 + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
2662 + if (filter != null) finddata.action = filter;
2663 + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func);
2664 + };
2665 + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
2666 + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
2667 + if (filter != null) finddata.action = filter;
2668 + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
2669 + };
2670 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }], msgid: { $in: msgids }, time: { $gte: start, $lte: end } }).project({ type: 0, _id: 0, domain: 0, node: 0 }).sort({ time: 1 }).toArray(func); };
2671 obj.GetUserLoginEvents = function (domain, userid, func) { obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }).project({ action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }).toArray(func); };
2364 - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { obj.eventsfile.find({ domain: domain, nodeid: nodeid }).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func); };
2365 - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { obj.eventsfile.find({ domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } }).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func); };
2672 + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
2673 + var finddata = { domain: domain, nodeid: nodeid };
2674 + if (filter != null) finddata.action = filter;
2675 + obj.eventsfile.find(finddata).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
2676 + };
2677 + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
2678 + var finddata = { domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } };
2679 + if (filter != null) finddata.action = filter;
2680 + obj.eventsfile.find(finddata).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func);
2681 + };
2682 obj.RemoveAllEvents = function (domain) { obj.eventsfile.deleteMany({ domain: domain }, { multi: true }); };
2683 obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; obj.eventsfile.deleteMany({ domain: domain, nodeid: nodeid }, { multi: true }); };
2684 obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; obj.eventsfile.deleteMany({ domain: domain, userid: userid }, { multi: true }); };
@@ -2527,20 +2843,40 @@ module.exports.CreateDB = function (parent, func) {
2843 // Database actions on the events collection
2844 obj.GetAllEvents = function (func) { obj.eventsfile.find({}, func); };
2845 obj.StoreEvent = function (event, func) { obj.eventsfile.insert(event, func); };
2530 - obj.GetEvents = function (ids, domain, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func); } else { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func); } };
2531 - obj.GetEventsWithLimit = function (ids, domain, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func); } };
2532 - obj.GetUserEvents = function (ids, domain, userid, func) {
2846 + obj.GetEvents = function (ids, domain, filter, func) {
2847 + var finddata = { domain: domain, ids: { $in: ids } };
2848 + if (filter != null) finddata.action = filter;
2849 + if (obj.databaseType == 1) {
2850 + obj.eventsfile.find(finddata, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func);
2851 + } else {
2852 + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func);
2853 + }
2854 + };
2855 + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) {
2856 + var finddata = { domain: domain, ids: { $in: ids } };
2857 + if (filter != null) finddata.action = filter;
2858 if (obj.databaseType == 1) {
2534 - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func);
2859 + obj.eventsfile.find(finddata, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func);
2860 } else {
2536 - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func);
2861 + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func);
2862 }
2863 };
2539 - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) {
2864 + obj.GetUserEvents = function (ids, domain, userid, filter, func) {
2865 + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
2866 + if (filter != null) finddata.action = filter;
2867 if (obj.databaseType == 1) {
2541 - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func);
2868 + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func);
2869 } else {
2543 - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func);
2870 + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func);
2871 + }
2872 + };
2873 + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) {
2874 + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] };
2875 + if (filter != null) finddata.action = filter;
2876 + if (obj.databaseType == 1) {
2877 + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func);
2878 + } else {
2879 + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func);
2880 }
2881 };
2882 obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) {
@@ -2557,8 +2893,24 @@ module.exports.CreateDB = function (parent, func) {
2893 obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }, { action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }, func);
2894 }
2895 };
2560 - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); } };
2561 - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); } };
2896 + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) {
2897 + var finddata = { domain: domain, nodeid: nodeid };
2898 + if (filter != null) finddata.action = filter;
2899 + if (obj.databaseType == 1) {
2900 + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func);
2901 + } else {
2902 + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func);
2903 + }
2904 + };
2905 + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) {
2906 + var finddata = { domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } };
2907 + if (filter != null) finddata.action = filter;
2908 + if (obj.databaseType == 1) {
2909 + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func);
2910 + } else {
2911 + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func);
2912 + }
2913 + };
2914 obj.RemoveAllEvents = function (domain) { obj.eventsfile.remove({ domain: domain }, { multi: true }); };
2915 obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; obj.eventsfile.remove({ domain: domain, nodeid: nodeid }, { multi: true }); };
2916 obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; obj.eventsfile.remove({ domain: domain, userid: userid }, { multi: true }); };
meshuser.js
+25 -10
@@ -1022,15 +1022,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1022 // TODO: Add the meshes command.userid has access to (???)
1023 var filter = [command.userid];
1024
1025 + var actionfilter = null;
1026 + if (command.filter != null) {
1027 + if (['agentlog','batchupload','changenode','manual','relaylog','removenode','runcommands'].includes(command.filter)) actionfilter = command.filter;
1028 + }
1029 +
1030 if ((command.limit == null) || (typeof command.limit != 'number')) {
1031 // Send the list of all events for this session
1027 - db.GetUserEvents(filter, domain.id, command.userid, function (err, docs) {
1032 + db.GetUserEvents(filter, domain.id, command.userid, actionfilter, function (err, docs) {
1033 if (err != null) return;
1034 try { ws.send(JSON.stringify({ action: 'events', events: docs, userid: command.userid, tag: command.tag })); } catch (ex) { }
1035 });
1036 } else {
1037 // Send the list of most recent events for this session, up to 'limit' count
1033 - db.GetUserEventsWithLimit(filter, domain.id, command.userid, command.limit, function (err, docs) {
1038 + db.GetUserEventsWithLimit(filter, domain.id, command.userid, command.limit, actionfilter, function (err, docs) {
1039 if (err != null) return;
1040 try { ws.send(JSON.stringify({ action: 'events', events: docs, userid: command.userid, tag: command.tag })); } catch (ex) { }
1041 });
@@ -1048,15 +1053,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1053 var limit = 10000;
1054 if (common.validateInt(command.limit, 1, 1000000) == true) { limit = command.limit; }
1055
1056 + var filter = null;
1057 + if (command.filter != null) {
1058 + if (['agentlog','batchupload','changenode','manual','relaylog','removenode','runcommands'].includes(command.filter)) filter = command.filter;
1059 + }
1060 +
1061 if (((rights & MESHRIGHT_LIMITEVENTS) != 0) && (rights != MESHRIGHT_ADMIN)) {
1062 // Send the list of most recent events for this nodeid that only apply to us, up to 'limit' count
1053 - db.GetNodeEventsSelfWithLimit(node._id, domain.id, user._id, limit, function (err, docs) {
1063 + db.GetNodeEventsSelfWithLimit(node._id, domain.id, user._id, limit, filter, function (err, docs) {
1064 if (err != null) return;
1065 try { ws.send(JSON.stringify({ action: 'events', events: docs, nodeid: node._id, tag: command.tag })); } catch (ex) { }
1066 });
1067 } else {
1068 // Send the list of most recent events for this nodeid, up to 'limit' count
1059 - db.GetNodeEventsWithLimit(node._id, domain.id, limit, function (err, docs) {
1069 + db.GetNodeEventsWithLimit(node._id, domain.id, limit, filter, function (err, docs) {
1070 if (err != null) return;
1071 try { ws.send(JSON.stringify({ action: 'events', events: docs, nodeid: node._id, tag: command.tag })); } catch (ex) { }
1072 });
@@ -1076,15 +1086,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1086 for (var link in obj.user.links) { if (((obj.user.links[link].rights & MESHRIGHT_LIMITEVENTS) != 0) && ((obj.user.links[link].rights != MESHRIGHT_ADMIN))) { exGroupFilter2.push(link); } }
1087 for (var i in filter2) { if (exGroupFilter2.indexOf(filter2[i]) == -1) { filter.push(filter2[i]); } }
1088
1089 + var actionfilter = null;
1090 + if (command.filter != null) {
1091 + if (['agentlog','batchupload','changenode','manual','relaylog','removenode','runcommands'].includes(command.filter)) actionfilter = command.filter;
1092 + }
1093 +
1094 if ((command.limit == null) || (typeof command.limit != 'number')) {
1095 // Send the list of all events for this session
1081 - db.GetEvents(filter, domain.id, function (err, docs) {
1096 + db.GetEvents(filter, domain.id, actionfilter, function (err, docs) {
1097 if (err != null) return;
1098 try { ws.send(JSON.stringify({ action: 'events', events: docs, user: command.user, tag: command.tag })); } catch (ex) { }
1099 });
1100 } else {
1101 // Send the list of most recent events for this session, up to 'limit' count
1087 - db.GetEventsWithLimit(filter, domain.id, command.limit, function (err, docs) {
1102 + db.GetEventsWithLimit(filter, domain.id, command.limit, actionfilter, function (err, docs) {
1103 if (err != null) return;
1104 try { ws.send(JSON.stringify({ action: 'events', events: docs, user: command.user, tag: command.tag })); } catch (ex) { }
1105 });
@@ -1101,7 +1116,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1116 if (err != null) { try { ws.send(JSON.stringify({ action: 'recordings', error: 1, tag: command.tag })); } catch (ex) { } return; }
1117 if ((command.limit == null) || (typeof command.limit != 'number')) {
1118 // Send the list of all recordings
1104 - db.GetEvents(['recording'], domain.id, function (err, docs) {
1119 + db.GetEvents(['recording'], domain.id, null, function (err, docs) {
1120 if (err != null) { try { ws.send(JSON.stringify({ action: 'recordings', error: 2, tag: command.tag })); } catch (ex) { } return; }
1121 for (var i in docs) {
1122 delete docs[i].action; delete docs[i].etype; delete docs[i].msg; // TODO: We could make a more specific query in the DB and never have these.
@@ -1111,7 +1126,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1126 });
1127 } else {
1128 // Send the list of most recent recordings, up to 'limit' count
1114 - db.GetEventsWithLimit(['recording'], domain.id, command.limit, function (err, docs) {
1129 + db.GetEventsWithLimit(['recording'], domain.id, command.limit, null, function (err, docs) {
1130 if (err != null) { try { ws.send(JSON.stringify({ action: 'recordings', error: 2, tag: command.tag })); } catch (ex) { } return; }
1131 for (var i in docs) {
1132 delete docs[i].action; delete docs[i].etype; delete docs[i].msg; // TODO: We could make a more specific query in the DB and never have these.
@@ -4779,7 +4794,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4794 });
4795 } else {
4796 // Old way
4782 - db.GetUserEvents([user._id], domain.id, user._id, function (err, docs) {
4797 + db.GetUserEvents([user._id], domain.id, user._id, null, function (err, docs) {
4798 if (err != null) return;
4799 var e = [];
4800 for (var i in docs) {
@@ -4805,7 +4820,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4820 });
4821 } else {
4822 // Old way
4808 - db.GetUserEvents([command.userid], domain.id, user._id, function (err, docs) {
4823 + db.GetUserEvents([command.userid], domain.id, user._id, null, function (err, docs) {
4824 if (err != null) return;
4825 var e = [];
4826 for (var i in docs) { if ((docs[i].msgArgs) && (docs[i].userid == command.userid) && ((docs[i].action == 'authfail') || (docs[i].action == 'login'))) { e.push({ t: docs[i].time, m: docs[i].msgid, a: docs[i].msgArgs }); } }
views/default.handlebars
+48 -3
@@ -470,6 +470,17 @@
470 <input type="button" style="display:none" value="Download Report" onclick=p3showReportDialog() />
471 </td>
472 <td class="auto-style1">
473 + Filter
474 + <select id=p3filterevents onchange=refreshEvents()>
475 + <option value=>All Logs</option>
476 + <option value=agentlog>Agent Logs</option>
477 + <option value=relaylog>Relay Logs</option>
478 + <option value=manual>Manual Logs</option>
479 + <option value=runcommands>Run Command Logs</option>
480 + <option value=batchupload>Batch Upload Logs</option>
481 + <option value=changenode>Change Node Logs</option>
482 + <option value=removenode>Remove Node Logs</option>
483 + </select>
484 Show
485 <select id=p3limitdropdown onchange=refreshEvents()>
486 <option value=60>Last 60</option>
@@ -975,6 +986,17 @@
986 <td class="h1"></td>
987 <!--<td>&nbsp;<input type=button onclick=refreshDeviceEvents() value="Refresh" /></td>-->
988 <td class="auto-style1">
989 + Filter
990 + <select id=p16filterevents onchange=refreshDeviceEvents()>
991 + <option value=>All Logs</option>
992 + <option value=agentlog>Agent Logs</option>
993 + <option value=relaylog>Relay Logs</option>
994 + <option value=manual>Manual Logs</option>
995 + <option value=runcommands>Run Command Logs</option>
996 + <option value=batchupload>Batch Upload Logs</option>
997 + <option value=changenode>Change Node Logs</option>
998 + <option value=removenode>Remove Node Logs</option>
999 + </select>
1000 Show
1001 <select id=p16limitdropdown onchange=refreshDeviceEvents()>
1002 <option value=60>Last 60</option>
@@ -1104,6 +1126,17 @@
1126 <td class="h1"></td>
1127 <!--<td>&nbsp;<input type=button onclick=refreshUsersEvents() value="Refresh" /></td>-->
1128 <td class="auto-style1">
1129 + Filter
1130 + <select id=p31filterevents onchange=refreshUsersEvents()>
1131 + <option value=>All Logs</option>
1132 + <option value=agentlog>Agent Logs</option>
1133 + <option value=relaylog>Relay Logs</option>
1134 + <option value=manual>Manual Logs</option>
1135 + <option value=runcommands>Run Command Logs</option>
1136 + <option value=batchupload>Batch Upload Logs</option>
1137 + <option value=changenode>Change Node Logs</option>
1138 + <option value=removenode>Remove Node Logs</option>
1139 + </select>
1140 Show
1141 <select id=p31limitdropdown onchange=refreshUsersEvents()>
1142 <option value=60>Last 60</option>
@@ -11714,7 +11747,11 @@
11747 }
11748
11749 function refreshDeviceEvents() {
11717 - meshserver.send({ action: 'events', nodeid: currentNode._id, limit: parseInt(p16limitdropdown.value) });
11750 + if (p16filterevents.value != "") {
11751 + meshserver.send({ action: 'events', nodeid: currentNode._id, limit: parseInt(p16limitdropdown.value), filter: p16filterevents.value });
11752 + } else {
11753 + meshserver.send({ action: 'events', nodeid: currentNode._id, limit: parseInt(p16limitdropdown.value) });
11754 + }
11755 }
11756
11757 function showEventDetails(h, mode) {
@@ -15063,7 +15100,11 @@
15100 }
15101
15102 function refreshEvents() {
15066 - meshserver.send({ action: 'events', limit: parseInt(p3limitdropdown.value) });
15103 + if (p3filterevents.value != "") {
15104 + meshserver.send({ action: 'events', limit: parseInt(p3limitdropdown.value), filter: p3filterevents.value });
15105 + } else {
15106 + meshserver.send({ action: 'events', limit: parseInt(p3limitdropdown.value) });
15107 + }
15108 }
15109
15110 function p3showReportDialog() {
@@ -16939,7 +16980,11 @@
16980 }
16981
16982 function refreshUsersEvents() {
16942 - meshserver.send({ action: 'events', limit: parseInt(p31limitdropdown.value), userid: currentUser._id });
16983 + if (p31filterevents.value != "") {
16984 + meshserver.send({ action: 'events', limit: parseInt(p31limitdropdown.value), userid: currentUser._id, filter: p31filterevents.value });
16985 + } else {
16986 + meshserver.send({ action: 'events', limit: parseInt(p31limitdropdown.value), userid: currentUser._id });
16987 + }
16988 }
16989
16990