"""Helper methods.""" from lib import constants from lib.abacus_event import AbacusEvent from lib.abacus_state import AbacusState from lib.utils import aws from lib.utils import event from lib.utils import ows from lib.utils import paths PARENT_TABLE_NAME = constants.DAG_ACCOUNTING_RUN_COMMIT_TARGET_TYPE def get_event_from_params(dag_run, **kwargs): """Get event from params passed to task callbacks.""" abacus_event = event.get_abacus_event(dag_run, **kwargs) event.validate_event_for_handler( abacus_event, event_name=constants.DAG_ACCOUNTING_RUN_COMMIT_EVENT_NAME, target_type=constants.DAG_ACCOUNTING_RUN_COMMIT_TARGET_TYPE ) return abacus_event def build_mechanical_export_location( accounting_period: dict, accounting_run: dict ) -> aws.S3Location: """Build s3 location for the mechanical HFA export. Args: accounting_period (dict): parent accounting_period details accounting_run (dict): accounting_run details Returns: an S3Location tuple that includes the key and url as strings example key: {accounting_period_id}-{accounting_period_name}/ {accounting_run_id}-{run_controller_name}/mechanical-export.tsv example url: s3://{env}-royalties-sales-files/ {accounting_period_id}-{accounting_period_name}/ {accounting_run_id}-{run_controller_name}/mechanical-export.tsv """ accounting_period_slug = paths.build_period_slug( accounting_period.get('accounting_period_id'), accounting_period.get('accounting_period_name'), ) accounting_run_slug = paths.build_accounting_run_slug( accounting_run.get('accounting_run_id'), accounting_run.get('run_controller_name'), ) return aws.location( accounting_period_slug, accounting_run_slug, constants.FILE_NAME_MECHANICAL_EXPORT_TSV ) def create_event(accounting_run_id: int, event_name: str) -> AbacusEvent: """Create accounting_run event. Args: accounting_run_id (int): ID of the accounting run, the event's target_id event_name (str): Name of the event Returns: an AbacusEvent instance """ params = { 'event_name': event_name, 'target_id': accounting_run_id, 'target_type': constants.DAG_ACCOUNTING_RUN_COMMIT_TARGET_TYPE } new_event = ows.create_abacus_event(**params) return AbacusEvent(**new_event) def get_event_records(accounting_run_id: int) -> tuple: """Get accounting_period and accounting_run from the abacus_event. Args: accounting_run_id (int): the target_id of the triggering abacus_event """ accounting_run = ows.get_accounting_run_details(accounting_run_id) accounting_period = ows.get_accounting_period_details( accounting_run.get('accounting_period_id') ) return accounting_period, accounting_run def create_abacus_state(accounting_run_id: int, action_name: str) -> AbacusState: """Create accounting_run event. Args: accounting_run_id (int): ID of the accounting run, the event's target_id action_name (str): Name of the state's status Returns: an AbacusEvent instance """ params = { 'action_name': action_name, 'parent_table_id': accounting_run_id, 'parent_table_name': constants.DAG_ACCOUNTING_RUN_COMMIT_TARGET_TYPE } new_states = ows.create_abacus_state(**params) if new_states: return AbacusState(**new_states[0]) def get_abacus_state(accounting_run_id: int) -> AbacusState: """Find the 'accounting_run' abacus_state record by accounting_run_id. Args: accounting_run_id (int): ID of the accounting_run Returns the 'accounting_run' abacus_state record """ abacus_states = ows.get_abacus_states(PARENT_TABLE_NAME, accounting_run_id) action_name = constants.ACCOUNTING_RUN_ACTIONS.COMMIT_ROYALTIES accounting_run_state = next( ( abacus_state for abacus_state in abacus_states if abacus_state['action_name'] == action_name ), None ) if not accounting_run_state: raise ValueError(f'{action_name} state not found') return AbacusState(**accounting_run_state)