""" Theatrical ETL log specific functions. This has convenience functions for all things related to the logs in the theatrical_revenue_etl_log table. It is separate from a general logger. """ from datetime import datetime from flows import datastore from flows import g from flows.theatrical import config from flows.theatrical import status def create(correlation_id, date_end, date_start, upcs, with_archive): """Create a new ETL log entry. Args: correlation_id (str): Correlation ID for ETL process. date_end (date): ending date context. date_start (date): starting date context. upcs (tuple): upc strings. with_archive (bool): if True the flow will reuse source csv files. """ sql = """ INSERT {log_table} ( correlation_id, date_end, date_start, etl_start, etl_status, upcs, with_archive) VALUES ( %(correlation_id)s, %(date_end)s, %(date_start)s, %(etl_start)s, %(etl_status)s, %(upcs)s, %(with_archive)s)""".format( log_table=config.THEATRICAL_REVENUE_ETL_LOG) params = { 'correlation_id': correlation_id, 'date_end': date_end, 'date_start': date_start, 'with_archive': with_archive, 'etl_start': datetime.now(), 'etl_status': status.STARTED, 'upcs': upcs.data_json} datastore.execute(sql, params) def add_run_id(correlation_id, workflow_run_id): """Update ETL log with run ID from AWS SWF. Args: correlation_id (str): Correlation ID for ETL process. workflow_run_id (str): SWF execution run id. """ sql = """ UPDATE {log_table} SET workflow_run_id = %(workflow_run_id)s WHERE correlation_id = %(correlation_id)s""".format( log_table=config.THEATRICAL_REVENUE_ETL_LOG) params = { 'correlation_id': correlation_id, 'workflow_run_id': workflow_run_id} datastore.execute(sql, params) def update_status(correlation_id, status): """Update ETL log status. Args: correlation_id (str): Correlation ID for ETL process. status (str): new status for the etl log. """ sql = """ UPDATE {log_table} SET etl_status = %(status)s WHERE correlation_id = %(correlation_id)s""".format( log_table=config.THEATRICAL_REVENUE_ETL_LOG) params = { 'correlation_id': correlation_id.split('.')[0], 'status': status} g.log.info( 'Updating ETL with correlation ID: "{correlation_id}" ' 'to status: "{status}"'.format(**params)) datastore.execute(sql, params)