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

Replace timestamp conversion with util functions

also:
* remove latest_bandwidth outside of class, since it should be
  generated from timestamp
* pass all init arguments as string
* require timestamp argument
* fix tests with changes
parent ab52e2c8
...@@ -135,31 +135,32 @@ def main(args, conf): ...@@ -135,31 +135,32 @@ def main(args, conf):
fresh_days = conf.getint('general', 'data_period') fresh_days = conf.getint('general', 'data_period')
results = load_recent_results_in_datadir( results = load_recent_results_in_datadir(
fresh_days, datadir, success_only=True) fresh_days, datadir, success_only=True)
if results:
# Using naive datetime object without timezone, assumed utc
timestamp = datetime.utcfromtimestamp(max([r.time for fp in results
for r in results[fp]]))
lastest_bandwidth = timestamp.replace(microsecond=0).isoformat()
earliest_bandwidth = datetime.utcfromtimestamp(
min([r.time for fp in results
for r in results[fp]])) \
.replace(microsecond=0).isoformat()
if len(results) < 1: if len(results) < 1:
log.warning('No recent results, so not generating anything. (Have you ' log.warning('No recent results, so not generating anything. (Have you '
'ran sbws scanner recently?)') 'ran sbws scanner recently?)')
return return
# process bandwidth lines
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)
# process header lines
# FIXME: what to move to V3BwHeader?
generator_started = read_started_ts(conf) generator_started = read_started_ts(conf)
if results: # here it is ensured that we have results
header = V3BwHeader(timestamp=timestamp, lastest_bandwidth_unixts = max([r.time for fp in results
lastest_bandwidth=lastest_bandwidth, for r in results[fp]])
earliest_bandwidth=earliest_bandwidth, timestamp = unixts_to_str(lastest_bandwidth_unixts)
generator_started=generator_started) earliest_bandwidth_unixts = min([r.time for fp in results
else: for r in results[fp]])
header = V3BwHeader(generator_started=generator_started) 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) log_stats(data_lines)
# FIXME: move this to V3BwFile class?
output = conf['paths']['v3bw_fname'] output = conf['paths']['v3bw_fname']
if args.output: if args.output:
output = args.output output = args.output
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
"""Classes and functions that create the bandwidth measurements document """Classes and functions that create the bandwidth measurements document
(v3bw) used by bandwidth authorities.""" (v3bw) used by bandwidth authorities."""
from datetime import datetime
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.timestamp import now_isodt_str, unixts_to_isodt_str
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -28,30 +28,26 @@ class V3BwHeader(object): ...@@ -28,30 +28,26 @@ class V3BwHeader(object):
Create a bandwidth measurements (V3bw) header Create a bandwidth measurements (V3bw) header
following bandwidth measurements document spec version 1.1.0. following bandwidth measurements document spec version 1.1.0.
:param int timestamp: timestamp in Unix Epoch seconds of the most recent :param str timestamp: timestamp in Unix Epoch seconds of the most recent
generator result. generator result.
:param str version: the spec version :param str version: the spec version
:param str software: the name of the software that generates this :param str software: the name of the software that generates this
:param str software_version: the version of the software :param str software_version: the version of the software
:param dict kwargs: extra headers. Currently supported: :param dict kwargs: extra headers. Currently supported:
- lastest_bandwidth: str, ISO 8601 timestamp in UTC time zone - earliest_bandwidth: str, ISO 8601 timestamp in UTC time zone
when the latest bandwidth was obtained when the first bandwidth was obtained
- earliest_bandwidth: str, ISO timestamp when the first bandwidth was - generator_started: str, ISO 8601 timestamp in UTC time zone
obtained when the generator started
- generator_started: str, ISO timestamp when the generator started
""" """
def __init__(self, timestamp=None, version=SPEC_VERSION, software='sbws', def __init__(self, timestamp, version=SPEC_VERSION, software='sbws',
software_version=__version__, **kwargs): software_version=__version__, **kwargs):
# FIXME: which value should timestamp have when is not given? # FIXME: which value should timestamp have when is not given?
self.timestamp = timestamp self.timestamp = timestamp
self.version = version self.version = version
self.software = software self.software = software
self.software_version = software_version self.software_version = software_version
self.file_created = kwargs.get('file_created', self.file_created = kwargs.get('file_created', now_isodt_str())
datetime.utcnow().replace(microsecond=0) self.lastest_bandwidth = unixts_to_isodt_str(timestamp)
.isoformat())
if kwargs.get('lastest_bandwidth'):
self.lastest_bandwidth = kwargs['lastest_bandwidth']
if kwargs.get('earliest_bandwidth'): if kwargs.get('earliest_bandwidth'):
self.earliest_bandwidth = kwargs['earliest_bandwidth'] self.earliest_bandwidth = kwargs['earliest_bandwidth']
if kwargs.get('generator_started'): if kwargs.get('generator_started'):
...@@ -133,7 +129,7 @@ class V3BwHeader(object): ...@@ -133,7 +129,7 @@ class V3BwHeader(object):
# is not a bw file or is v100 # is not a bw file or is v100
log.warn('Terminator is not in lines') log.warn('Terminator is not in lines')
return None return None
ts = int(lines[0]) ts = lines[0]
# not checking order # not checking order
kwargs = dict([l.split(K_SEP_V110) kwargs = dict([l.split(K_SEP_V110)
for l in lines[:index_terminator] for l in lines[:index_terminator]
......
...@@ -11,11 +11,11 @@ software_l = K_SEP_V110.join(['software', 'sbws']) ...@@ -11,11 +11,11 @@ software_l = K_SEP_V110.join(['software', 'sbws'])
software_version_l = K_SEP_V110.join(['software_version', version]) software_version_l = K_SEP_V110.join(['software_version', version])
file_created = '2018-04-25T13:10:57' file_created = '2018-04-25T13:10:57'
file_created_l = K_SEP_V110.join(['file_created', file_created]) file_created_l = K_SEP_V110.join(['file_created', file_created])
header_ls = [timestamp_l, version_l, file_created_l, software_l,
software_version_l, TERMINATOR]
header_str = LINE_SEP.join(header_ls) + LINE_SEP
lastest_bandwidth = '2018-04-17T14:09:07' lastest_bandwidth = '2018-04-17T14:09:07'
lastest_bandwidth_l = K_SEP_V110.join(['lastest_bandwidth', lastest_bandwidth]) lastest_bandwidth_l = K_SEP_V110.join(['lastest_bandwidth', lastest_bandwidth])
header_ls = [timestamp_l, version_l, file_created_l, lastest_bandwidth_l,
software_l, software_version_l, TERMINATOR]
header_str = LINE_SEP.join(header_ls) + LINE_SEP
earliest_bandwidth = '2018-04-16T14:09:07' earliest_bandwidth = '2018-04-16T14:09:07'
earliest_bandwidth_l = K_SEP_V110.join(['earliest_bandwidth', earliest_bandwidth_l = K_SEP_V110.join(['earliest_bandwidth',
earliest_bandwidth]) earliest_bandwidth])
...@@ -31,14 +31,13 @@ header_extra_str = LINE_SEP.join(header_extra_ls) + LINE_SEP ...@@ -31,14 +31,13 @@ header_extra_str = LINE_SEP.join(header_extra_ls) + LINE_SEP
def test_v3bwheader_str(): def test_v3bwheader_str():
"""Test header str""" """Test header str"""
header = V3BwHeader(timestamp, file_created=file_created) header = V3BwHeader(timestamp_l, file_created=file_created)
assert header_str == str(header) assert header_str == str(header)
def test_v3bwheader_extra_str(): def test_v3bwheader_extra_str():
"""Test header str with additional headers""" """Test header str with additional headers"""
header = V3BwHeader(timestamp, header = V3BwHeader(timestamp_l,
lastest_bandwidth=lastest_bandwidth,
file_created=file_created, file_created=file_created,
generator_started=generator_started, generator_started=generator_started,
earliest_bandwidth=earliest_bandwidth) earliest_bandwidth=earliest_bandwidth)
...@@ -47,23 +46,21 @@ def test_v3bwheader_extra_str(): ...@@ -47,23 +46,21 @@ def test_v3bwheader_extra_str():
def test_v3bwheader_from_lines(): def test_v3bwheader_from_lines():
"""""" """"""
header_obj = V3BwHeader(timestamp, header_obj = V3BwHeader(timestamp_l,
lastest_bandwidth=lastest_bandwidth,
file_created=file_created, file_created=file_created,
generator_started=generator_started, generator_started=generator_started,
earliest_bandwidth=earliest_bandwidth) earliest_bandwidth=earliest_bandwidth)
header, _ = V3BwHeader().from_lines_v110(header_extra_ls) header, _ = V3BwHeader.from_lines_v110(header_extra_ls)
assert str(header_obj) == str(header) assert str(header_obj) == str(header)
def test_v3bwheader_from_text(): def test_v3bwheader_from_text():
"""""" """"""
header_obj = V3BwHeader(timestamp, header_obj = V3BwHeader(timestamp_l,
lastest_bandwidth=lastest_bandwidth,
file_created=file_created, file_created=file_created,
generator_started=generator_started, generator_started=generator_started,
earliest_bandwidth=earliest_bandwidth) earliest_bandwidth=earliest_bandwidth)
header, _ = V3BwHeader().from_text_v110(header_extra_str) header, _ = V3BwHeader.from_text_v110(header_extra_str)
assert str(header_obj) == str(header) assert str(header_obj) == str(header)
......
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