Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
juga
sbws
Commits
f7c433c1
Commit
f7c433c1
authored
May 23, 2018
by
juga
Committed by
Matt Traudt
Jun 05, 2018
Browse files
Move logic re. header to header class
parent
edc6ff2b
Changes
2
Hide whitespace changes
Inline
Side-by-side
sbws/core/generate.py
View file @
f7c433c1
...
...
@@ -2,7 +2,6 @@ from sbws.globals import (fail_hard, is_initted)
from
sbws.lib.v3bwfile
import
V3BwHeader
from
sbws.lib.resultdump
import
ResultSuccess
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
argparse
import
ArgumentDefaultsHelpFormatter
from
statistics
import
median
...
...
@@ -104,24 +103,6 @@ def log_stats(data_lines):
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
):
if
not
is_initted
(
args
.
directory
):
fail_hard
(
'Sbws isn
\'
t initialized. Try sbws init'
)
...
...
@@ -144,21 +125,11 @@ def main(args, conf):
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
=
scale_lines
(
args
,
data_lines
)
log_stats
(
data_lines
)
# process header lines
# FIXME: what to move to V3BwHeader?
generator_started
=
read_started_ts
(
conf
)
# 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
)
header
=
V3BwHeader
.
from_results
(
conf
,
results
)
# FIXME: move this to V3BwFile class?
output
=
conf
[
'paths'
][
'v3bw_fname'
]
...
...
sbws/lib/v3bwfile.py
View file @
f7c433c1
...
...
@@ -3,8 +3,10 @@
(v3bw) used by bandwidth authorities."""
import
logging
from
sbws
import
__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
log
=
logging
.
getLogger
(
__name__
)
...
...
@@ -20,9 +22,29 @@ UNORDERED_KEYVALUES = EXTRA_ARG_KEYVALUES + ['lastest_bandwidth']
# List of all the KeyValues currently being used to generate the file
ALL_KEYVALUES
=
[
'version'
]
+
UNORDERED_KEYVALUES
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
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
):
"""
Create a bandwidth measurements (V3bw) header
...
...
@@ -40,7 +62,10 @@ class V3BwHeader(object):
when the generator started
"""
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
self
.
version
=
kwargs
.
get
(
'version'
,
SPEC_VERSION
)
self
.
software
=
kwargs
.
get
(
'software'
,
'sbws'
)
...
...
@@ -132,3 +157,32 @@ class V3BwHeader(object):
"""
assert
isinstance
(
text
,
str
)
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment