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

juga at torproject.org juga at torproject.org
Wed Aug 26 13:51:33 UTC 2020


commit 2924e440373341abfb1548c61db3608fb7eebfad
Author: juga0 <juga at riseup.net>
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):





More information about the tor-commits mailing list