scaling.py 1.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from statistics import mean


def bw_measurements_from_results(results):
    return [
        dl['amount'] / dl['duration']
        for r in results for dl in r.downloads
    ]


def bw_filt(bw_measurements):
    """Filtered bandwidth for a relay.

    It is the equivalent to Torflow's ``filt_sbw``.
    ``mu`` in this function is the equivalent to Torflow's ``sbw``.
    """
17
18
19
    # 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.
20
21
22
    # This should never be the case, as the measurements come from successful
    # results.
    if not bw_measurements:
23
        return 0
24
25
26
    # Torflow is rounding to an integer, so is `bw_mean_from_results` in
    # `v3bwfile.py`
    mu = round(mean(bw_measurements))
27
28
    bws_gte_mean = list(filter(lambda bw: bw >= mu, bw_measurements))
    if bws_gte_mean:
juga's avatar
juga committed
29
        return round(mean(bws_gte_mean))
30
    return 1
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


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