Added functions for each report type
Noah Zalev committed
Jan 10, 2022 at 13:48 UTC
0017c99cce6f641cd5e28dde6e85ef91e20f93ac
1 file changed
+143
-139
meshuser.js
+143
-139
@@ -5809,145 +5809,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5809
5810
const msgIdFilter = [5, 10, 11, 12, 122, 123, 124, 125, 126];
5811
5812
- if (command.type == 1) { // This is the remote session report. Shows desktop, terminal, files...
5813
- // If we are not user administrator on this site, only search for events with our own user id.
5814
- var ids = [user._id];
5815
- if ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0) {
5816
- if (command.devGroup != null) {
5817
- ids = [ user._id, command.devGroup ];
5818
- } else {
5819
- if (manageAllDeviceGroups) { ids = ['*']; } else if (user.links) { for (var i in user.links) { ids.push(i); } }
5820
- }
5821
- }
5822
-
5823
- // Get the events in the time range
5824
- // MySQL or MariaDB query will ignore the MsgID filter.
5825
- db.GetEventsTimeRange(ids, domain.id, msgIdFilter, new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
5826
- if (err != null) return;
5827
- var data = { groups: {} };
5828
- var guestNamePresent = false;
5829
-
5830
- // Columns
5831
- if (command.groupBy == 1) {
5832
- data.groupFormat = 'user';
5833
- data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'meshid', title: "devgroup", format: 'mesh' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
5834
- } else if (command.groupBy == 2) {
5835
- data.groupFormat = 'nodemesh';
5836
- data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'userid', title: "user", format: 'user' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
5837
- } else if (command.groupBy == 3) {
5838
- data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'meshid', title: "devgroup", format: 'mesh' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
5839
- }
5840
-
5841
- // Add traffic columns
5842
- if (command.showTraffic) {
5843
- data.columns.push({ id: 'bytesin', title: "bytesin", format: 'bytes', align: 'center', sumBy: 'protocol' });
5844
- data.columns.push({ id: 'bytesout', title: "bytesout", format: 'bytes', align: 'center', sumBy: 'protocol' });
5845
- }
5846
-
5847
- // Rows
5848
- for (var i in docs) {
5849
- // If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
5850
- if (msgIdFilter.indexOf(docs[i].msgid) < 0) continue;
5851
- if ((command.devGroup != null) && (docs[i].ids != null) && (docs[i].ids.indexOf(command.devGroup) == -1)) continue;
5852
-
5853
- var entry = { time: docs[i].time.valueOf() };
5854
-
5855
- // UserID
5856
- if (command.groupBy != 1) { entry.userid = docs[i].userid; }
5857
- if (command.groupBy != 2) { entry.nodeid = docs[i].nodeid; }
5858
- entry.protocol = docs[i].protocol;
5859
-
5860
- // Device Group
5861
- if (docs[i].ids != null) { for (var j in docs[i].ids) { if (docs[i].ids[j].startsWith('mesh/')) { entry.meshid = docs[i].ids[j]; } } }
5862
-
5863
- // Add traffic data
5864
- if (command.showTraffic) { entry.bytesin = docs[i].bytesin; entry.bytesout = docs[i].bytesout; }
5865
-
5866
- // Add guest name if present
5867
- if (docs[i].guestname != null) { entry.guestname = docs[i].guestname; guestNamePresent = true; }
5868
-
5869
- // Session length
5870
- if (((docs[i].msgid >= 10) && (docs[i].msgid <= 12)) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[3] == 'number')) { entry.length = docs[i].msgArgs[3]; }
5871
- else if ((docs[i].msgid >= 122) && (docs[i].msgid <= 126) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[0] == 'number')) { entry.length = docs[i].msgArgs[0]; }
5872
-
5873
- if (command.groupBy == 1) { // Add entry to per user
5874
- if (data.groups[docs[i].userid] == null) { data.groups[docs[i].userid] = { entries: [] }; }
5875
- data.groups[docs[i].userid].entries.push(entry);
5876
- } else if (command.groupBy == 2) { // Add entry to per mesh+device
5877
- if (entry.meshid != null) {
5878
- var k = docs[i].nodeid + '/' + entry.meshid;
5879
- if (data.groups[k] == null) { data.groups[k] = { entries: [] }; }
5880
- data.groups[k].entries.push(entry);
5881
- } else {
5882
- if (data.groups[docs[i].nodeid] == null) { data.groups[docs[i].nodeid] = { entries: [] }; }
5883
- data.groups[docs[i].nodeid].entries.push(entry);
5884
- }
5885
- } else if (command.groupBy == 3) { // Add entry to per day
5886
- var day;
5887
- if ((typeof command.l == 'string') && (typeof command.tz == 'string')) {
5888
- day = new Date(docs[i].time).toLocaleDateString(command.l, { timeZone: command.tz });
5889
- } else {
5890
- day = docs[i].time; // TODO
5891
- }
5892
- if (data.groups[day] == null) { data.groups[day] = { entries: [] }; }
5893
- data.groups[day].entries.push(entry);
5894
- }
5895
-
5896
- }
5897
-
5898
- // Remove guest column if not needed
5899
- if (guestNamePresent == false) {
5900
- if ((command.groupBy == 1) || (command.groupBy == 3)) {
5901
- data.columns.splice(3, 1);
5902
- } else if (command.groupBy == 2) {
5903
- data.columns.splice(2, 1);
5904
- }
5905
- }
5906
-
5907
- try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
5908
- });
5909
- }
5910
-
5911
- if (command.type == 2) { // This is the user traffic usage report.
5912
- // If we are not user administrator on this site, only search for events with our own user id.
5913
- var ids = [user._id]; // If we are nto user administrator, only count our own traffic.
5914
- if ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0) { ids = ['*']; } // If user administrator, count traffic of all users.
5915
-
5916
- // Get the events in the time range
5917
- // MySQL or MariaDB query will ignore the MsgID filter.
5918
- db.GetEventsTimeRange(ids, domain.id, msgIdFilter, new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
5919
- if (err != null) return;
5920
- var data = { groups: { 0: { entries: [] } } };
5921
- data.columns = [{ id: 'userid', title: "user", format: 'user' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: true }, { id: 'bytesin', title: "bytesin", format: 'bytes', align: 'center', sumBy: true }, { id: 'bytesout', title: "bytesout", format: 'bytes', align: 'center', sumBy: true }];
5922
- var userEntries = {};
5923
-
5924
- // Sum all entry logs for each user
5925
- for (var i in docs) {
5926
- // If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
5927
- if (msgIdFilter.indexOf(docs[i].msgid) < 0) continue;
5928
- if ((command.devGroup != null) && (docs[i].ids != null) && (docs[i].ids.indexOf(command.devGroup) == -1)) continue;
5929
-
5930
- // Fetch or create the user entry
5931
- var userEntry = userEntries[docs[i].userid];
5932
- if (userEntry == null) { userEntry = { userid: docs[i].userid, length: 0, bytesin: 0, bytesout: 0}; }
5933
- if (docs[i].bytesin) { userEntry.bytesin += docs[i].bytesin; }
5934
- if (docs[i].bytesout) { userEntry.bytesout += docs[i].bytesout; }
5935
-
5936
- // Session length
5937
- if (((docs[i].msgid >= 10) && (docs[i].msgid <= 12)) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[3] == 'number')) { userEntry.length += docs[i].msgArgs[3]; }
5938
- else if ((docs[i].msgid >= 122) && (docs[i].msgid <= 126) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[0] == 'number')) { userEntry.length += docs[i].msgArgs[0]; }
5939
-
5940
- // Set the user entry
5941
- userEntries[docs[i].userid] = userEntry;
5942
- }
5943
-
5944
- var userEntries2 = [];
5945
- for (var i in userEntries) { userEntries2.push(userEntries[i]); }
5946
- data.groups[0].entries = userEntries2;
5947
-
5948
- try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
5949
- });
5950
- }
5812
+ if (command.type == 1)
5813
+ remoteSessionReport(command, manageAllDeviceGroups, msgIdFilter);
5814
+ if (command.type == 2)
5815
+ trafficUsageReport(command, msgIdFilter);
5816
}
5817
5818
function serverCommandServerClearErrorLog(command) {
@@ -6823,6 +6688,145 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6688
6689
function csvClean(s) { return '\"' + s.split('\"').join('').split(',').join('').split('\r').join('').split('\n').join('') + '\"'; }
6690
6691
+ function remoteSessionReport(command, manageAllDeviceGroups, msgIdFilter) {
6692
+ // If we are not user administrator on this site, only search for events with our own user id.
6693
+ var ids = [user._id];
6694
+ if ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0) {
6695
+ if (command.devGroup != null) {
6696
+ ids = [ user._id, command.devGroup ];
6697
+ } else {
6698
+ if (manageAllDeviceGroups) { ids = ['*']; } else if (user.links) { for (var i in user.links) { ids.push(i); } }
6699
+ }
6700
+ }
6701
+
6702
+ // Get the events in the time range
6703
+ // MySQL or MariaDB query will ignore the MsgID filter.
6704
+ db.GetEventsTimeRange(ids, domain.id, msgIdFilter, new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
6705
+ if (err != null) return;
6706
+ var data = { groups: {} };
6707
+ var guestNamePresent = false;
6708
+
6709
+ // Columns
6710
+ if (command.groupBy == 1) {
6711
+ data.groupFormat = 'user';
6712
+ data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'meshid', title: "devgroup", format: 'mesh' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
6713
+ } else if (command.groupBy == 2) {
6714
+ data.groupFormat = 'nodemesh';
6715
+ data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'userid', title: "user", format: 'user' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
6716
+ } else if (command.groupBy == 3) {
6717
+ data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'meshid', title: "devgroup", format: 'mesh' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
6718
+ }
6719
+
6720
+ // Add traffic columns
6721
+ if (command.showTraffic) {
6722
+ data.columns.push({ id: 'bytesin', title: "bytesin", format: 'bytes', align: 'center', sumBy: 'protocol' });
6723
+ data.columns.push({ id: 'bytesout', title: "bytesout", format: 'bytes', align: 'center', sumBy: 'protocol' });
6724
+ }
6725
+
6726
+ // Rows
6727
+ for (var i in docs) {
6728
+ // If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
6729
+ if (msgIdFilter.indexOf(docs[i].msgid) < 0) continue;
6730
+ if ((command.devGroup != null) && (docs[i].ids != null) && (docs[i].ids.indexOf(command.devGroup) == -1)) continue;
6731
+
6732
+ var entry = { time: docs[i].time.valueOf() };
6733
+
6734
+ // UserID
6735
+ if (command.groupBy != 1) { entry.userid = docs[i].userid; }
6736
+ if (command.groupBy != 2) { entry.nodeid = docs[i].nodeid; }
6737
+ entry.protocol = docs[i].protocol;
6738
+
6739
+ // Device Group
6740
+ if (docs[i].ids != null) { for (var j in docs[i].ids) { if (docs[i].ids[j].startsWith('mesh/')) { entry.meshid = docs[i].ids[j]; } } }
6741
+
6742
+ // Add traffic data
6743
+ if (command.showTraffic) { entry.bytesin = docs[i].bytesin; entry.bytesout = docs[i].bytesout; }
6744
+
6745
+ // Add guest name if present
6746
+ if (docs[i].guestname != null) { entry.guestname = docs[i].guestname; guestNamePresent = true; }
6747
+
6748
+ // Session length
6749
+ if (((docs[i].msgid >= 10) && (docs[i].msgid <= 12)) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[3] == 'number')) { entry.length = docs[i].msgArgs[3]; }
6750
+ else if ((docs[i].msgid >= 122) && (docs[i].msgid <= 126) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[0] == 'number')) { entry.length = docs[i].msgArgs[0]; }
6751
+
6752
+ if (command.groupBy == 1) { // Add entry to per user
6753
+ if (data.groups[docs[i].userid] == null) { data.groups[docs[i].userid] = { entries: [] }; }
6754
+ data.groups[docs[i].userid].entries.push(entry);
6755
+ } else if (command.groupBy == 2) { // Add entry to per mesh+device
6756
+ if (entry.meshid != null) {
6757
+ var k = docs[i].nodeid + '/' + entry.meshid;
6758
+ if (data.groups[k] == null) { data.groups[k] = { entries: [] }; }
6759
+ data.groups[k].entries.push(entry);
6760
+ } else {
6761
+ if (data.groups[docs[i].nodeid] == null) { data.groups[docs[i].nodeid] = { entries: [] }; }
6762
+ data.groups[docs[i].nodeid].entries.push(entry);
6763
+ }
6764
+ } else if (command.groupBy == 3) { // Add entry to per day
6765
+ var day;
6766
+ if ((typeof command.l == 'string') && (typeof command.tz == 'string')) {
6767
+ day = new Date(docs[i].time).toLocaleDateString(command.l, { timeZone: command.tz });
6768
+ } else {
6769
+ day = docs[i].time; // TODO
6770
+ }
6771
+ if (data.groups[day] == null) { data.groups[day] = { entries: [] }; }
6772
+ data.groups[day].entries.push(entry);
6773
+ }
6774
+ }
6775
+
6776
+ // Remove guest column if not needed
6777
+ if (guestNamePresent == false) {
6778
+ if ((command.groupBy == 1) || (command.groupBy == 3)) {
6779
+ data.columns.splice(3, 1);
6780
+ } else if (command.groupBy == 2) {
6781
+ data.columns.splice(2, 1);
6782
+ }
6783
+ }
6784
+
6785
+ try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
6786
+ });
6787
+ }
6788
+
6789
+ function trafficUsageReport(command, msgIdFilter) {
6790
+ // If we are not user administrator on this site, only search for events with our own user id.
6791
+ var ids = [user._id]; // If we are nto user administrator, only count our own traffic.
6792
+ if ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0) { ids = ['*']; } // If user administrator, count traffic of all users.
6793
+
6794
+ // Get the events in the time range
6795
+ // MySQL or MariaDB query will ignore the MsgID filter.
6796
+ db.GetEventsTimeRange(ids, domain.id, msgIdFilter, new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
6797
+ if (err != null) return;
6798
+ var data = { groups: { 0: { entries: [] } } };
6799
+ data.columns = [{ id: 'userid', title: "user", format: 'user' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: true }, { id: 'bytesin', title: "bytesin", format: 'bytes', align: 'center', sumBy: true }, { id: 'bytesout', title: "bytesout", format: 'bytes', align: 'center', sumBy: true }];
6800
+ var userEntries = {};
6801
+
6802
+ // Sum all entry logs for each user
6803
+ for (var i in docs) {
6804
+ // If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
6805
+ if (msgIdFilter.indexOf(docs[i].msgid) < 0) continue;
6806
+ if ((command.devGroup != null) && (docs[i].ids != null) && (docs[i].ids.indexOf(command.devGroup) == -1)) continue;
6807
+
6808
+ // Fetch or create the user entry
6809
+ var userEntry = userEntries[docs[i].userid];
6810
+ if (userEntry == null) { userEntry = { userid: docs[i].userid, length: 0, bytesin: 0, bytesout: 0}; }
6811
+ if (docs[i].bytesin) { userEntry.bytesin += docs[i].bytesin; }
6812
+ if (docs[i].bytesout) { userEntry.bytesout += docs[i].bytesout; }
6813
+
6814
+ // Session length
6815
+ if (((docs[i].msgid >= 10) && (docs[i].msgid <= 12)) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[3] == 'number')) { userEntry.length += docs[i].msgArgs[3]; }
6816
+ else if ((docs[i].msgid >= 122) && (docs[i].msgid <= 126) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[0] == 'number')) { userEntry.length += docs[i].msgArgs[0]; }
6817
+
6818
+ // Set the user entry
6819
+ userEntries[docs[i].userid] = userEntry;
6820
+ }
6821
+
6822
+ var userEntries2 = [];
6823
+ for (var i in userEntries) { userEntries2.push(userEntries[i]); }
6824
+ data.groups[0].entries = userEntries2;
6825
+
6826
+ try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
6827
+ });
6828
+ }
6829
+
6830
// Return detailed information about an array of nodeid's
6831
function getDeviceDetailedInfo(nodeids, type, func) {
6832
if (nodeids == null) { getAllDeviceDetailedInfo(type, func); return; }