"""Helper methods for the run_calculating dag.""" from lib import constants from lib.utils import aws from lib.utils import event from lib.utils import ows from lib.utils import paths 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, target_type=constants.DAG_CALC_TARGET_TYPE, event_name=constants.DAG_CALC_EVENT_NAME ) return abacus_event def get_event_records(accounting_run_id): """Get event records.""" 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 build_accounting_period_prefix(accounting_period): """Return accounting period prefix on s3.""" return paths.build_period_slug( accounting_period.get('accounting_period_id'), accounting_period.get('accounting_period_name') ) def build_accounting_run_prefix(accounting_period, accounting_run): """Return accounting run prefix on s3.""" period_slug = build_accounting_period_prefix(accounting_period) run_slug = paths.build_accounting_run_slug( accounting_run.get('accounting_run_id'), accounting_run.get('run_controller_name') ) return aws.join(period_slug, run_slug) def build_snapshot_prefix_from_accounting_run( accounting_period, accounting_run, module_directory ): """Return snapshot s3 location for accounting run.""" accounting_run_prefix = build_accounting_run_prefix( accounting_period, accounting_run ) return aws.join( accounting_run_prefix, module_directory, constants.DIRECTORY_SNAPSHOTS ) def build_snapshot_key_from_accounting_run( accounting_period: dict, accounting_run: dict, module_directory: str ) -> str: """Build S3 key of contract snapshot locations. Excludes s3://bucket prefix. Args: accounting_period (dict): accounting_period details accounting_run (dict): accounting_run details module_directory (str): the module path """ 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.join( accounting_period_slug, accounting_run_slug, module_directory, constants.DIRECTORY_SNAPSHOTS ) def get_sales_file_ids(accounting_period_id: int) -> list: """Get sales files in the parent accounting period. Return their IDs. Args: accounting_period_id (int): ID of the parent account period Returns: a list of sales_files """ sales_files = ows.get_accounting_period_sales_files(accounting_period_id) return [sales_file.get('sales_file_id') for sales_file in sales_files]