""" Cable ETL log specific functions. This has convenience functions for all things related to the logs in the cable_revenue_ingestion_etl_log table. It is separate from a general logger. """ from datetime import datetime from flows import datastore from flows import g from flows.cable_ingestion import queries from flows.cable_ingestion import status def create(correlation_id, date_end, date_start): """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. """ sql = """ INSERT cable_revenue_ingestion_etl_log (correlation_id, date_end, date_start, etl_start, etl_status) VALUES ( %(correlation_id)s, %(date_end)s, %(date_start)s, %(etl_start)s, %(etl_status)s)""" params = { 'correlation_id': correlation_id, 'date_end': date_end, 'date_start': date_start, 'etl_start': datetime.now(), 'etl_status': status.STARTED} 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 cable_revenue_ingestion_etl_log SET workflow_run_id = %(workflow_run_id)s WHERE correlation_id = %(correlation_id)s""" params = { 'correlation_id': correlation_id, 'workflow_run_id': workflow_run_id} datastore.execute(sql, params) def update_status(correlation_id, status, end_of_etl=False): """Update ETL log status. Args: correlation_id (str): Correlation ID for ETL process. status (str): new status for the etl log. """ etl_end = None if end_of_etl: etl_end = datetime.now() sql = """ UPDATE cable_revenue_ingestion_etl_log SET etl_status = %(status)s, etl_end = %(etl_end)s WHERE correlation_id = %(correlation_id)s""" params = { 'correlation_id': correlation_id.split('.')[0], 'status': status, 'etl_end': etl_end} g.log.info( 'Updating Cable Revenue Ingestion ETL with correlation ID: ' '"{correlation_id}" to status: "{status}"'.format(**params)) datastore.execute(sql, params) def get_etl_log(correlation_id): """Get the ETL log row from the database. Args: correlation_id (str): Correlation ID for ETL process. """ params = {'correlation_id': correlation_id.split('.')[0]} cursor = datastore.query(queries.SELECT_ETL_LOG, params) return cursor.fetchone()