Update unit tests for yardstick/benchmark/core/report.py
[yardstick.git] / yardstick / benchmark / core / report.py
1 #############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka
3 #
4 # Author: Rajesh Kudaka 4k.rajesh@gmail.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 """ Handler for yardstick command 'report' """
12
13 import ast
14 import re
15 import uuid
16
17 from api.utils import influx
18
19 from django.conf import settings
20 from django.template import Context
21 from django.template import Template
22
23 from oslo_utils import encodeutils
24 from oslo_utils import uuidutils
25 from yardstick.common import constants as consts
26 from yardstick.common.html_template import template
27 from yardstick.common.utils import cliargs
28
29 settings.configure()
30
31
32 class Report(object):
33     """Report commands.
34
35     Set of commands to manage benchmark tasks.
36     """
37
38     def __init__(self):
39         self.Timestamp = []
40         self.yaml_name = ""
41         self.task_id = ""
42
43     def _validate(self, yaml_name, task_id):
44         if re.match(r"^[\w-]+$", yaml_name):
45             self.yaml_name = yaml_name
46         else:
47             raise ValueError("invalid yaml_name", yaml_name)
48
49         if uuidutils.is_uuid_like(task_id):
50             task_id = '{' + task_id + '}'
51             task_uuid = (uuid.UUID(task_id))
52             self.task_id = task_uuid
53         else:
54             raise ValueError("invalid task_id", task_id)
55
56     def _get_fieldkeys(self):
57         fieldkeys_cmd = "show field keys from \"%s\""
58         fieldkeys_query = fieldkeys_cmd % (self.yaml_name)
59         query_exec = influx.query(fieldkeys_query)
60         if query_exec:
61             return query_exec
62         else:
63             raise KeyError("Test case not found.")
64
65     def _get_tasks(self):
66         task_cmd = "select * from \"%s\" where task_id= '%s'"
67         task_query = task_cmd % (self.yaml_name, self.task_id)
68         query_exec = influx.query(task_query)
69         if query_exec:
70             return query_exec
71         else:
72             raise KeyError("Task ID or Test case not found.")
73
74     @cliargs("task_id", type=str, help=" task id", nargs=1)
75     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
76     def generate(self, args):
77         """Start report generation."""
78         self._validate(args.yaml_name[0], args.task_id[0])
79
80         self.db_fieldkeys = self._get_fieldkeys()
81
82         self.db_task = self._get_tasks()
83
84         field_keys = []
85         temp_series = []
86         table_vals = {}
87
88         field_keys = [encodeutils.to_utf8(field['fieldKey'])
89                       for field in self.db_fieldkeys]
90
91         for key in field_keys:
92             self.Timestamp = []
93             series = {}
94             values = []
95             for task in self.db_task:
96                 task_time = encodeutils.to_utf8(task['time'])
97                 if not isinstance(task_time, str):
98                     task_time = str(task_time, 'utf8')
99                     key = str(key, 'utf8')
100                 task_time = task_time[11:]
101                 head, _, tail = task_time.partition('.')
102                 task_time = head + "." + tail[:6]
103                 self.Timestamp.append(task_time)
104                 if task[key] is None:
105                     values.append('')
106                 elif isinstance(task[key], (int, float)) is True:
107                     values.append(task[key])
108                 else:
109                     values.append(ast.literal_eval(task[key]))
110             table_vals['Timestamp'] = self.Timestamp
111             table_vals[key] = values
112             series['name'] = key
113             series['data'] = values
114             temp_series.append(series)
115
116         Template_html = Template(template)
117         Context_html = Context({"series": temp_series,
118                                 "Timestamp": self.Timestamp,
119                                 "task_id": self.task_id,
120                                 "table": table_vals})
121         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
122             file_open.write(Template_html.render(Context_html))
123
124         print("Report generated. View /tmp/yardstick.htm")