Improved report totals.

Ylian Saint-Hilaire committed Sep 9, 2021 at 11:38 UTC 333d7813b9478cf3528d4a368fd8ff2d28ff1fd6
2 files changed +43 -15
meshuser.js
+3 -3
@@ -5428,12 +5428,12 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5428 // Columns
5429 if (command.groupBy == 1) {
5430 data.groupFormat = 'user';
5431 - data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: "nodeid", title: "device", format: "node" }, { id: "protocol", title: "session", format: "protocol", align: "center" }, { id: "length", title: "length", format: "seconds", align: "center" } ];
5431 + data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
5432 } else if (command.groupBy == 2) {
5433 data.groupFormat = 'node';
5434 - data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: "userid", title: "user", format: "user" }, { id: "protocol", title: "session", format: "protocol", align: "center" }, { id: "length", title: "length", format: "seconds", align: "center" } ];
5434 + data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
5435 } else if (command.groupBy == 3) {
5436 - data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: "nodeid", title: "device", format: "node" }, { id: "userid", title: "user", format: "user" }, { id: "protocol", title: "session", format: "protocol", align: "center" }, { id: "length", title: "length", format: "seconds", align:"center" } ];
5436 + data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ];
5437 }
5438
5439 // Rows
views/default.handlebars
+40 -12
@@ -14965,6 +14965,9 @@
14965 //console.log('renderReport', r);
14966 var colTranslation = { time: "Time", device: "Device", session: "Session", user: "User", length: "Length" }
14967 var x = '<table style=width:100%>';
14968 + var sumByCol = null; // Indicate by what colum we sum by
14969 + var sumByValues = []; // Indicate by what values we sum by
14970 +
14971 x += '<tr>'
14972 for (var i in r.columns) {
14973 var coltitle;
@@ -14987,9 +14990,16 @@
14990 var style = '';
14991 if (r.columns[k].align) { style = 'text-align:' + EscapeHtml(r.columns[k].align); }
14992 if (e[r.columns[k].id] != null) { x += '<td style="' + style + '">' + renderReportFormat(e[r.columns[k].id], r.columns[k].format) + '</td>'; } else { x += '<td></td>'; }
14990 - if (r.columns[k].format == 'seconds') {
14991 - var v = e[r.columns[k].id];
14992 - if (v != null) { if (r.columns[k].subtotal == null) { r.columns[k].subtotal = v; r.columns[k].total = v; } else { r.columns[k].subtotal += v; r.columns[k].total += v; } }
14993 + if (r.columns[k].sumBy != null) {
14994 + var v1 = e[r.columns[k].sumBy];
14995 + var v2 = e[r.columns[k].id];
14996 + sumByCol = r.columns[k].sumBy;
14997 + if (v2 != null) {
14998 + if (sumByValues.indexOf(v1) == -1) { sumByValues.push(v1); }
14999 + if (r.columns[k].subtotals == null) { r.columns[k].subtotals = {}; r.columns[k].totals = {}; }
15000 + if (r.columns[k].subtotals[v1] == null) { r.columns[k].subtotals[v1] = v2; } else { r.columns[k].subtotals[v1] += v2; }
15001 + if (r.columns[k].totals[v1] == null) { r.columns[k].totals[v1] = v2; } else { r.columns[k].totals[v1] += v2; }
15002 + }
15003 }
15004 }
15005 x += '</tr>'
@@ -14997,17 +15007,35 @@
15007 }
15008
15009 // Display totals
15000 - x += '<tr>'
15001 - for (var i in r.columns) {
15002 - if (r.columns[i].total != null) {
15003 - var style = '';
15004 - if (r.columns[k].align) { style = 'text-align:' + EscapeHtml(r.columns[k].align); }
15005 - x += '<td style="border-top:1pt solid black;color:#777;' + style + '">' + renderReportFormat(r.columns[i].total, r.columns[i].format); + '</td>';
15006 - } else {
15007 - x += '<td></td>';
15010 + if (sumByCol != null) {
15011 + var sumByColNum = -1;
15012 + for (var i in r.columns) { if (r.columns[i].id == sumByCol) { sumByColNum = i; } }
15013 + if (sumByColNum >= 0) {
15014 + sumByValues.sort();
15015 + var firstRow = true;
15016 + x += '<tr style=height:8px></tr>'
15017 + for (var j in sumByValues) {
15018 + x += '<tr>'
15019 + for (var i in r.columns) {
15020 + if (i == sumByColNum) {
15021 + var style = '';
15022 + if (r.columns[k].align) { style += ';text-align:' + EscapeHtml(r.columns[k].align); }
15023 + if (firstRow) { style += ';border-top:1pt solid #777'; }
15024 + x += '<td style="color:#777' + style + '">' + renderReportFormat(sumByValues[j], r.columns[i].format); + '</td>';
15025 + } else if (r.columns[i].totals != null) {
15026 + var style = '';
15027 + if (r.columns[k].align) { style += ';text-align:' + EscapeHtml(r.columns[k].align); }
15028 + if (firstRow) { style += ';border-top:1pt solid #777'; }
15029 + x += '<td style="color:#777' + style + '">' + renderReportFormat(r.columns[i].totals[sumByValues[j]], r.columns[i].format); + '</td>';
15030 + } else {
15031 + x += '<td></td>';
15032 + }
15033 + }
15034 + x += '</tr>'
15035 + firstRow = false;
15036 + }
15037 }
15038 }
15010 - x += '</tr>'
15039
15040 x += '</table>';
15041 QH('p60report', x);