Add docstring to cli outputter, register summary on individual tx adds

This commit is contained in:
nolash 2021-08-27 11:10:21 +02:00
parent 1dd403b4a2
commit 70cbbcc7de
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 41 additions and 4 deletions

View File

@ -1,3 +1,6 @@
# standard imports
import logging
# external imports # external imports
from hexathon import add_0x from hexathon import add_0x
@ -10,8 +13,23 @@ from chainqueue.enum import (
status_str, status_str,
) )
logg = logging.getLogger(__name__)
class Outputter: class Outputter:
"""Output helper for chainqueue cli listings tools.
:param chain_spec: Chain spec to use as getter context
:type chain_spec: chainlib.chain.ChainSpec
:param writer: Writer to write output to. Will automatically flush.
:type writer: Writer
:param getter: Transaction getter
:type getter: See chainqueue.sql.backend.get_otx
:param session_method: Backend session generator method
:type session_method: varies
:param decode_status: Print status bit details
:type decode_status: bool
"""
def __init__(self, chain_spec, writer, getter, session_method=None, decode_status=True): def __init__(self, chain_spec, writer, getter, session_method=None, decode_status=True):
self.decode_status = decode_status self.decode_status = decode_status
@ -36,7 +54,15 @@ class Outputter:
def add(self, tx_hash): def add(self, tx_hash):
tx = tx_getter(self.chain_spec, tx_hash, session=self.session) """Retrieve a transaction by hash and add it for summary output generation.
:param tx_hash: Transaction hash
:type tx_hash: str
"""
tx = self.getter(self.chain_spec, tx_hash, session=self.session)
self.__add(tx)
def __add(self, tx):
category = None category = None
if is_alive(tx['status_code']): if is_alive(tx['status_code']):
category = 'pending' category = 'pending'
@ -44,20 +70,30 @@ class Outputter:
category = 'final' category = 'final'
self.results[category] += 1 self.results[category] += 1
if is_error_status(tx['status_code']): if is_error_status(tx['status_code']):
logg.debug('registered {} as {} with error'.format(tx_hash, category)) logg.debug('registered {} as {} with error'.format(tx['tx_hash'], category))
self.results[category + '_error'] += 1 self.results[category + '_error'] += 1
else: else:
logg.debug('registered {} as {}'.format(tx_hash, category)) logg.debug('registered {} as {}'.format(tx['tx_hash'], category))
def decode_summary(self): def decode_summary(self):
"""Writes summary to the registered writer.
"""
self.writer.write('pending\t{}\t{}\n'.format(self.results['pending'], self.results['pending_error'])) self.writer.write('pending\t{}\t{}\n'.format(self.results['pending'], self.results['pending_error']))
self.writer.write('final\t{}\t{}\n'.format(self.results['final'], self.results['final_error'])) self.writer.write('final\t{}\t{}\n'.format(self.results['final'], self.results['final_error']))
self.writer.write('total\t{}\t{}\n'.format(self.results['final'] + self.results['pending'], self.results['final_error'] + self.results['pending_error'])) self.writer.write('total\t{}\t{}\n'.format(self.results['final'] + self.results['pending'], self.results['final_error'] + self.results['pending_error']))
def decode_single(self, tx_hash): def decode_single(self, tx_hash):
"""Retrieves the transaction with the given hash and writes the details to the underlying writer.
Registers the transaction with the summary generator.
:param tx_hash: Transaction hash
:type tx_hash: str
"""
tx = self.getter(self.chain_spec, tx_hash, session=self.session) tx = self.getter(self.chain_spec, tx_hash, session=self.session)
self.__add(tx)
status = tx['status'] status = tx['status']
if self.decode_status: if self.decode_status:
status = status_str(tx['status_code'], bits_only=True) status = status_str(tx['status_code'], bits_only=True)

View File

@ -175,6 +175,7 @@ class Otx(SessionBase):
self.__set_status(StatusBits.UNKNOWN_ERROR | StatusBits.FINAL, session) self.__set_status(StatusBits.UNKNOWN_ERROR | StatusBits.FINAL, session)
self.__reset_status(StatusBits.QUEUED | StatusBits.RESERVED, session)
if self.tracing: if self.tracing:
self.__state_log(session=session) self.__state_log(session=session)

View File

@ -26,7 +26,7 @@ default_data_dir = os.path.join(xdg_data_dirs[0], 'chainqueue')
default_config_dir = load_first_config('chainqueue') default_config_dir = load_first_config('chainqueue')
config_dir = os.environ.get('CONFINI_DIR', default_config_dir) config_dir = os.environ.get('CONFINI_DIR', default_config_dir)
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser("Database migration tool for chainqueue")
argparser.add_argument('-c', type=str, default=config_dir, help='config file') argparser.add_argument('-c', type=str, default=config_dir, help='config file')
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
argparser.add_argument('--data-dir', dest='data_dir', type=str, default=default_data_dir, help='data directory') argparser.add_argument('--data-dir', dest='data_dir', type=str, default=default_data_dir, help='data directory')