aboutsummaryrefslogtreecommitdiffstats
path: root/qtip/cli/commands/cmd_report.py
blob: cca1283c3794c215087fd68aceb21aba61c7ba4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
##############################################################################
# Copyright (c) 2017 taseer94@gmail.com and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from asq.initiators import query
import click
from prettytable import PrettyTable

from qtip.reporter.console import ConsoleReporter


def extract_section(qpi, section_name, node):
    """ Extract information related to QPI """
    data = query(qpi).where(lambda nodes: nodes['name'] == node) \
        .select_many(lambda section: section['sections']) \
        .where(lambda section: section['name'] == section_name) \
        .first()

    return data


def display_report(report, section_name, node):
    table_workload = PrettyTable(['Workload', 'Description',
                                  'Result', 'Score'])
    table_workload.align = 'l'

    section_report = extract_section(report['nodes'], section_name, node)

    for metric in section_report['metrics']:
        for wl in metric['workloads']:
            table_workload.add_row(['{}.{}'.format(metric['name'],
                                                   wl['name']),
                                    wl['description'],
                                    wl['result'],
                                    wl['score']])
    return {
        "ss": section_report['score'],
        "desc": section_report['description'],
        "table": table_workload
    }


@click.group()
def cli():
    """ View QTIP results"""
    pass


@cli.command('show')
@click.option('-n', '--node', help="Compute node in OPNFV cluster")
@click.argument('section-name')
def show(node, section_name):
    qpi = ConsoleReporter.load_result()
    result = display_report(qpi, section_name, node)

    click.echo("Node Score: {}".format(qpi['score']))
    click.echo("Section Score: {}".format(result['ss']))
    click.echo("Description: {}".format(result['desc']))
    click.echo(result['table'])