From 53c80e9eff864ec1032c389b1cb48c79c08290ee Mon Sep 17 00:00:00 2001 From: Dan Berger Date: Mon, 29 May 2017 11:10:27 -0700 Subject: [PATCH 1/3] add option to group by column 0 (hostname/mac address) useful to undertand usage for multi-homed and/or dual-stack hosts --- luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js | 42 ++++++++++++++++++++ luci-wrtbwmon/luasrc/view/wrtbwmon.htm | 4 ++ 2 files changed, 46 insertions(+) diff --git a/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js b/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js index c4b81ed..bbdda63 100644 --- a/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js +++ b/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js @@ -1,6 +1,7 @@ // interval in seconds var scheduleTimeout, updateTimeout, isScheduled = true, interval = 5; var sortedColumn = 7, sortedEltId = "thTotal", sortDirection = "desc"; +var groupByHost = false; (function () { var oldDate, oldValues = []; @@ -65,6 +66,43 @@ var sortedColumn = 7, sortedEltId = "thTotal", sortDirection = "desc"; } }); + // we've already sorted by the desired column, now optionally group by hostname (or MAC address) + if (groupByHost && sortedColumn != 0) { + var prevHost = 0; + var curHost = 0; + var insertAt = 1; + while (curHost < data.length && insertAt < data.length) { + // grab the current hostname/mac, and walk the data looking for rows with the same host/mac + var hostName = data[curHost][1][0].toLowerCase(); + for (var k = insertAt; k < data.length; k++) { + if (data[k][1][0].toLowerCase() == hostName) { + // this is another row for the same host, group it with any other rows for this host + data.splice(insertAt, 0, data.splice(k, 1)[0]); + insertAt++; + } + } + // if we found more than one row for the host, add a subtotal row + if (insertAt > curHost + 1) { + var hostTotals = [0, 0, 0, 0, 0]; + for (var i = curHost; i < insertAt; i++) { + for (var j = 0; j < hostTotals.length; j++) { + hostTotals[j] += data[i][1][3 + j]; + } + } + var hostTotalRow = '' + data[curHost][1][0] + ''; + for (var m = 0; m < hostTotals.length; m++) { + var t = hostTotals[m]; + hostTotalRow += '' + getSize(t) + (m < 2 ? '/s' : '') + '' + } + hostTotalRow += ''; + data.splice(insertAt, 0, [hostTotalRow]); + } + prevHost = curHost; + curHost = insertAt+1; + insertAt = curHost+1; + } + } + // display data var result = '\ Client\ @@ -222,6 +260,10 @@ var sortedColumn = 7, sortedEltId = "thTotal", sortDirection = "desc"; } }); + document.getElementById('groupByHost').addEventListener('change', function () { + groupByHost = !groupByHost; + }); + function stopSchedule() { window.clearTimeout(scheduleTimeout); window.clearTimeout(updateTimeout); diff --git a/luci-wrtbwmon/luasrc/view/wrtbwmon.htm b/luci-wrtbwmon/luasrc/view/wrtbwmon.htm index 40374f2..2ffd164 100644 --- a/luci-wrtbwmon/luasrc/view/wrtbwmon.htm +++ b/luci-wrtbwmon/luasrc/view/wrtbwmon.htm @@ -24,6 +24,10 @@

Network Usage:

+

From 9e3499cad4dd1168b7731d67386c2382c492b501 Mon Sep 17 00:00:00 2001 From: Dan Berger Date: Tue, 30 May 2017 11:11:00 -0700 Subject: [PATCH 2/3] implement per-host-aggregates vs. host grouping keeping the display properly sorted when grouping hosts was inefficient for event moderate numbers of hosts. instead, just optionally compute per-host aggregate rows and include them in the normal sort/display logic. --- luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js | 69 ++++++++++---------- luci-wrtbwmon/luasrc/view/wrtbwmon.htm | 9 ++- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js b/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js index bbdda63..f0d9582 100644 --- a/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js +++ b/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js @@ -1,7 +1,7 @@ // interval in seconds var scheduleTimeout, updateTimeout, isScheduled = true, interval = 5; var sortedColumn = 7, sortedEltId = "thTotal", sortDirection = "desc"; -var groupByHost = false; +var perHostTotals = false, showPerHostTotalsOnly = false; (function () { var oldDate, oldValues = []; @@ -52,57 +52,55 @@ var groupByHost = false; } } - data.sort(function (x, y) { - var a = x[1][sortedColumn]; - var b = y[1][sortedColumn]; - if (sortDirection == "desc") { - if (a < b) return 1; - if (a > b) return -1; - return 0; - } else { - if (a > b) return 1; - if (a < b) return -1; - return 0; - } - }); - - // we've already sorted by the desired column, now optionally group by hostname (or MAC address) - if (groupByHost && sortedColumn != 0) { - var prevHost = 0; - var curHost = 0; - var insertAt = 1; + // aggregate (sub-total) by hostname (or MAC address) after the global totals are computed, before sort and display + if (perHostTotals) { + var curHost = 0, insertAt = 1; while (curHost < data.length && insertAt < data.length) { // grab the current hostname/mac, and walk the data looking for rows with the same host/mac var hostName = data[curHost][1][0].toLowerCase(); - for (var k = insertAt; k < data.length; k++) { + for (var k = curHost+1; k < data.length; k++) { if (data[k][1][0].toLowerCase() == hostName) { // this is another row for the same host, group it with any other rows for this host data.splice(insertAt, 0, data.splice(k, 1)[0]); insertAt++; } } + // if we found more than one row for the host, add a subtotal row - if (insertAt > curHost + 1) { - var hostTotals = [0, 0, 0, 0, 0]; - for (var i = curHost; i < insertAt; i++) { - for (var j = 0; j < hostTotals.length; j++) { - hostTotals[j] += data[i][1][3 + j]; + if (insertAt > curHost+1) { + var hostTotals = [data[curHost][1][0], '', '', 0, 0, 0, 0, 0]; + for (var i = curHost; i < insertAt && i < data.length; i++) { + for (var j = 3; j < hostTotals.length; j++) { + hostTotals[j] += data[i][1][j]; } } - var hostTotalRow = ''; - for (var m = 0; m < hostTotals.length; m++) { + var hostTotalRow = ''; + for (var m = 3; m < hostTotals.length; m++) { var t = hostTotals[m]; - hostTotalRow += '' + hostTotalRow += '' } hostTotalRow += ''; - data.splice(insertAt, 0, [hostTotalRow]); + data.splice(insertAt, 0, [hostTotalRow, hostTotals]); } - prevHost = curHost; curHost = insertAt+1; insertAt = curHost+1; } } + data.sort(function (x, y) { + var a = x[1][sortedColumn]; + var b = y[1][sortedColumn]; + if (sortDirection == "desc") { + if (a < b) return 1; + if (a > b) return -1; + return 0; + } else { + if (a > b) return 1; + if (a < b) return -1; + return 0; + } + }); + // display data var result = '\ \ @@ -260,10 +258,15 @@ var groupByHost = false; } }); - document.getElementById('groupByHost').addEventListener('change', function () { - groupByHost = !groupByHost; + document.getElementById('perHostTotals').addEventListener('change', function () { + perHostTotals = !perHostTotals; }); + //document.getElementById('showPerHostTotalsOnly').addEventListener('change', function () { + // showPerHostTotalsOnly = !showPerHostTotalsOnly; + //}); + + function stopSchedule() { window.clearTimeout(scheduleTimeout); window.clearTimeout(updateTimeout); diff --git a/luci-wrtbwmon/luasrc/view/wrtbwmon.htm b/luci-wrtbwmon/luasrc/view/wrtbwmon.htm index 2ffd164..e933321 100644 --- a/luci-wrtbwmon/luasrc/view/wrtbwmon.htm +++ b/luci-wrtbwmon/luasrc/view/wrtbwmon.htm @@ -24,10 +24,15 @@

Network Usage:

+
+

' + data[curHost][1][0] + '
' + data[curHost][1][0] + '
(host total)
' + getSize(t) + (m < 2 ? '/s' : '') + '' + getSize(t) + (m < 5 ? '/s' : '') + '
Client
From e2d779a09697d067fb47f78e951ee02acdb55bfa Mon Sep 17 00:00:00 2001 From: Dan Berger Date: Wed, 31 May 2017 12:26:58 -0700 Subject: [PATCH 3/3] fix off-by-one bug --- luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js b/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js index f0d9582..5c28789 100644 --- a/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js +++ b/luci-wrtbwmon/htdocs/luci-static/wrtbwmon.js @@ -82,7 +82,7 @@ var perHostTotals = false, showPerHostTotalsOnly = false; hostTotalRow += ''; data.splice(insertAt, 0, [hostTotalRow, hostTotals]); } - curHost = insertAt+1; + curHost = insertAt; insertAt = curHost+1; } }