""" ETL Cable Command Line Interface. All required methods to setup and execute the command line for cable related tasks are in here. """ from datetime import date from datetime import timedelta from time import sleep from garcon.activity import ActivityWorker from garcon.decider import DeciderWorker from flows import config as base_config from flows import util from flows.cable_calculation import config from flows.cable_calculation import log from flows.cable_calculation.flow import Flow from flows.flow import DatabaseParam def init_execute_parser(parser): """Setup the 'execute cable' sub-command to the cli. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ parser.add_argument( '-u', '--upc', action='append', dest='upcs', metavar='UPC', type=util.upc_cli_type, help='Release UPC(s) to process. This can be used multiple times.') parser.add_argument( '-s', '--start-date', dest='date_start', metavar='YYYY-MM-DD', type=util.date_cli_type, help=( 'Starting date of data to process, inclusive. ' 'Default is 10 weeks ago.')) parser.add_argument( '-e', '--end-date', dest='date_end', metavar='YYYY-MM-DD', type=util.date_cli_type, help=( 'Ending date of data to process, exclusive. ' "Default is tomorrow's date.")) def run_execute(correlation_id, upcs, date_end, date_start): """CLI entry point to the 'execute cable' sub-command. Args: correlation_id (str): new correlation ID for this ETL. date_end (date): ending date context. date_start (date): starting date context. upcs (tuple): UPCs to process. Returns: str: workflow execution's run ID. """ context = {} context['correlation_id'] = correlation_id context['date_end'] = date_end if date_end else \ (date.today() + timedelta(days=1)).strftime('%Y-%m-%d') context['date_start'] = date_start if date_start else \ (date.today() - timedelta(weeks=10)).strftime('%Y-%m-%d') upcs = tuple(upcs) if upcs else util.get_serviced_upcs(query_based=True) context['upcs'] = DatabaseParam('upcs', data=upcs) log.create(**context) context['correlation_id'] += '.1' resp = util.start_swf_execution( context, config.SWF_WORKFLOW_TIMEOUT, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION) log.add_run_id(correlation_id, resp['runId']) return resp['runId'] def init_decider_parser(parser): """Setup the 'decider cable_calculation' sub-command to the cli. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ pass def run_decider(): """CLI entry point to the 'decider cable_calculation' sub-command.""" decider = DeciderWorker(Flow( base_config.SWF_DOMAIN, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION)) while True: decider.run() sleep(1) def init_worker_parser(parser): """Setup the 'worker cable_calculation' sub-command to the cli. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ pass def run_worker(): """CLI entry point to the 'worker cable_calculation' sub-command.""" worker = ActivityWorker(Flow( base_config.SWF_DOMAIN, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION)) worker.run()