def print_last_process_state_change(process_type=None): """ Print the last time that a process of the specified type has changed its state. This function will also print a warning if the daemon is not running. :param process_type: optional process type for which to get the latest state change timestamp. Valid process types are either 'calculation' or 'work'. """ from aiida.cmdline.utils.echo import echo_info, echo_warning from aiida.daemon.client import DaemonClient from aiida.utils import timezone from aiida.common.utils import str_timedelta from aiida.work.utils import get_process_state_change_timestamp client = DaemonClient() timestamp = get_process_state_change_timestamp(process_type) if timestamp is None: echo_info('last time an entry changed state: never') else: timedelta = timezone.delta(timestamp, timezone.now()) formatted = format_local_time(timestamp, format_str='at %H:%M:%S on %Y-%m-%d') relative = str_timedelta(timedelta, negative_to_zero=True, max_num_fields=1) echo_info('last time an entry changed state: {} ({})'.format( relative, formatted)) if not client.is_daemon_running: echo_warning('the daemon is not running', bold=True)
def do_list(past_days, limit): """ Return a list of running workflows on screen """ from aiida.common.utils import str_timedelta from aiida.backends.utils import load_dbenv, is_dbenv_loaded if not is_dbenv_loaded(): load_dbenv() import aiida.utils.timezone as timezone from aiida.orm.mixins import SealableMixin _SEALED_ATTRIBUTE_KEY = 'attributes.{}'.format(SealableMixin.SEALED_KEY) now = timezone.now() table = [] for res in _build_query(limit=limit, past_days=past_days): calc = res['calculation'] creation_time = str_timedelta(timezone.delta(calc['ctime'], now), negative_to_zero=True, max_num_fields=1) table.append([ calc['id'], creation_time, calc['type'], str(calc[_SEALED_ATTRIBUTE_KEY]) ]) print(tabulate(table, headers=["PID", "Creation time", "Type", "Sealed"]))
def format_relative_time(datetime): """ Return a string formatted timedelta of the given datetime with respect to the current datetime :param datetime: the datetime to format :return: string representation of the relative time since the given datetime """ from aiida.common.utils import str_timedelta from aiida.utils import timezone timedelta = timezone.delta(datetime, timezone.now()) return str_timedelta(timedelta, negative_to_zero=True, max_num_fields=1)
def do_list(past_days, all_nodes, limit): """ Return a list of running workflows on screen """ from aiida.common.utils import str_timedelta from aiida.backends.utils import load_dbenv, is_dbenv_loaded if not is_dbenv_loaded(): load_dbenv() import aiida.utils.timezone as timezone from aiida.orm.mixins import Sealable _SEALED_ATTRIBUTE_KEY = 'attributes.{}'.format(Sealable.SEALED_KEY) now = timezone.now() if all_nodes: past_days = None table = [] for res in _build_query(limit=limit, past_days=past_days, order_by={'ctime': 'desc'}): calc = res['calculation'] creation_time = str_timedelta(timezone.delta(calc['ctime'], now), negative_to_zero=True, max_num_fields=1) table.append([ calc['id'], creation_time, calc['attributes._process_label'], str(calc[_SEALED_ATTRIBUTE_KEY]) ]) # Revert table: # in this way, I order by 'desc', so I start by the most recent, but then # I print this as the las one (like 'verdi calculation list' does) # This is useful when 'limit' is set to not None table = table[::-1] print( tabulate(table, headers=["PID", "Creation time", "ProcessLabel", "Sealed"]))
def do_list(past_days, all_states, limit, project): """ Return a list of running workflows on screen """ from aiida.backends.utils import load_dbenv, is_dbenv_loaded if not is_dbenv_loaded(): load_dbenv() from aiida.common.utils import str_timedelta from aiida.utils import timezone from aiida.orm.mixins import Sealable from aiida.orm.calculation.work import WorkCalculation _SEALED_ATTRIBUTE_KEY = 'attributes.{}'.format(Sealable.SEALED_KEY) _ABORTED_ATTRIBUTE_KEY = 'attributes.{}'.format( WorkCalculation.ABORTED_KEY) _FAILED_ATTRIBUTE_KEY = 'attributes.{}'.format(WorkCalculation.FAILED_KEY) _FINISHED_ATTRIBUTE_KEY = 'attributes.{}'.format( WorkCalculation.FINISHED_KEY) if not project: project = ('id', 'ctime', 'label', 'state', 'sealed' ) # default projections # Mapping of projections to list table headers. hmap_dict = { 'id': 'PID', 'ctime': 'Creation time', 'label': 'Process Label', 'uuid': 'UUID', 'descr': 'Description', 'mtime': 'Modification time' } def map_header(p): try: return hmap_dict[p] except KeyError: return p.capitalize() # Mapping of querybuilder keys that differ from projections. pmap_dict = { 'label': 'attributes._process_label', 'sealed': _SEALED_ATTRIBUTE_KEY, 'failed': _FAILED_ATTRIBUTE_KEY, 'aborted': _ABORTED_ATTRIBUTE_KEY, 'finished': _FINISHED_ATTRIBUTE_KEY, 'descr': 'description', } def map_projection(p): try: return pmap_dict[p] except KeyError: return p def calculation_state(calculation): if calculation[_FAILED_ATTRIBUTE_KEY]: return 'FAILED' elif calculation[_ABORTED_ATTRIBUTE_KEY]: return 'ABORTED' elif calculation[_FINISHED_ATTRIBUTE_KEY]: return 'FINISHED' else: return 'RUNNING' # Mapping of to-string formatting of projections that do need it. rmap_dict = { 'ctime': lambda calc: str_timedelta(timezone.delta( calc[map_projection('ctime')], now), negative_to_zero=True, max_num_fields=1), 'mtime': lambda calc: str_timedelta(timezone.delta( calc[map_projection('mtime')], now), negative_to_zero=True, max_num_fields=1), 'sealed': lambda calc: str(calc[map_projection('sealed')]), 'state': lambda calc: calculation_state(calc), } def map_result(p, obj): try: return rmap_dict[p](obj) except: return obj[map_projection(p)] mapped_projections = list(map(lambda p: map_projection(p), project)) mapped_projections.extend([ _FAILED_ATTRIBUTE_KEY, _ABORTED_ATTRIBUTE_KEY, _FINISHED_ATTRIBUTE_KEY ]) table = [] for res in _build_query(limit=limit, projections=mapped_projections, past_days=past_days, order_by={'ctime': 'desc'}): calc = res['calculation'] if calc[_SEALED_ATTRIBUTE_KEY] and not all_states: continue table.append(list(map(lambda p: map_result(p, calc), project))) # Since we sorted by descending creation time, we revert the list to print the most # recent entries last table = table[::-1] print( tabulate(table, headers=(list(map(lambda p: map_header(p), project)))))
def do_list(past_days, all_states, limit): """ Return a list of running workflows on screen """ from aiida.backends.utils import load_dbenv, is_dbenv_loaded if not is_dbenv_loaded(): load_dbenv() from aiida.common.utils import str_timedelta from aiida.utils import timezone from aiida.orm.mixins import Sealable from aiida.orm.calculation.work import WorkCalculation _SEALED_ATTRIBUTE_KEY = 'attributes.{}'.format(Sealable.SEALED_KEY) _ABORTED_ATTRIBUTE_KEY = 'attributes.{}'.format( WorkCalculation.ABORTED_KEY) _FAILED_ATTRIBUTE_KEY = 'attributes.{}'.format(WorkCalculation.FAILED_KEY) _FINISHED_ATTRIBUTE_KEY = 'attributes.{}'.format( WorkCalculation.FINISHED_KEY) table = [] for res in _build_query(limit=limit, past_days=past_days, order_by={'ctime': 'desc'}): calculation = res['calculation'] creation_time = str_timedelta(timezone.delta(calculation['ctime'], timezone.now()), negative_to_zero=True, max_num_fields=1) if _SEALED_ATTRIBUTE_KEY in calculation and calculation[ _SEALED_ATTRIBUTE_KEY]: sealed = True else: sealed = False if _FINISHED_ATTRIBUTE_KEY in calculation and calculation[ _FINISHED_ATTRIBUTE_KEY]: state = 'Finished' elif _FAILED_ATTRIBUTE_KEY in calculation and calculation[ _FAILED_ATTRIBUTE_KEY]: state = 'Failed' elif _ABORTED_ATTRIBUTE_KEY in calculation and calculation[ _ABORTED_ATTRIBUTE_KEY]: state = 'Aborted' elif sealed: # If it is not in terminal state but sealed, we have an inconsistent state state = 'Unknown' else: state = 'Running' # By default we only display unsealed entries, unless all_states flag is set if sealed and not all_states: continue table.append([ calculation['id'], creation_time, state, str(sealed), calculation['attributes._process_label'] ]) # Since we sorted by descending creation time, we revert the list to print the most # recent entries last table = table[::-1] print( tabulate(table, headers=['PK', 'Creation', 'State', 'Sealed', 'ProcessLabel']))