Commit f7c433c1 authored by juga's avatar juga Committed by Matt Traudt
Browse files

Move logic re. header to header class

parent edc6ff2b
...@@ -2,7 +2,6 @@ from sbws.globals import (fail_hard, is_initted) ...@@ -2,7 +2,6 @@ from sbws.globals import (fail_hard, is_initted)
from sbws.lib.v3bwfile import V3BwHeader from sbws.lib.v3bwfile import V3BwHeader
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.filelock import FileLock
from sbws.util.timestamp import unixts_to_isodt_str, unixts_to_str from sbws.util.timestamp import unixts_to_isodt_str, unixts_to_str
from argparse import ArgumentDefaultsHelpFormatter from argparse import ArgumentDefaultsHelpFormatter
from statistics import median from statistics import median
...@@ -104,24 +103,6 @@ def log_stats(data_lines): ...@@ -104,24 +103,6 @@ def log_stats(data_lines):
log.info('Mean bandwidth per line: %f "KiB"', bw_per_line) log.info('Mean bandwidth per line: %f "KiB"', bw_per_line)
def read_started_ts(conf):
"""Read ISO formated timestamp which represents the date and time
when scanner started.
:param ConfigParser conf: configuration
:returns: str, ISO formated timestamp
"""
filepath = conf['paths']['started_filepath']
try:
with FileLock(filepath):
with open(filepath, 'r') as fd:
generator_started = fd.read()
except FileNotFoundError as e:
log.warn('File %s not found.%s', filepath, e)
return ''
return generator_started
def main(args, conf): def main(args, conf):
if not is_initted(args.directory): if not is_initted(args.directory):
fail_hard('Sbws isn\'t initialized. Try sbws init') fail_hard('Sbws isn\'t initialized. Try sbws init')
...@@ -144,21 +125,11 @@ def main(args, conf): ...@@ -144,21 +125,11 @@ def main(args, conf):
data_lines = [result_data_to_v3bw_line(results, fp) for fp in results] data_lines = [result_data_to_v3bw_line(results, fp) for fp in results]
data_lines = sorted(data_lines, key=lambda d: d.bw, reverse=True) data_lines = sorted(data_lines, key=lambda d: d.bw, reverse=True)
data_lines = scale_lines(args, data_lines) data_lines = scale_lines(args, data_lines)
log_stats(data_lines)
# process header lines # process header lines
# FIXME: what to move to V3BwHeader? # FIXME: what to move to V3BwHeader?
generator_started = read_started_ts(conf) header = V3BwHeader.from_results(conf, results)
# here it is ensured that we have results
lastest_bandwidth_unixts = max([r.time for fp in results
for r in results[fp]])
timestamp = unixts_to_str(lastest_bandwidth_unixts)
earliest_bandwidth_unixts = min([r.time for fp in results
for r in results[fp]])
earliest_bandwidth = unixts_to_isodt_str(earliest_bandwidth_unixts)
header = V3BwHeader(timestamp=timestamp,
earliest_bandwidth=earliest_bandwidth,
generator_started=generator_started)
log_stats(data_lines)
# FIXME: move this to V3BwFile class? # FIXME: move this to V3BwFile class?
output = conf['paths']['v3bw_fname'] output = conf['paths']['v3bw_fname']
......
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
(v3bw) used by bandwidth authorities.""" (v3bw) used by bandwidth authorities."""
import logging import logging
from sbws import __version__ from sbws import __version__
from sbws.globals import SPEC_VERSION from sbws.globals import SPEC_VERSION
from sbws.util.filelock import FileLock
from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -20,9 +22,29 @@ UNORDERED_KEYVALUES = EXTRA_ARG_KEYVALUES + ['lastest_bandwidth'] ...@@ -20,9 +22,29 @@ UNORDERED_KEYVALUES = EXTRA_ARG_KEYVALUES + ['lastest_bandwidth']
# List of all the KeyValues currently being used to generate the file # List of all the KeyValues currently being used to generate the file
ALL_KEYVALUES = ['version'] + UNORDERED_KEYVALUES ALL_KEYVALUES = ['version'] + UNORDERED_KEYVALUES
TERMINATOR = '====' TERMINATOR = '===='
# Num header lines in v1.1.0 using all the KeyValues
NUM_LINES_HEADER_V110 = len(ALL_KEYVALUES) + 2
LINE_TERMINATOR = TERMINATOR + LINE_SEP LINE_TERMINATOR = TERMINATOR + LINE_SEP
def read_started_ts(conf):
"""Read ISO formated timestamp which represents the date and time
when scanner started.
:param ConfigParser conf: configuration
:returns: str, ISO formated timestamp
"""
filepath = conf['paths']['started_filepath']
try:
with FileLock(filepath):
with open(filepath, 'r') as fd:
generator_started = fd.read()
except FileNotFoundError as e:
log.warn('File %s not found.%s', filepath, e)
return ''
return generator_started
class V3BwHeader(object): class V3BwHeader(object):
""" """
Create a bandwidth measurements (V3bw) header Create a bandwidth measurements (V3bw) header
...@@ -40,7 +62,10 @@ class V3BwHeader(object): ...@@ -40,7 +62,10 @@ class V3BwHeader(object):
when the generator started when the generator started
""" """
def __init__(self, timestamp, **kwargs): def __init__(self, timestamp, **kwargs):
self.timestamp = str(timestamp) assert isinstance(timestamp, str)
for v in kwargs.values():
assert isinstance(v, str)
self.timestamp = timestamp
# KeyValues with default value when not given by kwargs # KeyValues with default value when not given by kwargs
self.version = kwargs.get('version', SPEC_VERSION) self.version = kwargs.get('version', SPEC_VERSION)
self.software = kwargs.get('software', 'sbws') self.software = kwargs.get('software', 'sbws')
...@@ -132,3 +157,32 @@ class V3BwHeader(object): ...@@ -132,3 +157,32 @@ class V3BwHeader(object):
""" """
assert isinstance(text, str) assert isinstance(text, str)
return self.from_lines_v110(text.split(LINE_SEP)) return self.from_lines_v110(text.split(LINE_SEP))
@property
def num_lines(self):
return len(self.__str__().split(LINE_SEP))
@staticmethod
def generator_started_from_file(conf):
return read_started_ts(conf)
@staticmethod
def lastest_bandwidth_from_results(results):
return max([r.time for fp in results for r in results[fp]])
@staticmethod
def earliest_bandwidth_from_results(results):
return min([r.time for fp in results for r in results[fp]])
@classmethod
def from_results(cls, conf, results):
kwargs = dict()
lastest_bandwidth = cls.lastest_bandwidth_from_results(results)
earliest_bandwidth = cls.lastest_bandwidth_from_results(results)
generator_started = cls.generator_started_from_file(conf)
timestamp = str(lastest_bandwidth)
kwargs['lastest_bandwidth'] = unixts_to_isodt_str(lastest_bandwidth)
kwargs['earliest_bandwidth'] = unixts_to_isodt_str(earliest_bandwidth)
kwargs['generator_started'] = generator_started
h = cls(timestamp, **kwargs)
return h
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