"""Task to flatten contract JSON and insert results into snowflake table.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from lib import constants from lib.utils import aws from tasks.accounting_run_calculate import helpers from templates.accounting_run_calculate.snowflake_flatten_contract_json \ import insert_flat_distro_contract_data_template def flatten_contract_json_task(dag_run: dict, **kwargs) -> None: """Flatten JSON in contract CSVs and insert results into contract_denormalized_*. Args: dag_run (dict): config of the DAG this task belongs to kwargs (dict): any other optional arguments """ event = helpers.get_event_from_params(dag_run, **kwargs) accounting_run_id = event.target_id accounting_period, accounting_run = helpers.get_event_records(accounting_run_id) snapshot_directory = helpers.build_snapshot_key_from_accounting_run( accounting_period, accounting_run, constants.DIRECTORY_MODULE_MATCH_CONTRACTS ) contract_csv_url = aws.location( snapshot_directory, constants.FILE_NAME_CONTRACTS_CSV ) term_csv_url = aws.location( snapshot_directory, constants.FILE_NAME_CONTRACT_TERMS_CSV ) hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) template = insert_flat_distro_contract_data_template() insert_statement = template.render( accounting_run_id=accounting_run_id, contract_csv_url=contract_csv_url.key, schema=config.OWS_ENV, tsv_stage=config.ABACUS_TSV_STAGE, term_csv_url=term_csv_url.key ) hook.run(insert_statement, autocommit=True)