30ba243c38adfdffaca8fe22ccbf0fd50f8dd82f
[yardstick.git] / yardstick / benchmark / core / report.py
1 ##############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka <4k.rajesh@gmail.com>
3 # Copyright (c) 2018-2019 Intel Corporation.
4 #
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 re
14 import six
15 import uuid
16
17 import jinja2
18 from api.utils import influx
19 from oslo_utils import uuidutils
20 from yardstick.common import constants as consts
21 from yardstick.common.utils import cliargs
22
23
24 class JSTree(object):
25     """Data structure to parse data for use with the JS library jsTree"""
26     def __init__(self):
27         self._created_nodes = ['#']
28         self.jstree_data = []
29
30     def _create_node(self, _id):
31         """Helper method for format_for_jstree to create each node.
32
33         Creates the node (and any required parents) and keeps track
34         of the created nodes.
35
36         :param _id: (string) id of the node to be created
37         :return: None
38         """
39         components = _id.split(".")
40
41         if len(components) == 1:
42             text = components[0]
43             parent_id = "#"
44         else:
45             text = components[-1]
46             parent_id = ".".join(components[:-1])
47             # make sure the parent has been created
48             if not parent_id in self._created_nodes:
49                 self._create_node(parent_id)
50
51         self.jstree_data.append({"id": _id, "text": text, "parent": parent_id})
52         self._created_nodes.append(_id)
53
54     def format_for_jstree(self, data):
55         """Format the data into the required format for jsTree.
56
57         The data format expected is a list of metric names e.g.:
58
59             ['tg__0.DropPackets', 'tg__0.LatencyAvg.5']
60
61         This data is converted into the format required for jsTree to group and
62         display the metrics in a hierarchial fashion, including creating a
63         number of parent nodes e.g.::
64
65             [{"id": "tg__0", "text": "tg__0", "parent": "#"},
66              {"id": "tg__0.DropPackets", "text": "DropPackets", "parent": "tg__0"},
67              {"id": "tg__0.LatencyAvg", "text": "LatencyAvg", "parent": "tg__0"},
68              {"id": "tg__0.LatencyAvg.5", "text": "5", "parent": "tg__0.LatencyAvg"},]
69
70         :param data: (list) data to be converted
71         :return: list
72         """
73         self._created_nodes = ['#']
74         self.jstree_data = []
75
76         for metric in data:
77             self._create_node(metric)
78
79         return self.jstree_data
80
81
82 class Report(object):
83     """Report commands.
84
85     Set of commands to manage reports.
86     """
87
88     def __init__(self):
89         self.Timestamp = []
90         self.yaml_name = ""
91         self.task_id = ""
92
93     def _validate(self, yaml_name, task_id):
94         if re.match(r"^[\w-]+$", yaml_name):
95             self.yaml_name = yaml_name
96         else:
97             raise ValueError("invalid yaml_name", yaml_name)
98
99         if uuidutils.is_uuid_like(task_id):
100             task_id = '{' + task_id + '}'
101             task_uuid = (uuid.UUID(task_id))
102             self.task_id = task_uuid
103         else:
104             raise ValueError("invalid task_id", task_id)
105
106     def _get_fieldkeys(self):
107         fieldkeys_cmd = "show field keys from \"%s\""
108         fieldkeys_query = fieldkeys_cmd % (self.yaml_name)
109         query_exec = influx.query(fieldkeys_query)
110         if query_exec:
111             return query_exec
112         else:
113             raise KeyError("Test case not found.")
114
115     def _get_metrics(self):
116         metrics_cmd = "select * from \"%s\" where task_id = '%s'"
117         metrics_query = metrics_cmd % (self.yaml_name, self.task_id)
118         query_exec = influx.query(metrics_query)
119         if query_exec:
120             return query_exec
121         else:
122             raise KeyError("Task ID or Test case not found.")
123
124     def _get_trimmed_timestamp(self, metric_time, resolution=4):
125         if not isinstance(metric_time, str):
126             metric_time = metric_time.encode('utf8') # PY2: unicode to str
127         metric_time = metric_time[11:]               # skip date, keep time
128         head, _, tail = metric_time.partition('.')   # split HH:MM:SS & nsZ
129         metric_time = head + '.' + tail[:resolution] # join HH:MM:SS & .us
130         return metric_time
131
132     def _get_timestamps(self, metrics, resolution=6):
133         # Extract the timestamps from a list of metrics
134         timestamps = []
135         for metric in metrics:
136             metric_time = self._get_trimmed_timestamp(
137                 metric['time'], resolution)
138             timestamps.append(metric_time)               # HH:MM:SS.micros
139         return timestamps
140
141     def _format_datasets(self, metric_name, metrics):
142         values = []
143         for metric in metrics:
144             val = metric.get(metric_name, None)
145             if val is None:
146                 # keep explicit None or missing entry as is
147                 pass
148             elif isinstance(val, (int, float)):
149                 # keep plain int or float as is
150                 pass
151             elif six.PY2 and isinstance(val,
152                         long):  # pylint: disable=undefined-variable
153                 # PY2: long value would be rendered with trailing L,
154                 # which JS does not support, so convert it to float
155                 val = float(val)
156             elif isinstance(val, six.string_types):
157                 s = val
158                 if not isinstance(s, str):
159                     s = s.encode('utf8')            # PY2: unicode to str
160                 try:
161                     # convert until failure
162                     val = s
163                     val = float(s)
164                     val = int(s)
165                     if six.PY2 and isinstance(val,
166                                 long):  # pylint: disable=undefined-variable
167                         val = float(val)            # PY2: long to float
168                 except ValueError:
169                     # value may have been converted to a number
170                     pass
171                 finally:
172                     # if val was not converted into a num, then it must be
173                     # text, which shouldn't end up in the report
174                     if isinstance(val, six.string_types):
175                         val = None
176             else:
177                 raise ValueError("Cannot convert %r" % val)
178             values.append(val)
179         return values
180
181     @cliargs("task_id", type=str, help=" task id", nargs=1)
182     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
183     def _generate_common(self, args):
184         """Actions that are common to both report formats.
185
186         Create the necessary data structure for rendering
187         the report templates.
188         """
189         self._validate(args.yaml_name[0], args.task_id[0])
190
191         db_fieldkeys = self._get_fieldkeys()
192         # list of dicts of:
193         # - PY2: unicode key and unicode value
194         # - PY3: str key and str value
195
196         db_metrics = self._get_metrics()
197         # list of dicts of:
198         # - PY2: unicode key and { None | unicode | float | long | int } value
199         # - PY3: str key and { None | str | float | int } value
200
201         # extract fieldKey entries, and convert them to str where needed
202         field_keys = [key if isinstance(key, str)       # PY3: already str
203                           else key.encode('utf8')       # PY2: unicode to str
204                       for key in
205                           [field['fieldKey']
206                            for field in db_fieldkeys]]
207
208         # extract timestamps
209         self.Timestamp = self._get_timestamps(db_metrics)
210
211         # prepare return values
212         datasets = []
213         table_vals = {'Timestamp': self.Timestamp}
214
215         # extract and convert field values
216         for key in field_keys:
217             values = self._format_datasets(key, db_metrics)
218             datasets.append({'label': key, 'data': values})
219             table_vals[key] = values
220
221         return datasets, table_vals
222
223     @cliargs("task_id", type=str, help=" task id", nargs=1)
224     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
225     def generate(self, args):
226         """Start report generation."""
227         datasets, table_vals = self._generate_common(args)
228
229         template_dir = consts.YARDSTICK_ROOT_PATH + "yardstick/common"
230         template_environment = jinja2.Environment(
231             autoescape=False,
232             loader=jinja2.FileSystemLoader(template_dir))
233
234         context = {
235             "datasets": datasets,
236             "Timestamps": self.Timestamp,
237             "task_id": self.task_id,
238             "table": table_vals,
239         }
240
241         template_html = template_environment.get_template("report.html.j2")
242
243         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
244             file_open.write(template_html.render(context))
245
246         print("Report generated. View %s" % consts.DEFAULT_HTML_FILE)
247
248     @cliargs("task_id", type=str, help=" task id", nargs=1)
249     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
250     def generate_nsb(self, args):
251         """Start NSB report generation."""
252         _, report_data = self._generate_common(args)
253         report_time = report_data.pop('Timestamp')
254         report_keys = sorted(report_data, key=str.lower)
255         report_tree = JSTree().format_for_jstree(report_keys)
256         report_meta = {
257             "testcase": self.yaml_name,
258             "task_id": self.task_id,
259         }
260
261         template_dir = consts.YARDSTICK_ROOT_PATH + "yardstick/common"
262         template_environment = jinja2.Environment(
263             autoescape=False,
264             loader=jinja2.FileSystemLoader(template_dir),
265             lstrip_blocks=True)
266
267         context = {
268             "report_meta": report_meta,
269             "report_data": report_data,
270             "report_time": report_time,
271             "report_keys": report_keys,
272             "report_tree": report_tree,
273         }
274
275         template_html = template_environment.get_template("nsb_report.html.j2")
276
277         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
278             file_open.write(template_html.render(context))
279
280         print("Report generated. View %s" % consts.DEFAULT_HTML_FILE)