""" Theatrical cuts ETL log specific functions. This has convenience functions for all things related to the logs in the theatrical_cuts_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_cuts import config from flows.theatrical_cuts import status def create(correlation_id, workflow_run_id, upc): """Create a new ETL log entry. Args: correlation_id (str): Correlation ID for ETL process. workflow_run_id (str): Workflow run id generated by AWS SWF service. upc (int): UPC value. """ sql = """ INSERT {log_table} ( correlation_id, workflow_run_id, upc, etl_start, etl_status) VALUES ( %(correlation_id)s, %(workflow_run_id)s, %(upc)s, %(etl_start)s, %(etl_status)s);""".format( log_table=config.THEATRICAL_CUTS_ETL_LOG) params = { 'correlation_id': correlation_id, 'workflow_run_id': workflow_run_id, 'upc': upc, 'etl_start': datetime.now(), 'etl_status': status.STARTED} 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, etl_end = %(etl_end)s WHERE correlation_id = %(correlation_id)s;""".format( log_table=config.THEATRICAL_CUTS_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)