"""Task to snapshot Snowflake for mechanical transaction details.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from lib.constants import ALBUM, TRACK from lib.utils import helpers as shared_helpers from tasks.accounting_period_mechanicals import helpers from templates.accounting_period_mechanicals \ import snowflake_snapshot_mechanical_transaction_details as templates def snowflake_snapshot_mechanical_transaction_details_task( dag_run: dict, album_or_track: str, **kwargs ) -> None: """Snapshot Snowflake for details of transactions eligible for mech deductions. - Finds track information via UPC ("album") or ISRC ("track") via art_relations - Uses the length of a track to determine which mechanical rates to use: - reference_mechanical_rate.base_rate for any tracks less than 5 minutes - reference_mechanical_rate.minute_rate for any tracks 5 minutes or longer - if track is not licensed (track.third_party_publisher = 'N'), rate is 0 - at this time, if the transaction is not US-based, rate is 0 - Uses the mechanical rate (AKA 'royalty_rate') and transaction's quantity to calculate the mechanical deduction (AKA 'royalty_amount') - Inserts results into SNAPSHOT_MECHANICAL_TRANSACTION_DETAIL Args: dag_run (dict): config of the DAG this task belongs to album_or_track (str): one of "album" or "track" kwargs (dict): any other optional arguments """ event = helpers.get_event_from_params(dag_run, **kwargs) sales_file_ids = shared_helpers.get_sales_file_ids(event.target_id) hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) template_params = { 'sales_file_ids': sales_file_ids, 'schema': config.OWS_ENV } if album_or_track == ALBUM: insert_statement = templates. \ insert_snapshot_mechanical_transaction_details_by_upc().render( **template_params ) elif album_or_track == TRACK: insert_statement = templates. \ insert_snapshot_mechanical_transaction_details_by_isrc().render( **template_params ) else: return hook.run(insert_statement, autocommit=True)