Merge "Remove redundant comment"
[yardstick.git] / yardstick / benchmark / core / report.py
1 ##############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka <4k.rajesh@gmail.com>
3 # Copyright (c) 2018 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 _generate_common(self, args):
125         """Actions that are common to both report formats.
126
127         Create the necessary data structure for rendering
128         the report templates.
129         """
130         self._validate(args.yaml_name[0], args.task_id[0])
131
132         db_fieldkeys = self._get_fieldkeys()
133         # list of dicts of:
134         # - PY2: unicode key and unicode value
135         # - PY3: str key and str value
136
137         db_metrics = self._get_metrics()
138         # list of dicts of:
139         # - PY2: unicode key and { None | unicode | float | long | int } value
140         # - PY3: str key and { None | str | float | int } value
141
142         # extract fieldKey entries, and convert them to str where needed
143         field_keys = [key if isinstance(key, str)       # PY3: already str
144                           else key.encode('utf8')       # PY2: unicode to str
145                       for key in
146                           [field['fieldKey']
147                            for field in db_fieldkeys]]
148
149         # extract timestamps
150         self.Timestamp = []
151         for metric in db_metrics:
152             metric_time = metric['time']                    # in RFC3339 format
153             if not isinstance(metric_time, str):
154                 metric_time = metric_time.encode('utf8')    # PY2: unicode to str
155             metric_time = metric_time[11:]                  # skip date, keep time
156             head, _, tail = metric_time.partition('.')      # split HH:MM:SS and nsZ
157             metric_time = head + '.' + tail[:6]             # join HH:MM:SS and .us
158             self.Timestamp.append(metric_time)              # HH:MM:SS.micros
159
160         # prepare return values
161         datasets = []
162         table_vals = {'Timestamp': self.Timestamp}
163
164         # extract and convert field values
165         for key in field_keys:
166             values = []
167             for metric in db_metrics:
168                 val = metric.get(key, None)
169                 if val is None:
170                     # keep explicit None or missing entry as is
171                     pass
172                 elif isinstance(val, (int, float)):
173                     # keep plain int or float as is
174                     pass
175                 elif six.PY2 and isinstance(val,
176                             long):  # pylint: disable=undefined-variable
177                     # PY2: long value would be rendered with trailing L,
178                     # which JS does not support, so convert it to float
179                     val = float(val)
180                 elif isinstance(val, six.string_types):
181                     s = val
182                     if not isinstance(s, str):
183                         s = s.encode('utf8')            # PY2: unicode to str
184                     try:
185                         # convert until failure
186                         val = s
187                         val = float(s)
188                         val = int(s)
189                         if six.PY2 and isinstance(val,
190                                     long):  # pylint: disable=undefined-variable
191                             val = float(val)            # PY2: long to float
192                     except ValueError:
193                         pass
194                 else:
195                     raise ValueError("Cannot convert %r" % val)
196                 values.append(val)
197             datasets.append({'label': key, 'data': values})
198             table_vals[key] = values
199
200         return datasets, table_vals
201
202     @cliargs("task_id", type=str, help=" task id", nargs=1)
203     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
204     def generate(self, args):
205         """Start report generation."""
206         datasets, table_vals = self._generate_common(args)
207
208         template_dir = consts.YARDSTICK_ROOT_PATH + "yardstick/common"
209         template_environment = jinja2.Environment(
210             autoescape=False,
211             loader=jinja2.FileSystemLoader(template_dir))
212
213         context = {
214             "datasets": datasets,
215             "Timestamps": self.Timestamp,
216             "task_id": self.task_id,
217             "table": table_vals,
218         }
219
220         template_html = template_environment.get_template("report.html.j2")
221
222         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
223             file_open.write(template_html.render(context))
224
225         print("Report generated. View %s" % consts.DEFAULT_HTML_FILE)
226
227     @cliargs("task_id", type=str, help=" task id", nargs=1)
228     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
229     def generate_nsb(self, args):
230         """Start NSB report generation."""
231         _, report_data = self._generate_common(args)
232         report_time = report_data.pop('Timestamp')
233         report_keys = sorted(report_data, key=str.lower)
234         report_tree = JSTree().format_for_jstree(report_keys)
235         report_meta = {
236             "testcase": self.yaml_name,
237             "task_id": self.task_id,
238         }
239
240         template_dir = consts.YARDSTICK_ROOT_PATH + "yardstick/common"
241         template_environment = jinja2.Environment(
242             autoescape=False,
243             loader=jinja2.FileSystemLoader(template_dir),
244             lstrip_blocks=True)
245
246         context = {
247             "report_meta": report_meta,
248             "report_data": report_data,
249             "report_time": report_time,
250             "report_keys": report_keys,
251             "report_tree": report_tree,
252         }
253
254         template_html = template_environment.get_template("nsb_report.html.j2")
255
256         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
257             file_open.write(template_html.render(context))
258
259         print("Report generated. View %s" % consts.DEFAULT_HTML_FILE)