"""Task to calculate accounting run totals and insert into snowflake staging table.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from tasks.accounting_run_calculate import helpers from templates.accounting_run_calculate.snowflake_calculate_run_results \ import calculate_totals_distro_template def calculate_run_totals_task(dag_run: dict, **kwargs) -> None: """Calculate and stage run totals using matched contracts/transaction results. Use contract_transaction_*_staging, contract_denormalized_*, and exchange_rates to calculate revenue and insert into accounting_run_results_*_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 = 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_distro_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)