[tor-commits] [sbws/master] Trim bandwidth results for relays that change IP

pastly at torproject.org pastly at torproject.org
Tue Jun 26 15:36:49 UTC 2018


commit 4e7bc3e5b3f4f17d9a5848f21176f4c60b96c098
Author: juga0 <juga at riseup.net>
Date:   Mon Jun 18 13:22:43 2018 +0000

    Trim bandwidth results for relays that change IP
---
 sbws/lib/resultdump.py | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/sbws/lib/resultdump.py b/sbws/lib/resultdump.py
index 32fd1cd..d64d843 100644
--- a/sbws/lib/resultdump.py
+++ b/sbws/lib/resultdump.py
@@ -82,7 +82,44 @@ def trim_results(fresh_days, result_dict):
     return out_results
 
 
-def load_recent_results_in_datadir(fresh_days, datadir, success_only=False):
+def trim_results_ip_changed(result_dict, ipv4=True, ipv6=False):
+    """When there are results for the same relay with different IPs,
+    create a new results' dictionary without that relay's results using an
+    older IP.
+
+    :param dict result_dict: a dictionary of results
+    :param bool ipv4: whether to trim the results when a relay's IPv4 changes
+    :param bool ipv6: whether to trim the results when a relay's IPv6 changes
+    :returns: a new results dictionary
+    """
+    assert isinstance(result_dict, dict)
+    assert ipv4 is True or ipv6 is True
+    new_results_dict = {}
+    if ipv4 is True:
+        for fp in result_dict.keys():
+            results = result_dict[fp]
+            # find if the results for a relay have more than one ipv4
+            # address
+            ipv4s = set([result.address for result in results])
+            if len(ipv4s) > 1:
+                # keep only the results for the last ip used
+                # probably we should not just discard all the results for
+                # a relay that change address
+                ordered_results = sorted(results, key=lambda r: r.time)
+                latest_address = ordered_results[-1].address
+                last_ip_results = [result for result in results
+                                   if result.address == latest_address]
+                new_results_dict[fp] = last_ip_results
+            else:
+                new_results_dict[fp] = results
+    if ipv6 is True:
+        # Not implemented
+        pass
+    return new_results_dict
+
+
+def load_recent_results_in_datadir(fresh_days, datadir, success_only=False,
+                                   ipv4=True, ipv6=False):
     ''' Given a data directory, read all results files in it that could have
     results in them that are still valid. Trim them, and return the valid
     Results as a list '''





More information about the tor-commits mailing list