mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-11-23 06:34:54 +03:00
Added user bandwidth report.
This commit is contained in:
parent
af3e9b8b4a
commit
508f294858
60
meshuser.js
60
meshuser.js
@ -5449,7 +5449,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
||||
}
|
||||
case 'report': {
|
||||
// Report request. Validate the input
|
||||
if (common.validateInt(command.type, 1, 1) == false) break; // Validate type
|
||||
if (common.validateInt(command.type, 1, 2) == false) break; // Validate type
|
||||
if (common.validateInt(command.groupBy, 1, 3) == false) break; // Validate groupBy: 1 = User, 2 = Device, 3 = Day
|
||||
if ((typeof command.start != 'number') || (typeof command.end != 'number') || (command.start >= command.end)) break; // Validate start and end time
|
||||
const manageAllDeviceGroups = ((user.siteadmin == 0xFFFFFFFF) && (parent.parent.config.settings.managealldevicegroups.indexOf(user._id) >= 0));
|
||||
@ -5471,6 +5471,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
||||
db.GetEventsTimeRange(ids, domain.id, [5, 10, 11, 12, 122, 123, 124, 125, 126], new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
|
||||
if (err != null) return;
|
||||
var data = { groups: {} };
|
||||
var guestNamePresent = false;
|
||||
|
||||
// Columns
|
||||
if (command.groupBy == 1) {
|
||||
@ -5489,9 +5490,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
||||
data.columns.push({ id: 'bytesout', title: "bytesout", format: 'bytes', align: 'center', sumBy: 'protocol' });
|
||||
}
|
||||
|
||||
// Remote guest column is not needed
|
||||
if (!command.showGuestName) { data.columns.splice(2, 1); }
|
||||
|
||||
// Rows
|
||||
for (var i in docs) {
|
||||
// If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
|
||||
@ -5509,7 +5507,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
||||
if (command.showTraffic) { entry.bytesin = docs[i].bytesin; entry.bytesout = docs[i].bytesout; }
|
||||
|
||||
// Add guest name if present
|
||||
if (docs[i].guestname != null) { entry.guestname = docs[i].guestname; }
|
||||
if (docs[i].guestname != null) { entry.guestname = docs[i].guestname; guestNamePresent = true; }
|
||||
|
||||
// Session length
|
||||
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]; }
|
||||
@ -5533,9 +5531,61 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Remove guest column if not needed
|
||||
if (guestNamePresent == false) { data.columns.splice(2, 1); }
|
||||
|
||||
try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
|
||||
});
|
||||
}
|
||||
|
||||
if (command.type == 2) { // This is the user bandwidth usage report.
|
||||
// If we are not user administrator on this site, only search for events with our own user id.
|
||||
var ids = [user._id];
|
||||
if ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0) {
|
||||
if (command.devGroup != null) {
|
||||
ids = [user._id, command.devGroup];
|
||||
} else {
|
||||
if (manageAllDeviceGroups) { ids = ['*']; } else if (user.links) { for (var i in user.links) { ids.push(i); } }
|
||||
}
|
||||
}
|
||||
|
||||
// Get the events in the time range
|
||||
// MySQL or MariaDB query will ignore the MsgID filter.
|
||||
db.GetEventsTimeRange(ids, domain.id, [5, 10, 11, 12, 122, 123, 124, 125, 126], new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
|
||||
if (err != null) return;
|
||||
var data = { groups: { 0: { entries: [] } } };
|
||||
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 }];
|
||||
var userEntries = {};
|
||||
|
||||
// Sum all entry logs for each user
|
||||
for (var i in docs) {
|
||||
// If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
|
||||
if ((docs[i].msgid != 5) && (docs[i].msgid != 10) && (docs[i].msgid != 11) && (docs[i].msgid != 12) && ((docs[i].msgid < 122) && (docs[i].msgid > 126))) continue;
|
||||
if ((command.devGroup != null) && (docs[i].ids != null) && (docs[i].ids.indexOf(command.devGroup) == -1)) continue;
|
||||
|
||||
// Fetch or create the user entry
|
||||
var userEntry = userEntries[docs[i].userid];
|
||||
if (userEntry == null) { userEntry = { userid: docs[i].userid, length: 0, bytesin: 0, bytesout: 0}; }
|
||||
if (docs[i].bytesin) { userEntry.bytesin += docs[i].bytesin; }
|
||||
if (docs[i].bytesout) { userEntry.bytesout += docs[i].bytesout; }
|
||||
|
||||
// Session length
|
||||
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]; }
|
||||
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]; }
|
||||
|
||||
// Set the user entry
|
||||
userEntries[docs[i].userid] = userEntry;
|
||||
}
|
||||
|
||||
var userEntries2 = [];
|
||||
for (var i in userEntries) { userEntries2.push(userEntries[i]); }
|
||||
data.groups[0].entries = userEntries2;
|
||||
|
||||
try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
|
@ -14925,19 +14925,23 @@
|
||||
if (xxdialogMode) return;
|
||||
var y = '', x = '', settings = JSON.parse(getstore('_ReportSettings', '{}'));
|
||||
|
||||
var options = { 1 : "Remote Sessions" }
|
||||
var options = { 1 : "Remote Sessions", 2 : "User Bandwidth Usage" }
|
||||
for (var i in options) { y += '<option value=' + i + ((settings.type == i)?' selected':'') + '>' + options[i] + '</option>'; }
|
||||
x += addHtmlValue("Type", '<select id=d2reportType style=float:right;width:250px onchange=generateReportDialogValidate()>' + y + '</select>');
|
||||
|
||||
x += '<div id=d2GroupByDiv style=display:none>';
|
||||
y = '';
|
||||
var options = { 1 : "User", 2: "Device", 3: "Day" }
|
||||
for (var i in options) { y += '<option value=' + i + ((settings.groupBy == i)?' selected':'') + '>' + options[i] + '</option>'; }
|
||||
x += addHtmlValue("Group by", '<select id=d2groupBy style=float:right;width:250px onchange=generateReportDialogValidate()>' + y + '</select>');
|
||||
x += '</div>';
|
||||
|
||||
x += '<div id=d2DeviceGroupDiv style=display:none>';
|
||||
y = '<option value=0' + ((settings.devGroup == 0)?' selected':'') + '>' + "All" + '</option>';
|
||||
var omeshs = getOrderedList(meshes, 'name');
|
||||
for (var i in omeshs) { y += '<option value=' + encodeURIComponentEx(omeshs[i]._id) + ((settings.devGroup == omeshs[i]._id)?' selected':'') + '>' + EscapeHtml(omeshs[i].name) + '</option>'; }
|
||||
x += addHtmlValue("Device Group", '<select onchange=generateReportDialogValidate() id=d2groupId style=float:right;width:250px>' + y + '</select>');
|
||||
x += '</div>';
|
||||
|
||||
y = '';
|
||||
if (settings.timeRange == null) { settings.timeRange = 1; }
|
||||
@ -14951,7 +14955,6 @@
|
||||
|
||||
x += '<div id=d2showTrafficDiv style=display:none>';
|
||||
x += addHtmlValue("", '<div style=width:250px><label><input type=checkbox id=d2showTraffic ' + ((settings.showTraffic) ? 'checked' : '') + '>' + "Show Traffic" + '</label><div>');
|
||||
x += addHtmlValue("", '<div style=width:250px><label><input type=checkbox id=d2showGuestName ' + ((settings.showGuestName) ? 'checked' : '') + '>' + "Show Guest Name" + '</label><div>');
|
||||
x += '</div>';
|
||||
|
||||
setDialogMode(2, "Generate Report", 3, generateReportDialogEx, x);
|
||||
@ -14965,6 +14968,8 @@
|
||||
|
||||
function generateReportDialogValidate() {
|
||||
QV('d2timeRangeDiv', Q('d2timeRange').value == 0);
|
||||
QV('d2GroupByDiv', Q('d2reportType').value == 1);
|
||||
QV('d2DeviceGroupDiv', Q('d2reportType').value == 1);
|
||||
QV('d2showTrafficDiv', Q('d2reportType').value == 1);
|
||||
}
|
||||
|
||||
@ -14982,8 +14987,8 @@
|
||||
try { tz = Intl.DateTimeFormat().resolvedOptions().timeZone; } catch (ex) {}
|
||||
var devGroup = decodeURIComponent(Q('d2groupId').value);
|
||||
if (devGroup == 0) { devGroup = null; }
|
||||
putstore('_ReportSettings', JSON.stringify({ type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), timeRange: parseInt(Q('d2timeRange').value), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked, showGuestName: Q('d2showGuestName').checked }));
|
||||
meshserver.send({ action: 'report', type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), start: start, end: end, tz: tz, tf: new Date().getTimezoneOffset(), l: getLang(), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked, showGuestName: Q('d2showGuestName').checked });
|
||||
putstore('_ReportSettings', JSON.stringify({ type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), timeRange: parseInt(Q('d2timeRange').value), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked }));
|
||||
meshserver.send({ action: 'report', type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), start: start, end: end, tz: tz, tf: new Date().getTimezoneOffset(), l: getLang(), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked });
|
||||
}
|
||||
|
||||
function renderReport(r) {
|
||||
@ -15014,12 +15019,12 @@
|
||||
for (var i in r.groups) {
|
||||
x += '<tr style=height:8px></tr>'
|
||||
x += '<tr><td colspan=' + r.columns.length + ' style="border-bottom:1pt solid black"><b>'
|
||||
x += renderReportFormat(i, r.groupFormat);
|
||||
if (i != 0) { x += renderReportFormat(i, r.groupFormat); }
|
||||
x += '</b></td></tr>'
|
||||
for (var j in r.groups[i].entries) {
|
||||
var e = r.groups[i].entries[j];
|
||||
x += '<tr>'
|
||||
reportCSV += '"' + csvClean(renderReportFormatCSV(i, r.groupFormat)) + '"';
|
||||
if (i != 0) { reportCSV += '"' + csvClean(renderReportFormatCSV(i, r.groupFormat)) + '"'; } else { reportCSV += '""'; }
|
||||
for (var k in r.columns) {
|
||||
var style = '';
|
||||
if (r.columns[k].align) { style = 'text-align:' + EscapeHtml(r.columns[k].align); }
|
||||
@ -15032,6 +15037,7 @@
|
||||
}
|
||||
if (r.columns[k].sumBy != null) {
|
||||
var v1 = e[r.columns[k].sumBy];
|
||||
if (r.columns[k].sumBy === true) { v1 = true; }
|
||||
var v2 = e[r.columns[k].id];
|
||||
sumByCol = r.columns[k].sumBy;
|
||||
if (v2 != null) {
|
||||
@ -15050,7 +15056,7 @@
|
||||
// Display totals
|
||||
if (sumByCol != null) {
|
||||
var sumByColNum = -1;
|
||||
for (var i in r.columns) { if (r.columns[i].id == sumByCol) { sumByColNum = i; } }
|
||||
if (sumByCol === true) { sumByColNum = 99999; } else { for (var i in r.columns) { if (r.columns[i].id == sumByCol) { sumByColNum = i; } } }
|
||||
if (sumByColNum >= 0) {
|
||||
sumByValues.sort();
|
||||
var firstRow = true;
|
||||
|
Loading…
Reference in New Issue
Block a user