-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve logging output during chain sync (#1478)
- Loading branch information
1 parent
2a6f3cb
commit adf66da
Showing
5 changed files
with
222 additions
and
48 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
tests/trinity/core/humanize-utils/test_humanize_elapsed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
|
||
from trinity.utils.humanize import humanize_elapsed | ||
|
||
|
||
SECOND = 1 | ||
MINUTE = 60 | ||
HOUR = 60 * 60 | ||
DAY = 24 * HOUR | ||
YEAR = 365 * DAY | ||
MONTH = YEAR // 12 | ||
WEEK = 7 * DAY | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'seconds,expected', | ||
( | ||
(0, '0s'), | ||
(1, '1s'), | ||
(60, '1m'), | ||
(61, '1m1s'), | ||
(119, '1m59s'), | ||
(HOUR, '1h'), | ||
(HOUR + 1, '1h0m1s'), | ||
(HOUR + MINUTE + 1, '1h1m1s'), | ||
(DAY + HOUR, '1d1h'), | ||
(DAY + HOUR + MINUTE, '1d1h1m'), | ||
(DAY + MINUTE, '1d0h1m'), | ||
(DAY + MINUTE + 1, '1d0h1m'), | ||
(WEEK + DAY + HOUR, '1w1d1h'), | ||
(WEEK + DAY + HOUR + MINUTE, '1w1d1h'), | ||
(WEEK + DAY + HOUR + SECOND, '1w1d1h'), | ||
(MONTH + WEEK + DAY, '1m1w1d'), | ||
(MONTH + WEEK + DAY + HOUR, '1m1w1d'), | ||
(YEAR + MONTH + WEEK, '1y1m1w'), | ||
(YEAR + MONTH + WEEK + DAY, '1y1m1w'), | ||
), | ||
) | ||
def test_humanize_elapsed(seconds, expected): | ||
actual = humanize_elapsed(seconds) | ||
assert actual == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Union | ||
|
||
from eth_utils import ValidationError | ||
|
||
|
||
class EMA: | ||
""" | ||
Represents an exponential moving average. | ||
https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average | ||
Smoothing factor, or "alpha" of the exponential moving average. | ||
- Closer to 0 gives you smoother, slower-to-update, data | ||
- Closer to 1 gives you choppier, quicker-to-update, data | ||
.. note:: | ||
A smoothing factor of 1 would completely ignore history whereas 0 would | ||
completely ignore new data | ||
The initial value is the starting value for the EMA | ||
""" | ||
def __init__(self, initial_value: float, smoothing_factor: float) -> None: | ||
self._value = initial_value | ||
if 0 < smoothing_factor < 1: | ||
self._alpha = smoothing_factor | ||
else: | ||
raise ValidationError("Smoothing factor of EMA must be between 0 and 1") | ||
|
||
def update(self, scalar: Union[int, float]) -> None: | ||
self._value = (self._value * (1 - self._alpha)) + (scalar * self._alpha) | ||
|
||
@property | ||
def value(self) -> float: | ||
return self._value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Iterator | ||
|
||
|
||
def humanize_elapsed(seconds: int) -> str: | ||
return ''.join(_humanize_elapsed(seconds)) | ||
|
||
|
||
SECOND = 1 | ||
MINUTE = 60 | ||
HOUR = 60 * 60 | ||
DAY = 24 * HOUR | ||
YEAR = 365 * DAY | ||
MONTH = YEAR // 12 | ||
WEEK = 7 * DAY | ||
|
||
|
||
UNITS = ( | ||
(YEAR, 'y'), | ||
(MONTH, 'm'), | ||
(WEEK, 'w'), | ||
(DAY, 'd'), | ||
(HOUR, 'h'), | ||
(MINUTE, 'm'), | ||
(SECOND, 's'), | ||
) | ||
|
||
|
||
def _humanize_elapsed(seconds: int) -> Iterator[str]: | ||
if not seconds: | ||
yield '0s' | ||
|
||
num_display_units = 0 | ||
remainder = seconds | ||
|
||
for duration, unit in UNITS: | ||
if not remainder: | ||
break | ||
if remainder >= duration or num_display_units: | ||
num = remainder // duration | ||
yield f"{num}{unit}" | ||
num_display_units += 1 | ||
|
||
if num_display_units >= 3: | ||
return | ||
|
||
remainder %= duration |