luci-mod-status: fix average calculations
authorJo-Philipp Wich <jo@mein.io>
Sat, 17 Nov 2018 13:54:43 +0000 (14:54 +0100)
committerJo-Philipp Wich <jo@mein.io>
Sat, 17 Nov 2018 13:57:08 +0000 (14:57 +0100)
Calculate the average over the actual data and not over the entire timeframe.

Fixes #2301
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-mod-status/luasrc/view/admin_status/bandwidth.htm
modules/luci-mod-status/luasrc/view/admin_status/connections.htm
modules/luci-mod-status/luasrc/view/admin_status/load.htm
modules/luci-mod-status/luasrc/view/admin_status/wireless.htm

index 3bb55f905448778343770531413f017e2e4e5faf..5cc661ad17b27a7bf10957e5bafa4f1cede42020 100644 (file)
@@ -1,5 +1,5 @@
 <%#
- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
+ Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io>
  Licensed to the public under the Apache License 2.0.
 -%>
 
@@ -34,7 +34,7 @@
        var step   = 5;
 
        var data_wanted = Math.floor(width / step);
-       var data_fill   = 0;
+       var data_fill   = 1;
        var data_stamp  = 0;
 
        var data_rx = [ ];
                                                        if (data[i][TIME] <= data_stamp)
                                                                continue;
 
+                                                       data_fill++;
+
                                                        /* normalize difference against time interval */
                                                        if (i > 0)
                                                        {
                                                /* cut off outdated entries */
                                                data_rx = data_rx.slice(data_rx.length - data_wanted, data_rx.length);
                                                data_tx = data_tx.slice(data_tx.length - data_wanted, data_tx.length);
+                                               data_fill = Math.min(data_fill, data_wanted);
 
                                                /* find peak */
                                                for (var i = 0; i < data_rx.length; i++)
                                                        data_tx_avg += data_tx[i];
                                                }
 
-                                               data_rx_avg = (data_rx_avg / Math.max(data_rx.length, 1));
-                                               data_tx_avg = (data_tx_avg / Math.max(data_tx.length, 1));
+                                               data_rx_avg = (data_rx_avg / data_fill);
+                                               data_tx_avg = (data_tx_avg / data_fill);
 
                                                var size = Math.floor(Math.log2(data_max)),
                                                    div = Math.pow(2, size - (size % 10)),
index 0a0db3be78a595cab0fa70614f9c072d2637e132..d9099aa0780930b576545ac309d2fbc662c5adc4 100644 (file)
@@ -1,5 +1,5 @@
 <%#
- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
+ Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io>
  Licensed to the public under the Apache License 2.0.
 -%>
 
@@ -19,7 +19,7 @@
        var step   = 5;
 
        var data_wanted = Math.floor(width / step);
-       var data_fill   = 0;
+       var data_fill   = 1;
        var data_stamp  = 0;
 
        var data_udp = [ ];
                                                        if (data[i][TIME] <= data_stamp)
                                                                continue;
 
+                                                       data_fill++;
+
                                                        data_udp.push(data[i][UDP]);
                                                        data_tcp.push(data[i][TCP]);
                                                        data_otr.push(data[i][OTHER]);
                                                }
 
                                                /* cut off outdated entries */
+                                               data_fill = Math.min(data_fill, data_wanted);
                                                data_udp = data_udp.slice(data_udp.length - data_wanted, data_udp.length);
                                                data_tcp = data_tcp.slice(data_tcp.length - data_wanted, data_tcp.length);
                                                data_otr = data_otr.slice(data_otr.length - data_wanted, data_otr.length);
                                                        data_tcp_peak = Math.max(data_tcp_peak, data_tcp[i]);
                                                        data_otr_peak = Math.max(data_otr_peak, data_otr[i]);
 
-                                                       if (i > 0)
-                                                       {
-                                                               data_udp_avg = (data_udp_avg + data_udp[i]) / 2;
-                                                               data_tcp_avg = (data_tcp_avg + data_tcp[i]) / 2;
-                                                               data_otr_avg = (data_otr_avg + data_otr[i]) / 2;
-                                                       }
-                                                       else
-                                                       {
-                                                               data_udp_avg = data_udp[i];
-                                                               data_tcp_avg = data_tcp[i];
-                                                               data_otr_avg = data_otr[i];
-                                                       }
+                                                       data_udp_avg += data_udp[i];
+                                                       data_tcp_avg += data_tcp[i];
+                                                       data_otr_avg += data_otr[i];
                                                }
 
+                                               data_udp_avg = data_udp_avg / data_fill;
+                                               data_tcp_avg = data_tcp_avg / data_fill;
+                                               data_otr_avg = data_otr_avg / data_fill;
+
                                                /* remember current timestamp, calculate horizontal scale */
                                                data_stamp = data[data.length-1][TIME];
                                                data_scale = height / (data_max * 1.1);
index bced06fa22cc88b73ecbd3b61a840451bef5d52e..d31d3406219b527f856aa044218b79fa5e7f8917 100644 (file)
@@ -1,5 +1,5 @@
 <%#
- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
+ Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io>
  Licensed to the public under the Apache License 2.0.
 -%>
 
@@ -19,7 +19,7 @@
        var step   = 5;
 
        var data_wanted = Math.floor(width / step);
-       var data_fill   = 0;
+       var data_fill   = 1;
        var data_stamp  = 0;
 
        var data_01  = [ ];
                                                        if (data[i][TIME] <= data_stamp)
                                                                continue;
 
+                                                       data_fill++;
+
                                                        data_01.push(data[i][L01]);
                                                        data_05.push(data[i][L05]);
                                                        data_15.push(data[i][L15]);
                                                }
 
                                                /* cut off outdated entries */
+                                               data_fill = Math.min(data_fill, data_wanted);
                                                data_01 = data_01.slice(data_01.length - data_wanted, data_01.length);
                                                data_05 = data_05.slice(data_05.length - data_wanted, data_05.length);
                                                data_15 = data_15.slice(data_15.length - data_wanted, data_15.length);
                                                        data_05_peak = Math.max(data_05_peak, data_05[i]);
                                                        data_15_peak = Math.max(data_15_peak, data_15[i]);
 
-                                                       if (i > 0)
-                                                       {
-                                                               data_01_avg = (data_01_avg + data_01[i]) / 2;
-                                                               data_05_avg = (data_05_avg + data_05[i]) / 2;
-                                                               data_15_avg = (data_15_avg + data_15[i]) / 2;
-                                                       }
-                                                       else
-                                                       {
-                                                               data_01_avg = data_01[i];
-                                                               data_05_avg = data_05[i];
-                                                               data_15_avg = data_15[i];
-                                                       }
+                                                       data_01_avg += data_01[i];
+                                                       data_05_avg += data_05[i];
+                                                       data_15_avg += data_15[i];
                                                }
 
+                                               data_01_avg = data_01_avg / data_fill;
+                                               data_05_avg = data_05_avg / data_fill;
+                                               data_15_avg = data_15_avg / data_fill;
+
                                                /* remember current timestamp, calculate horizontal scale */
                                                data_stamp = data[data.length-1][TIME];
                                                data_scale = height / (data_max * 1.1);
index 03ea2bef01c4a8fbd80eae520b5551bd8d0d6a98..5ac2eb462dc7d76c4a665b2a4d100373001851d7 100644 (file)
@@ -1,5 +1,5 @@
 <%#
- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
+ Copyright 2011-2018 Jo-Philipp Wich <jo@mein.io>
  Licensed to the public under the Apache License 2.0.
 -%>
 
@@ -33,7 +33,7 @@
        var step   = 5;
 
        var data_wanted = Math.floor(width / step);
-       var data_fill   = 0;
+       var data_fill   = 1;
        var data_stamp  = 0;
 
        var data_rssi = [ ];
                                                        if (data[i][TIME] <= data_stamp)
                                                                continue;
 
+                                                       data_fill++;
+
                                                        data_rssi.push(data[i][RSSI] - noise_floor);
                                                        data_noise.push(data[i][NOISE] - noise_floor);
                                                        data_rate.push(Math.floor(data[i][RATE] / 1000));
                                                }
 
                                                /* cut off outdated entries */
+                                               data_fill = Math.min(data_fill, data_wanted);
                                                data_rssi = data_rssi.slice(data_rssi.length - data_wanted, data_rssi.length);
                                                data_noise = data_noise.slice(data_noise.length - data_wanted, data_noise.length);
                                                data_rate = data_rate.slice(data_rate.length - data_wanted, data_rate.length);
                                                        data_noise_peak = Math.max(data_noise_peak, data_noise[i]);
                                                        data_rate_peak = Math.max(data_rate_peak, data_rate[i]);
 
-                                                       if (i > 0)
-                                                       {
-                                                               data_rssi_avg = (data_rssi_avg + data_rssi[i]) / 2;
-                                                               data_noise_avg = (data_noise_avg + data_noise[i]) / 2;
-                                                               data_rate_avg = (data_rate_avg + data_rate[i]) / 2;
-                                                       }
-                                                       else
-                                                       {
-                                                               data_rssi_avg = data_rssi[i];
-                                                               data_noise_avg = data_noise[i];
-                                                               data_rate_avg = data_rate[i];
-                                                       }
+                                                       data_rssi_avg += data_rssi[i];
+                                                       data_noise_avg += data_noise[i];
+                                                       data_rate_avg += data_rate[i];
                                                }
 
+                                               data_rssi_avg = data_rssi_avg / data_fill;
+                                               data_noise_avg = data_noise_avg / data_fill;
+                                               data_rate_avg = data_rate_avg / data_fill;
+
                                                /* remember current timestamp, calculate horizontal scale */
                                                data_stamp = data[data.length-1][TIME];
                                                data_scale = (height / (data_max * 1.1)).toFixed(1);