"""Helper methods.""" from lib import constants from lib.abacus_event import AbacusEvent from lib.config import S3_PAYMENTS_BUCKET_NAME from lib.utils import aws from lib.utils import event from lib.utils import ows from lib.utils import paths PARENT_TABLE_NAME = 'payment_group_payment' def get_event_from_params(dag_run: dict, **kwargs) -> AbacusEvent: """Get event from params passed to task callbacks. Args: dag_run (dict): a dag's config Returns: instance of an AbacusEvent """ abacus_event = event.get_abacus_event(dag_run, **kwargs) event.validate_event_for_handler( abacus_event, event_name=constants.DAG_PAYMENTS_GENERATE_EXPORT_EVENT_NAME, target_type=constants.DAG_PAYMENTS_GENERATE_EXPORT_TARGET_TYPE ) return abacus_event def build_report_summary_archive_location( payment_group_payment_id: int, payment_name: str ) -> tuple: """Build S3 path to archive a summary report. Args: payment_group_payment_id (int): ID of the payment_group_payment payment_name (str): name of the payment_group_payment Returns: a namedtuple of the S3 key and url """ summary_path = build_report_summary_path(payment_group_payment_id, payment_name) file_name = paths.build_archived_payment_summary_file_name(payment_name) path = aws.join(summary_path, constants.DIRECTORY_ARCHIVE, file_name) return aws.location(path, bucket_name=S3_PAYMENTS_BUCKET_NAME) def build_report_summary_location( payment_group_payment_id: int, payment_name: str ) -> tuple: """Build S3 location to a summary report. Args: payment_group_payment_id (int): ID of the payment_group_payment payment_name (str): name of the payment_group_payment Returns: a namedtuple of the S3 key and url """ summary_path = build_report_summary_path(payment_group_payment_id, payment_name) file_name = paths.build_payment_summary_file_name(payment_name) path = aws.join(summary_path, file_name) return aws.location(path, bucket_name=S3_PAYMENTS_BUCKET_NAME) def build_report_summary_path(payment_group_payment_id: int, payment_name: str) -> str: """Build S3 path for a report summary. Args: payment_group_payment_id (int): ID of the payment_group_payment payment_name (str): name of the payment_group_payment Returns: a path to a payment summary directory in S3. """ payment_slug = paths.build_payments_slug(payment_group_payment_id, payment_name) return aws.join(payment_slug, constants.DIRECTORY_SUMMARY) def get_abacus_state(action_name: str, payment_group_payment_id: int,) -> dict: """Find specified action in list of payment_group_payment actions. Args: action_name (str): name of the action for to get details payment_group_payment_id (int): ID of the payment_group_payment Returns: the specified action details """ abacus_states = ows.get_abacus_states(PARENT_TABLE_NAME, payment_group_payment_id) action_state = next( (action for action in abacus_states if action['action_name'] == action_name), None ) if not action_state: raise ValueError(f'{action_name} state not found') return action_state