Skip to content

Commit

Permalink
chore: change function names
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Jun 5, 2023
1 parent ef3520b commit 2814a87
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions hathor/cli/db_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def iter_tx(self) -> Iterator['BaseTransaction']:
yield tx

def run(self) -> None:
from hathor.util import progress
from hathor.util import tx_progress
self.log.info('export')
self.out_file.write(MAGIC_HEADER)
tx_count = 0
Expand All @@ -108,7 +108,7 @@ def run(self) -> None:
# estimated total, this will obviously be wrong if we're not exporting everything, but it's still better than
# nothing, and it's probably better to finish sooner than expected, rather than later than expected
total = self.tx_storage.get_vertices_count()
for tx in progress(self.iter_tx(), log=self.log, total=total):
for tx in tx_progress(self.iter_tx(), log=self.log, total=total):
assert tx.hash is not None
tx_meta = tx.get_metadata()
if tx.is_block:
Expand Down
4 changes: 2 additions & 2 deletions hathor/cli/db_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def prepare(self, args: Namespace, *, register_resources: bool = True) -> None:
self.in_file = io.BufferedReader(args.import_file)

def run(self) -> None:
from hathor.util import progress
from hathor.util import tx_progress

header = self.in_file.read(len(MAGIC_HEADER))
if header != MAGIC_HEADER:
Expand All @@ -60,7 +60,7 @@ def run(self) -> None:
self.tx_storage.pre_init()
actual_tx_count = 0
actual_block_count = 0
for tx in progress(self._import_txs(), log=self.log, total=total):
for tx in tx_progress(self._import_txs(), log=self.log, total=total):
if tx.is_block:
actual_block_count += 1
else:
Expand Down
4 changes: 2 additions & 2 deletions hathor/indexes/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from hathor.indexes.tokens_index import TokensIndex
from hathor.indexes.utxo_index import UtxoIndex
from hathor.transaction import BaseTransaction
from hathor.util import progress
from hathor.util import tx_progress

if TYPE_CHECKING: # pragma: no cover
import rocksdb
Expand Down Expand Up @@ -173,7 +173,7 @@ def _manually_initialize(self, tx_storage: 'TransactionStorage') -> None:
if indexes_to_init:
overall_scope = reduce(operator.__or__, map(lambda i: i.get_scope(), indexes_to_init))
tx_iter_inner = overall_scope.get_iterator(tx_storage)
tx_iter = progress(tx_iter_inner, log=self.log, total=tx_storage.get_vertices_count())
tx_iter = tx_progress(tx_iter_inner, log=self.log, total=tx_storage.get_vertices_count())
self.log.debug('indexes init', scope=overall_scope)
else:
tx_iter = iter([])
Expand Down
4 changes: 2 additions & 2 deletions hathor/indexes/partial_rocksdb_tips_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from hathor.indexes.memory_tips_index import MemoryTipsIndex
from hathor.indexes.rocksdb_utils import RocksDBIndexUtils
from hathor.indexes.tips_index import ScopeType
from hathor.util import generic_progress
from hathor.util import progress

if TYPE_CHECKING: # pragma: no cover
import rocksdb
Expand Down Expand Up @@ -101,7 +101,7 @@ def init_start(self, indexes_manager: 'IndexesManager') -> None:
else:
log.info('index not identified, skipping total count')
total = None
for iv in generic_progress(self._iter_intervals_db(), log=log, total=total):
for iv in progress(self._iter_intervals_db(), log=log, total=total):
self.tree.add(iv)
self.tx_last_interval[iv.data] = iv

Expand Down
24 changes: 12 additions & 12 deletions hathor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,13 @@ def verified_cast(interface_class: Type[Z], obj: Any) -> Z:
_DT_YIELD_WARN = 1 # time in seconds to warn when `yield tx` takes too long (which is when processing happens)


def generic_progress(
def progress(
it: Iterator[T],
*,
log: 'structlog.stdlib.BoundLogger',
total: Optional[int]
) -> Iterator[T]:
""" Implementation of progress helper for using with a generic type.
""" Implementation of progress helper for using with an iterator of any type.
This is basically a stripped down version of `hathor.util.progress`
"""
Expand Down Expand Up @@ -499,19 +499,19 @@ def generic_progress(
log.info('loaded', count=count, rate=rate, total_dt=dt_total)


def progress(iter_tx: Iterator['BaseTransaction'], *, log: Optional['structlog.stdlib.BoundLogger'] = None,
total: Optional[int] = None) -> Iterator['BaseTransaction']:
def tx_progress(iter_tx: Iterator['BaseTransaction'], *, log: Optional['structlog.stdlib.BoundLogger'] = None,
total: Optional[int] = None) -> Iterator['BaseTransaction']:
""" Log the progress of a transaction iterator while iterating.
"""
if log is None:
log = logger.new()

yield from _progress(iter_tx, log=log, total=total)
yield from _tx_progress(iter_tx, log=log, total=total)


def _progress(iter_tx: Iterator['BaseTransaction'], *, log: 'structlog.stdlib.BoundLogger', total: Optional[int]
) -> Iterator['BaseTransaction']:
""" Inner implementation of progress helper, it expects the gc to be disabled.
def _tx_progress(iter_tx: Iterator['BaseTransaction'], *, log: 'structlog.stdlib.BoundLogger', total: Optional[int]
) -> Iterator['BaseTransaction']:
""" Inner implementation of progress helper.
"""
t_start = time.time()
h = 0
Expand Down Expand Up @@ -549,12 +549,12 @@ def _progress(iter_tx: Iterator['BaseTransaction'], *, log: 'structlog.stdlib.Bo
ts = datetime.datetime.fromtimestamp(ts_tx)
kwargs = dict(tx_rate=tx_rate, tx_new=dcount, dt=dt_log, total=count, latest_ts=ts, height=h)
if total is not None:
progress = count / total
progress_ = count / total
elapsed_time = t_log - t_start
remaining_time = LogDuration(elapsed_time / progress - elapsed_time)
remaining_time = LogDuration(elapsed_time / progress_ - elapsed_time)
log.info(
f'loading... {math.floor(progress * 100):2.0f}%',
progress=progress,
f'loading... {math.floor(progress_ * 100):2.0f}%',
progress=progress_,
remaining_time=remaining_time,
**kwargs
)
Expand Down

0 comments on commit 2814a87

Please sign in to comment.