[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/master] fix: v3bwfile: Avoid statistics without data



commit 2924e440373341abfb1548c61db3608fb7eebfad
Author: juga0 <juga@xxxxxxxxxx>
Date:   Tue Aug 11 16:02:01 2020 +0000

    fix: v3bwfile: Avoid statistics without data
    
    If mean or median argument is empty, they throw an exception.
    This can happen when the scanner has stopped and the result is
    stored as successful without any downloads.
    
    Closes: #40012
---
 sbws/lib/scaling.py  | 10 +++++++---
 sbws/lib/v3bwfile.py | 14 ++++++++++----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/sbws/lib/scaling.py b/sbws/lib/scaling.py
index 0fc3f84..079f36b 100644
--- a/sbws/lib/scaling.py
+++ b/sbws/lib/scaling.py
@@ -14,6 +14,10 @@ def bw_filt(bw_measurements):
     It is the equivalent to Torflow's ``filt_sbw``.
     ``mu`` in this function is the equivalent to Torflow's ``sbw``.
     """
-    mu = mean(bw_measurements)
-    bws_gte_mean = filter(lambda bw: bw >= mu, bw_measurements)
-    return mean(bws_gte_mean)
+    mu = 1
+    if bw_measurements:
+        mu = mean(bw_measurements)
+    bws_gte_mean = list(filter(lambda bw: bw >= mu, bw_measurements))
+    if bws_gte_mean:
+        return mean(bws_gte_mean)
+    return 1
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py
index 13b202c..45563b6 100644
--- a/sbws/lib/v3bwfile.py
+++ b/sbws/lib/v3bwfile.py
@@ -869,13 +869,19 @@ class V3BWLine(object):
 
     @staticmethod
     def bw_median_from_results(results):
-        return max(round(median([dl['amount'] / dl['duration']
-                                 for r in results for dl in r.downloads])), 1)
+        bws = [dl['amount'] / dl['duration']
+               for r in results for dl in r.downloads]
+        if bws:
+            return max(round(median(bws)), 1)
+        return 1
 
     @staticmethod
     def bw_mean_from_results(results):
-        return max(round(mean([dl['amount'] / dl['duration']
-                               for r in results for dl in r.downloads])), 1)
+        bws = [dl['amount'] / dl['duration']
+               for r in results for dl in r.downloads]
+        if bws:
+            return max(round(mean(bws)), 1)
+        return 1
 
     @staticmethod
     def last_time_from_results(results):



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits