"""Helper methods for the vat 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_VAT_TARGET_TYPE, event_name=constants.DAG_CALC_VAT_EVENT_NAME ) return abacus_event def build_vat_prefix(accounting_period_id, accounting_period_name): """Format the s3 path for the period's vat directory.""" accounting_period_slug = paths.build_period_slug( accounting_period_id, accounting_period_name ) return aws.join( accounting_period_slug, constants.DIRECTORY_MODULE_VAT ) def build_snapshot_path_for_module( accounting_period_id, accounting_period_name, module_name): """Build the s3 path for the snapshot dir of some module.""" return aws.join( build_vat_prefix(accounting_period_id, accounting_period_name), constants.DIRECTORY_SNAPSHOTS, module_name ) def build_vat_report_path( accounting_period_id, accounting_period_name, report_type): """Build the s3 path for generated vat reports.""" file_name = f'{report_type}.tsv' return aws.location(aws.join( build_vat_prefix(accounting_period_id, accounting_period_name), 'reports', file_name )).url def get_accounting_run_export_urls_by_period(accounting_period_id): """Get accounting runs' export urls by accounting period.""" accounting_runs = ows.get_accounting_period_accounting_runs(accounting_period_id) summary_urls = [item['summary_export_url'] for item in accounting_runs['items']] return list( filter( lambda url: url is not None and aws.file_exists(url), summary_urls )) def get_abacus_state_id_by_action_name(abacus_actions, action_name): """Get the id of abacus action by specified action name.""" abacus_state = list(filter( lambda x: x['action_name'] == action_name, abacus_actions )) if abacus_state: return abacus_state[0]['abacus_state_id'] print(f'ACTION {action_name} not found in abacus state.') return None def set_abacus_action_to_status(abacus_state_id, status): """Invoke ows-abacus-state PUT to set state into provided status.""" return ows.update_abacus_state( abacus_state_id, body=dict( action_status=status ) )