"""Task to calculate NR accounting_run totals and insert them into snowflake staging.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from tasks.accounting_run_calculate import helpers as shared_helpers from tasks.accounting_run_calculate_nr import helpers from templates.accounting_run_calculate.snowflake_calculate_run_results_nr import \ calculate_totals_nr_template def calculate_run_totals_task(dag_run: dict, **kwargs) -> None: """Calculate and stage run totals using matched NR contracts/transaction results. Use CONTRACT_TRANSACTION_NR_STAGING, SNAPSHOT_CONTRACT_NR, STMT_DB_SALES_NR, and exchange_rates to calculate revenue and insert into ACCOUNTING_RUN_RESULTS_NR_STAGING. 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 = shared_helpers.get_event_records( accounting_run_id ) run_controller_name = accounting_run.get('run_controller_name') statement_period_id = accounting_period.get('statement_period_id') \ or event.statement_period_id hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) insert_statement = calculate_totals_nr_template().render( accounting_run_id=accounting_run_id, accounting_run_name=run_controller_name, schema=config.OWS_ENV, statement_period_id=statement_period_id ) hook.run(insert_statement, autocommit=True)