<commit_before># Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

""" Rally command: task """

from __future__ import print_function

import collections
import json
import math
import os
import pprint
import prettytable
import sys
import webbrowser

import yaml

from rally.benchmark.processing import plot
from rally.cmd import cliutils
from rally.cmd import envutils
from rally import db
from rally import exceptions
from rally.openstack.common.gettextutils import _
from rally.orchestrator import api


class TaskCommands(object):

    @cliutils.args('--deploy-id', type=str, dest='deploy_id', required=False,
                   help='UUID of the deployment')
    @cliutils.args('--task',
                   help='Path to the file with full configuration of task')
    @cliutils.args('--tag',
                   help='Tag for this task')
    @envutils.deploy_id_default
    def start(self, task, deploy_id=None, tag=None):
        """Run a benchmark task.

        :param task: a file with yaml/json configration
        :param deploy_id: a UUID of a deployment
        :param tag: optional tag for this task
        """
        with open(task, 'rb') as task_file:
            config_dict = yaml.safe_load(task_file.read())
            try:
                task = api.create_task(deploy_id, tag)
                self.list(task_list=[task])
                api.start_task(deploy_id, config_dict, task=task)
                self.detailed(task_id=task['uuid'])
            except exceptions.InvalidArgumentsException:
                print(_("Reason: %s") % sys.exc_info()[1])

    @cliutils.args('--task-id', type=str, dest='task_id', help='UUID of task')
    def abort(self, task_id):
        """Force abort task

        :param task_id: Task uuid
        """
        api.abort_task(task_id)

    @cliutils.args('--task-id', type=str, dest='task_id', help='UUID of task')
    def status(self, task_id):
        """Get status of task

        :param task_id: Task uuid
        Returns current status of task
        """
        task = db.task_get(task_id)
        print(_("Task %(task_id)s is %(status)s.")
              % {'task_id': task_id, 'status': task['status']})

    @cliutils.args(
        '--task-id', type=str, dest='task_id',
        help=('uuid of task, if --task-id is "last" results of most '
              'recently created task will be displayed.'))
    def detailed(self, task_id):
        """Get detailed information about task

        :param task_id: Task uuid
        Prints detailed information of task.
        """
        def _print_atomic_actions_time(raw):
            atime_merged = []
            for r in raw:
                if 'atomic_actions_time' in r:
                    for a in r['atomic_actions_time']:
                        atime_merged.append(a)

            times_by_action = collections.defaultdict(list)
            for at in atime_merged:
                times_by_action[at['action']].append(at['duration'])
            if times_by_action:
                atomic_action_table = prettytable.PrettyTable(
                                                            ['action',
                                                             'count',
                                                             'max (sec)',
                                                             'avg (sec)',
                                                             'min (sec)',
                                                             '90 percentile',
                                                             '95 percentile'])
                for k, v in times_by_action.iteritems():
                    atomic_action_table.add_row([k,
                                                len(v),
                                                max(v),
                                                sum(v) / len(v),
                                                min(v),
                                                percentile(v, 0.90),
                                                percentile(v, 0.95)])
                print(atomic_action_table)
                print()

        if task_id == "last":
            task = db.task_get_detailed_last()
            task_id = task.uuid
        else:
            task = db.task_get_detailed(task_id)<commit_msg>Show atomic operations results without aggregation with CLI<commit_after># Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

""" Rally command: task """

from __future__ import print_function

import collections
import json
import math
import os
import pprint
import prettytable
import sys
import webbrowser

import yaml

from rally.benchmark.processing import plot
from rally.cmd import cliutils
from rally.cmd import envutils
from rally import db
from rally import exceptions
from rally.openstack.common.gettextutils import _
from rally.orchestrator import api


class TaskCommands(object):

    @cliutils.args('--deploy-id', type=str, dest='deploy_id', required=False,
                   help='UUID of the deployment')
    @cliutils.args('--task',
                   help='Path to the file with full configuration of task')
    @cliutils.args('--tag',
                   help='Tag for this task')
    @envutils.deploy_id_default
    def start(self, task, deploy_id=None, tag=None):
        """Run a benchmark task.

        :param task: a file with yaml/json configration
        :param deploy_id: a UUID of a deployment
        :param tag: optional tag for this task
        """
        with open(task, 'rb') as task_file:
            config_dict = yaml.safe_load(task_file.read())
            try:
                task = api.create_task(deploy_id, tag)
                self.list(task_list=[task])
                api.start_task(deploy_id, config_dict, task=task)
                self.detailed(task_id=task['uuid'])
            except exceptions.InvalidArgumentsException:
                print(_("Reason: %s") % sys.exc_info()[1])

    @cliutils.args('--task-id', type=str, dest='task_id', help='UUID of task')
    def abort(self, task_id):
        """Force abort task

        :param task_id: Task uuid
        """
        api.abort_task(task_id)

    @cliutils.args('--task-id', type=str, dest='task_id', help='UUID of task')
    def status(self, task_id):
        """Get status of task

        :param task_id: Task uuid
        Returns current status of task
        """
        task = db.task_get(task_id)
        print(_("Task %(task_id)s is %(status)s.")
              % {'task_id': task_id, 'status': task['status']})

    @cliutils.args(
        '--task-id', type=str, dest='task_id',
        help=('uuid of task, if --task-id is "last" results of most '
              'recently created task will be displayed.'))
    @cliutils.args('--no-aggregation', dest='no_aggregation',
                   action='store_true',
                   help='do not aggregate atomic operation results')
    def detailed(self, task_id, no_aggregation=False):
        """Get detailed information about task

        :param task_id: Task uuid
        :param no_aggregation: do not aggregate atomic operations
        Prints detailed information of task.
        """
        def _print_atomic_actions_time_no_aggregation(raw):
            headers = ['iteration', "full duration"]
            for i in range(0, len(raw)):
                if raw[i]['atomic_actions_time']:
                    for (c, a) in enumerate(raw[i]['atomic_actions_time'], 1):
                        action = str(c) + "-" + a['action']
                        headers.append(action)
                    break
            atomic_action_table = prettytable.PrettyTable(headers)
            for (c, r) in enumerate(raw, 1):
                dlist = [c]
                d = []
                if r['atomic_actions_time']:
                    for l in r['atomic_actions_time']:
                        d.append(l['duration'])
                    dlist.append(sum(d))
                    dlist = dlist + d
                    atomic_action_table.add_row(dlist)
                else:
                    atomic_action_table.add_row(dlist +
                                                ["N/A" for i in
                                                range(1, len(headers))])
            print(atomic_action_table)
            print()

        def _print_atomic_actions_time_aggregation(raw):
            atime_merged = []
            for r in raw:
                if 'atomic_actions_time' in r:
                    for a in r['atomic_actions_time']:
                        atime_merged.append(a)

            times_by_action = collections.defaultdict(list)
            for at in atime_merged:
                times_by_action[at['action']].append(at['duration'])
            if times_by_action:
                atomic_action_table = prettytable.PrettyTable(
                                                        ['action',
                                                         'count',
                                                         'max (sec)',
                                                         'avg (sec)',
                                                         'min (sec)',
                                                         '90 percentile',
                                                         '95 percentile'])
                for k, v in times_by_action.iteritems():
                    atomic_action_table.add_row([k,
                                                len(v),
                                                max(v),
                                                sum(v) / len(v),
                                                min(v),
                                                percentile(v, 0.90),
                                                percentile(v, 0.95)])
                print(atomic_action_table)
                print()

        def _print_atomic_actions_time(raw):
            if no_aggregation:
                _print_atomic_actions_time_no_aggregation(raw)
            else:
                _print_atomic_actions_time_aggregation(raw)

        if task_id == "last":
            task = db.task_get_detailed_last()
            task_id = task.uuid
        else:
            task = db.task_get_detailed(task_id)
{'fraction_non_alphanumeric': 0.09176965553510387, 'fraction_numerical': 0.0036813042334998686, 'mean_word_length': 1.508243570015388, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4090960', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2781, 'n_words': 821}