commit d43e76f181cfad4e78e5e0c8503cffc2f7597aeb Author: juga0 juga@riseup.net Date: Mon May 21 12:47:07 2018 +0000
Move bw line class to v3bwfile.py
so that it close to the header and we can reuse methods/functions --- sbws/core/generate.py | 22 +--------------------- sbws/lib/v3bwfile.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/sbws/core/generate.py b/sbws/core/generate.py index e89b078..acda1e5 100644 --- a/sbws/core/generate.py +++ b/sbws/core/generate.py @@ -1,5 +1,5 @@ from sbws.globals import (fail_hard, is_initted) -from sbws.lib.v3bwfile import V3BwHeader +from sbws.lib.v3bwfile import V3BwHeader, V3BWLine from sbws.lib.resultdump import ResultSuccess from sbws.lib.resultdump import load_recent_results_in_datadir from sbws.util.timestamp import unixts_to_isodt_str @@ -11,26 +11,6 @@ import logging log = logging.getLogger(__name__)
-# FIXME: move this to v3bwfile? -class V3BWLine: - # TODO: docstrings - def __init__(self, fp, bw, nick, rtts, last_time): - # TODO: asserts checking arg types - self.fp = fp - self.nick = nick - # convert to KiB and make sure the answer is at least 1 - self.bw = max(round(bw / 1024), 1) - # convert to ms - rtts = [round(r * 1000) for r in rtts] - self.rtt = round(median(rtts)) - self.time = unixts_to_isodt_str(last_time) - - def __str__(self): - frmt = 'node_id=${fp} bw={sp} nick={n} rtt={rtt} time={t}' - return frmt.format(fp=self.fp, sp=self.bw, n=self.nick, rtt=self.rtt, - t=self.time) - - def result_data_to_v3bw_line(data, fingerprint): assert fingerprint in data results = data[fingerprint] diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py index 00859e6..03ec3da 100644 --- a/sbws/lib/v3bwfile.py +++ b/sbws/lib/v3bwfile.py @@ -3,6 +3,7 @@ (v3bw) used by bandwidth authorities."""
import logging +from statistics import median
from sbws import __version__ from sbws.globals import SPEC_VERSION @@ -186,3 +187,20 @@ class V3BwHeader(object): kwargs['generator_started'] = generator_started h = cls(timestamp, **kwargs) return h + + +class V3BWLine: + def __init__(self, fp, bw, nick, rtts, last_time): + self.fp = fp + self.nick = nick + # convert to KiB and make sure the answer is at least 1 + self.bw = max(round(bw / 1024), 1) + # convert to ms + rtts = [round(r * 1000) for r in rtts] + self.rtt = round(median(rtts)) + self.time = last_time + + def __str__(self): + frmt = 'node_id=${fp} bw={sp} nick={n} rtt={rtt} time={t}' + return frmt.format(fp=self.fp, sp=self.bw, n=self.nick, rtt=self.rtt, + t=self.time)
tor-commits@lists.torproject.org