from datetime import datetime, timedelta import click from click import secho from delphi_es_utils.core.utils import printf_code, printf_keyval from delphi_es_utils.errors.exceptions import IndexManagerException from delphi_es_utils.services import IndexManager from delphi_es_utils.settings import ENVIRONMENT @click.command('task-status', short_help=f'Get the status of a running task.') @click.option('--task', required=True, help='Full task id in format "node:task_id"') @click.option('--env', default=ENVIRONMENT, help='Environment override') def task_status_cmd(task, env=ENVIRONMENT): try: result = IndexManager().get_status(task) except IndexManagerException as e: secho('**✘ Getting task status failed**') secho(str(e)) exit(1) except Exception as e: secho('**✘ Getting task status failed**') printf_code(e) exit(1) else: if result: secho(f'**Here is the current task summary for task ID `{task}`**') printf_keyval('Total', result.total) printf_keyval('Processed', result.processed) printf_keyval('Progress', f'{result.progress}%') printf_keyval('Completed?', result.completed) if not result.completed: time_remaining = timedelta(seconds=result.time_remaining) estimated_completion = datetime.utcnow() + time_remaining printf_keyval('Est. Time Remaining', str(time_remaining)) printf_keyval('Est. Completion (UTC)', estimated_completion.strftime('%c')) exit(0)