Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sbws
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
juga
sbws
Commits
992ea07e
Commit
992ea07e
authored
6 years ago
by
juga
Browse files
Options
Downloads
Patches
Plain Diff
Reorder methods in BWV3Line
1. magic methods 2. classmethods 3. staticmethods 4. properties 5. methods
parent
40a76cc7
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
sbws/lib/v3bwfile.py
+65
-65
65 additions, 65 deletions
sbws/lib/v3bwfile.py
with
65 additions
and
65 deletions
sbws/lib/v3bwfile.py
+
65
−
65
View file @
992ea07e
...
...
@@ -249,38 +249,30 @@ class V3BWLine(object):
[
setattr
(
self
,
k
,
v
)
for
k
,
v
in
kwargs
.
items
()
if
k
in
BW_EXTRA_ARG_KEYVALUES
]
@property
def
bw_keyvalue_tuple_ls
(
self
):
"""
Return list of KeyValue Bandwidth Line tuples.
"""
# sort the list to generate determinist headers
keyvalue_tuple_ls
=
sorted
([(
k
,
v
)
for
k
,
v
in
self
.
__dict__
.
items
()
if
k
in
BW_KEYVALUES
])
return
keyvalue_tuple_ls
@property
def
bw_keyvalue_v110str_ls
(
self
):
"""
Return list of KeyValue Bandwidth Line strings following
spec v1.1.0.
"""
bw_keyvalue_str
=
[
KEYVALUE_SEP_V110
.
join
([
k
,
str
(
v
)])
for
k
,
v
in
self
.
bw_keyvalue_tuple_ls
]
return
bw_keyvalue_str
@property
def
bw_strv110
(
self
):
"""
Return Bandwidth Line string following spec v1.1.0.
"""
bw_line_str
=
BW_KEYVALUE_SEP_V110
.
join
(
self
.
bw_keyvalue_v110str_ls
)
+
LINE_SEP
if
len
(
bw_line_str
)
>
BW_LINE_SIZE
:
# if this is the case, probably there are too many KeyValues,
# or the limit needs to be changed in Tor
log
.
warn
(
"
The bandwidth line %s is longer than %s
"
,
len
(
bw_line_str
),
BW_LINE_SIZE
)
return
bw_line_str
def
__str__
(
self
):
return
self
.
bw_strv110
@classmethod
def
from_results
(
cls
,
results
):
success_results
=
[
r
for
r
in
results
if
isinstance
(
r
,
ResultSuccess
)]
# log.debug('len(success_results) %s', len(success_results))
node_id
=
'
$
'
+
results
[
0
].
fingerprint
bw
=
cls
.
bw_from_results
(
success_results
)
kwargs
=
dict
()
kwargs
[
'
nick
'
]
=
results
[
0
].
nickname
if
getattr
(
results
[
0
],
'
master_key_ed25519
'
):
kwargs
[
'
master_key_ed25519
'
]
=
results
[
0
].
master_key_ed25519
kwargs
[
'
rtt
'
]
=
cls
.
rtt_from_results
(
success_results
)
kwargs
[
'
time
'
]
=
cls
.
last_time_from_results
(
results
)
kwargs
.
update
(
cls
.
result_types_from_results
(
results
))
bwl
=
cls
(
node_id
,
bw
,
**
kwargs
)
return
bwl
@classmethod
def
from_data
(
cls
,
data
,
fingerprint
):
assert
fingerprint
in
data
return
cls
.
from_results
(
data
[
fingerprint
])
@classmethod
def
from_bw_line_v110
(
cls
,
line
):
assert
isinstance
(
line
,
str
)
...
...
@@ -293,6 +285,24 @@ class V3BWLine(object):
bw_line
=
cls
(
**
kwargs
)
return
bw_line
@staticmethod
def
last_time_from_results
(
results
):
return
unixts_to_isodt_str
(
round
(
max
([
r
.
time
for
r
in
results
])))
@staticmethod
def
rtt_from_results
(
results
):
# convert from miliseconds to seconds
rtts
=
[(
round
(
rtt
*
1000
))
for
r
in
results
for
rtt
in
r
.
rtts
]
rtt
=
round
(
median
(
rtts
))
return
rtt
@staticmethod
def
result_types_from_results
(
results
):
rt_dict
=
dict
([(
result_type_to_key
(
rt
.
value
),
num_results_of_type
(
results
,
rt
.
value
))
for
rt
in
_ResultType
])
return
rt_dict
@staticmethod
def
bw_from_results
(
results
):
median_bw
=
median
([
dl
[
'
amount
'
]
/
dl
[
'
duration
'
]
...
...
@@ -319,44 +329,34 @@ class V3BWLine(object):
bw_kb
=
max
(
round
(
bw
/
1024
),
1
)
return
bw_kb
@staticmethod
def
last_time_from_results
(
results
):
return
unixts_to_isodt_str
(
round
(
max
([
r
.
time
for
r
in
results
])))
@staticmethod
def
rtt_from_results
(
results
):
# convert from miliseconds to seconds
rtts
=
[(
round
(
rtt
*
1000
))
for
r
in
results
for
rtt
in
r
.
rtts
]
rtt
=
round
(
median
(
rtts
))
return
rtt
@staticmethod
def
result_types_from_results
(
results
):
rt_dict
=
dict
([(
result_type_to_key
(
rt
.
value
),
num_results_of_type
(
results
,
rt
.
value
))
for
rt
in
_ResultType
])
return
rt_dict
@property
def
bw_keyvalue_tuple_ls
(
self
):
"""
Return list of KeyValue Bandwidth Line tuples.
"""
# sort the list to generate determinist headers
keyvalue_tuple_ls
=
sorted
([(
k
,
v
)
for
k
,
v
in
self
.
__dict__
.
items
()
if
k
in
BW_KEYVALUES
])
return
keyvalue_tuple_ls
@classmethod
def
from_results
(
cls
,
results
):
success_results
=
[
r
for
r
in
results
if
isinstance
(
r
,
ResultSuccess
)]
# log.debug('len(success_results) %s', len(success_results))
node_id
=
'
$
'
+
results
[
0
].
fingerprint
bw
=
cls
.
bw_from_results
(
success_results
)
kwargs
=
dict
()
kwargs
[
'
nick
'
]
=
results
[
0
].
nickname
if
getattr
(
results
[
0
],
'
master_key_ed25519
'
):
kwargs
[
'
master_key_ed25519
'
]
=
results
[
0
].
master_key_ed25519
kwargs
[
'
rtt
'
]
=
cls
.
rtt_from_results
(
success_results
)
kwargs
[
'
time
'
]
=
cls
.
last_time_from_results
(
results
)
kwargs
.
update
(
cls
.
result_types_from_results
(
results
))
bwl
=
cls
(
node_id
,
bw
,
**
kwargs
)
return
bwl
@property
def
bw_keyvalue_v110str_ls
(
self
):
"""
Return list of KeyValue Bandwidth Line strings following
spec v1.1.0.
"""
bw_keyvalue_str
=
[
KEYVALUE_SEP_V110
.
join
([
k
,
str
(
v
)])
for
k
,
v
in
self
.
bw_keyvalue_tuple_ls
]
return
bw_keyvalue_str
@classmethod
def
from_data
(
cls
,
data
,
fingerprint
):
assert
fingerprint
in
data
return
cls
.
from_results
(
data
[
fingerprint
])
@property
def
bw_strv110
(
self
):
"""
Return Bandwidth Line string following spec v1.1.0.
"""
bw_line_str
=
BW_KEYVALUE_SEP_V110
.
join
(
self
.
bw_keyvalue_v110str_ls
)
+
LINE_SEP
if
len
(
bw_line_str
)
>
BW_LINE_SIZE
:
# if this is the case, probably there are too many KeyValues,
# or the limit needs to be changed in Tor
log
.
warn
(
"
The bandwidth line %s is longer than %s
"
,
len
(
bw_line_str
),
BW_LINE_SIZE
)
return
bw_line_str
class
V3BWFile
(
object
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment