Commit d43e76f1 authored by juga's avatar juga
Browse files

Move bw line class to v3bwfile.py

so that it close to the header and we can reuse methods/functions
parent 388bf748
from sbws.globals import (fail_hard, is_initted) 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 ResultSuccess
from sbws.lib.resultdump import load_recent_results_in_datadir from sbws.lib.resultdump import load_recent_results_in_datadir
from sbws.util.timestamp import unixts_to_isodt_str from sbws.util.timestamp import unixts_to_isodt_str
...@@ -11,26 +11,6 @@ import logging ...@@ -11,26 +11,6 @@ import logging
log = logging.getLogger(__name__) 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): def result_data_to_v3bw_line(data, fingerprint):
assert fingerprint in data assert fingerprint in data
results = data[fingerprint] results = data[fingerprint]
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
(v3bw) used by bandwidth authorities.""" (v3bw) used by bandwidth authorities."""
import logging import logging
from statistics import median
from sbws import __version__ from sbws import __version__
from sbws.globals import SPEC_VERSION from sbws.globals import SPEC_VERSION
...@@ -186,3 +187,20 @@ class V3BwHeader(object): ...@@ -186,3 +187,20 @@ class V3BwHeader(object):
kwargs['generator_started'] = generator_started kwargs['generator_started'] = generator_started
h = cls(timestamp, **kwargs) h = cls(timestamp, **kwargs)
return h 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)
Supports Markdown
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