From 333d7813b9478cf3528d4a368fd8ff2d28ff1fd6 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Thu, 9 Sep 2021 11:38:26 -0700 Subject: [PATCH] Improved report totals. --- meshuser.js | 6 ++--- views/default.handlebars | 52 ++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/meshuser.js b/meshuser.js index efa47b62..aef4b56f 100644 --- a/meshuser.js +++ b/meshuser.js @@ -5428,12 +5428,12 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // Columns if (command.groupBy == 1) { data.groupFormat = 'user'; - 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" } ]; + 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' } ]; } else if (command.groupBy == 2) { data.groupFormat = 'node'; - 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" } ]; + 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' } ]; } else if (command.groupBy == 3) { - 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" } ]; + 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' } ]; } // Rows diff --git a/views/default.handlebars b/views/default.handlebars index d69f61d5..d317128c 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -14965,6 +14965,9 @@ //console.log('renderReport', r); var colTranslation = { time: "Time", device: "Device", session: "Session", user: "User", length: "Length" } var x = ''; + var sumByCol = null; // Indicate by what colum we sum by + var sumByValues = []; // Indicate by what values we sum by + x += '' for (var i in r.columns) { var coltitle; @@ -14987,9 +14990,16 @@ var style = ''; if (r.columns[k].align) { style = 'text-align:' + EscapeHtml(r.columns[k].align); } if (e[r.columns[k].id] != null) { x += ''; } else { x += ''; } - if (r.columns[k].format == 'seconds') { - var v = e[r.columns[k].id]; - 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; } } + if (r.columns[k].sumBy != null) { + var v1 = e[r.columns[k].sumBy]; + var v2 = e[r.columns[k].id]; + sumByCol = r.columns[k].sumBy; + if (v2 != null) { + if (sumByValues.indexOf(v1) == -1) { sumByValues.push(v1); } + if (r.columns[k].subtotals == null) { r.columns[k].subtotals = {}; r.columns[k].totals = {}; } + if (r.columns[k].subtotals[v1] == null) { r.columns[k].subtotals[v1] = v2; } else { r.columns[k].subtotals[v1] += v2; } + if (r.columns[k].totals[v1] == null) { r.columns[k].totals[v1] = v2; } else { r.columns[k].totals[v1] += v2; } + } } } x += '' @@ -14997,17 +15007,35 @@ } // Display totals - x += '' - for (var i in r.columns) { - if (r.columns[i].total != null) { - var style = ''; - if (r.columns[k].align) { style = 'text-align:' + EscapeHtml(r.columns[k].align); } - x += ''; - } else { - x += ''; + if (sumByCol != null) { + var sumByColNum = -1; + for (var i in r.columns) { if (r.columns[i].id == sumByCol) { sumByColNum = i; } } + if (sumByColNum >= 0) { + sumByValues.sort(); + var firstRow = true; + x += '' + for (var j in sumByValues) { + x += '' + for (var i in r.columns) { + if (i == sumByColNum) { + var style = ''; + if (r.columns[k].align) { style += ';text-align:' + EscapeHtml(r.columns[k].align); } + if (firstRow) { style += ';border-top:1pt solid #777'; } + x += ''; + } else if (r.columns[i].totals != null) { + var style = ''; + if (r.columns[k].align) { style += ';text-align:' + EscapeHtml(r.columns[k].align); } + if (firstRow) { style += ';border-top:1pt solid #777'; } + x += ''; + } else { + x += ''; + } + } + x += '' + firstRow = false; + } } } - x += '' x += '
' + renderReportFormat(e[r.columns[k].id], r.columns[k].format) + '
' + renderReportFormat(r.columns[i].total, r.columns[i].format); + '
' + renderReportFormat(sumByValues[j], r.columns[i].format); + '' + renderReportFormat(r.columns[i].totals[sumByValues[j]], r.columns[i].format); + '
'; QH('p60report', x);