Commit a82c2618 authored by juga  's avatar juga
Browse files

fix: scaling: Stop returning 1 as the means minima

since they are used as the numerator when calculating the ratio and the
rounding already returns a minimum of 1.
parent 455a7f69
...@@ -14,10 +14,13 @@ def bw_filt(bw_measurements): ...@@ -14,10 +14,13 @@ def bw_filt(bw_measurements):
It is the equivalent to Torflow's ``filt_sbw``. It is the equivalent to Torflow's ``filt_sbw``.
``mu`` in this function is the equivalent to Torflow's ``sbw``. ``mu`` in this function is the equivalent to Torflow's ``sbw``.
""" """
# It's safe to return 0 here, because:
# 1. this value will be the numerator when calculating the ratio.
# 2. `kb_round_x_sig_dig` returns a minimum of 1.
# This should never be the case, as the measurements come from successful # This should never be the case, as the measurements come from successful
# results. # results.
if not bw_measurements: if not bw_measurements:
return 1 return 0
# Torflow is rounding to an integer, so is `bw_mean_from_results` in # Torflow is rounding to an integer, so is `bw_mean_from_results` in
# `v3bwfile.py` # `v3bwfile.py`
mu = round(mean(bw_measurements)) mu = round(mean(bw_measurements))
...@@ -25,3 +28,23 @@ def bw_filt(bw_measurements): ...@@ -25,3 +28,23 @@ def bw_filt(bw_measurements):
if bws_gte_mean: if bws_gte_mean:
return round(mean(bws_gte_mean)) return round(mean(bws_gte_mean))
return 1 return 1
def network_means_by_relay_type(bw_lines, router_statuses_d):
# Temporarily assign the type of relay to calculate network stream and
# filtered bandwidth by type
for line in bw_lines:
rs = None
if router_statuses_d:
rs = router_statuses_d.get(line.node_id.replace("$", ""), None)
line.set_relay_type(rs_relay_type(rs))
mu_type = muf_type = {}
for rt in RELAY_TYPES:
bw_lines_type = [line for line in bw_lines if line.relay_type == rt]
if len(bw_lines_type) > 0:
# Torflow does not round these values.
# Ensure they won't be 0 to avoid division by 0 Exception
mu_type[rt] = mean([line.bw_mean for line in bw_lines_type]) or 1
muf_type[rt] = mean([line.bw_filt for line in bw_lines_type]) or 1
return mu_type, muf_type
...@@ -875,9 +875,12 @@ class V3BWLine(object): ...@@ -875,9 +875,12 @@ class V3BWLine(object):
def bw_mean_from_results(results): def bw_mean_from_results(results):
bws = [dl['amount'] / dl['duration'] bws = [dl['amount'] / dl['duration']
for r in results for dl in r.downloads] for r in results for dl in r.downloads]
# It's safe to return 0 here, because:
# 1. this value will be the numerator when calculating the ratio.
# 2. `kb_round_x_sig_dig` returns a minimum of 1.
if bws: if bws:
return max(round(mean(bws)), 1) return round(mean(bws))
return 1 return 0
@staticmethod @staticmethod
def last_time_from_results(results): def last_time_from_results(results):
......
...@@ -18,7 +18,7 @@ def test_bw_filt(): ...@@ -18,7 +18,7 @@ def test_bw_filt():
# When there are no measurements what can not be the case for successful # When there are no measurements what can not be the case for successful
# results. # results.
bw_measurements = [] bw_measurements = []
assert 1 == scaling.bw_filt(bw_measurements) assert 0 == scaling.bw_filt(bw_measurements)
bw_measurements = [1, 0] bw_measurements = [1, 0]
# Because rounded to int # Because rounded to int
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment