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, PostReindexError from delphi_es_utils.services import IndexManager from delphi_es_utils.settings import ENVIRONMENT @click.command('post-reindex', short_help=f'Cleans up after a reindexing job.') @click.option('--name', required=True, help='Name of the index alias.') @click.option('--task', required=True, help='Full task id in format "node:task_id"') @click.option('--env', default=ENVIRONMENT, help='Environment override') def post_reindex_cmd(name, task, env=ENVIRONMENT): try: result = IndexManager(name).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: 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) try: IndexManager().post_reindex(index_name=name, full_task_id=task) except PostReindexError as e: time_remaining = timedelta(seconds=result.time_remaining) estimated_completion = datetime.utcnow() + time_remaining printf_keyval('Est. Remaining', str(time_remaining)) printf_keyval('Est. Completion (UTC)', estimated_completion.strftime('%c')) secho('\n✘ **This task has not completed yet!** Aliases will not be modified.') else: secho('\n**✔︎ The task completed so we updated the aliases.**') secho('You can always undo this change by running the **`rollback`** command:') rollback_cmd = f'rollback --name {name} --env {env}' printf_code(rollback_cmd)