<commit_before>import sys
import operator
from functools import partial, wraps
from datetime import datetime

from tabulate import tabulate

from . import WorkflowStats
from simpleflow.history import History


TEMPLATE = '''
Workflow Execution {workflow_id}
Domain: {workflow_type.domain.name}
Workflow Type: {workflow_type.name}

{tag_list}

Total time = {total_time} seconds
'''


TIME_FORMAT = '%Y-%m-%d %H:%M'


def _show_tag_list(tag_list):
    return '\n'.join(
        '{}:\t{}'.format(key.strip(), value.strip()) for key, value in (
            keyval.split('=') for keyval in tag_list
        )
    )


def _to_timestamp(date):
    return (date - datetime(1970, 1, 1)).total_seconds()


def tabular(values, headers, tablefmt, floatfmt):
    return tabulate(
        values,
        headers=headers,
        tablefmt=tablefmt,
        floatfmt=floatfmt,
    )


def csv(values, headers, delimiter=','):
    import csv

    return csv.writer(sys.stdout, delimiter=delimiter).writerows(values)


def human(values, headers):
    return tabulate(
        [(str(k), str(v)) for k, v in zip(headers, values[0])],
        tablefmt='plain',
    )


DEFAULT_FORMAT = partial(tabular, tablefmt='plain', floatfmt='.2f')
FORMATS = {
    'csv': csv,
    'tsv': partial(csv, delimiter='\t'),
    'tabular': DEFAULT_FORMAT,
    'human': human,
}


def get_timestamps(task):
    last_state = task['state']
    timestamp = task[last_state + '_timestamp']
    scheduled_timestamp = task.get('scheduled_timestamp', '')

    return last_state, timestamp, scheduled_timestamp


def info(workflow_execution):
    history = History(workflow_execution.history())
    history.parse()

    first_event = history._tasks[0]
    last_event = history._tasks[-1]
    execution_time = (
        last_event[last_event['state'] + '_timestamp'] -
        first_event[first_event['state'] + '_timestamp']
    ).total_seconds()

    header = (
        'domain',
        'workflow_type.name',
        'workflow_type.version',
        'workflow_id',
        'tag_list',
        'execution_time',
        'input',
    )
    ex = workflow_execution
    rows = [(
        ex.domain.name,
        ex.workflow_type.name,
        ex.workflow_type.version,
        ex.workflow_id,
        ','.join(ex.tag_list),
        execution_time,
        first_event['input'],
    )]
    return header, rows


def profile(workflow_execution, nb_tasks=None):
    stats = WorkflowStats(History(workflow_execution.history()))

    header = (<commit_msg>Fix swf.stats.pretty: handle workflow execution with activity or child workflow<commit_after>import sys
import operator
from functools import partial, wraps
from datetime import datetime

from tabulate import tabulate

from . import WorkflowStats
from simpleflow.history import History


TEMPLATE = '''
Workflow Execution {workflow_id}
Domain: {workflow_type.domain.name}
Workflow Type: {workflow_type.name}

{tag_list}

Total time = {total_time} seconds
'''


TIME_FORMAT = '%Y-%m-%d %H:%M'


def _show_tag_list(tag_list):
    return '\n'.join(
        '{}:\t{}'.format(key.strip(), value.strip()) for key, value in (
            keyval.split('=') for keyval in tag_list
        )
    )


def _to_timestamp(date):
    return (date - datetime(1970, 1, 1)).total_seconds()


def tabular(values, headers, tablefmt, floatfmt):
    return tabulate(
        values,
        headers=headers,
        tablefmt=tablefmt,
        floatfmt=floatfmt,
    )


def csv(values, headers, delimiter=','):
    import csv

    return csv.writer(sys.stdout, delimiter=delimiter).writerows(values)


def human(values, headers):
    return tabulate(
        [(str(k), str(v)) for k, v in zip(headers, values[0])],
        tablefmt='plain',
    )


DEFAULT_FORMAT = partial(tabular, tablefmt='plain', floatfmt='.2f')
FORMATS = {
    'csv': csv,
    'tsv': partial(csv, delimiter='\t'),
    'tabular': DEFAULT_FORMAT,
    'human': human,
}


def get_timestamps(task):
    last_state = task['state']
    timestamp = task[last_state + '_timestamp']
    scheduled_timestamp = task.get('scheduled_timestamp', '')

    return last_state, timestamp, scheduled_timestamp


def info(workflow_execution):
    history = History(workflow_execution.history())
    history.parse()

    if history._tasks:
        first_event = history._tasks[0]
        first_timestamp = first_event[first_event['state'] + '_timestamp']
        last_event = history._tasks[-1]
        last_timestamp = last_event[last_event['state'] + '_timestamp']
        workflow_input = first_event['input']
    else:
        first_event = history.events[0]
        first_timestamp = first_event.timestamp
        last_event = history.events[0]
        last_timestamp = last_event.timestamp
        workflow_input = first_event.input

    execution_time = (last_timestamp - first_timestamp).total_seconds()

    header = (
        'domain',
        'workflow_type.name',
        'workflow_type.version',
        'task_list',
        'workflow_id',
        'run_id',
        'tag_list',
        'execution_time',
        'input',
    )
    ex = workflow_execution
    rows = [(
        ex.domain.name,
        ex.workflow_type.name,
        ex.workflow_type.version,
        ex.task_list,
        ex.workflow_id,
        ex.run_id,
        ','.join(ex.tag_list),
        execution_time,
        workflow_input,
    )]
    return header, rows


def profile(workflow_execution, nb_tasks=None):
    stats = WorkflowStats(History(workflow_execution.history()))

    header = (
{'fraction_non_alphanumeric': 0.12586269524155466, 'fraction_numerical': 0.003995641118779513, 'mean_word_length': 3.0792592592592594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3603622', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1711, 'n_words': 363}