""" Distribution Fee ETL log specific functions. This has convenience functions for all things related to the logs in the distribution_fee_etl_log table. It is separate from a general logger. """ from datetime import datetime from flows import datastore from flows import g from flows.distribution_fee import config from flows.distribution_fee import status def create(correlation_id, upcs): """Create a new ETL log entry. Args: correlation_id (str): Correlation ID for ETL process. upcs (tuple): upc strings. """ sql = """ INSERT {table_name} (correlation_id, date_end, date_start, etl_start, etl_status, upcs) VALUES ( %(correlation_id)s, null, null, %(etl_start)s, %(etl_status)s, %(upcs)s)""".format( table_name=config.TABLE_ETL_LOG) params = { 'correlation_id': correlation_id, '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 {table_name} SET workflow_run_id = %(workflow_run_id)s WHERE correlation_id = %(correlation_id)s""".format( table_name=config.TABLE_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 {table_name} SET etl_status = %(status)s WHERE correlation_id = %(correlation_id)s""".format( table_name=config.TABLE_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)