Use Chart.js for graphs in HTML reports
[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 ast
14 import re
15 import uuid
16
17 import jinja2
18 from api.utils import influx
19 from oslo_utils import encodeutils
20 from oslo_utils import uuidutils
21 from yardstick.common import constants as consts
22 from yardstick.common.utils import cliargs
23
24
25 class JSTree(object):
26     """Data structure to parse data for use with the JS library jsTree"""
27     def __init__(self):
28         self._created_nodes = ['#']
29         self.jstree_data = []
30
31     def _create_node(self, _id):
32         """Helper method for format_for_jstree to create each node.
33
34         Creates the node (and any required parents) and keeps track
35         of the created nodes.
36
37         :param _id: (string) id of the node to be created
38         :return: None
39         """
40         components = _id.split(".")
41
42         if len(components) == 1:
43             text = components[0]
44             parent_id = "#"
45         else:
46             text = components[-1]
47             parent_id = ".".join(components[:-1])
48             # make sure the parent has been created
49             if not parent_id in self._created_nodes:
50                 self._create_node(parent_id)
51
52         self.jstree_data.append({"id": _id, "text": text, "parent": parent_id})
53         self._created_nodes.append(_id)
54
55     def format_for_jstree(self, data):
56         """Format the data into the required format for jsTree.
57
58         The data format expected is a list of key-value pairs which represent
59         the data and name for each metric e.g.:
60
61             [{'data': [0, ], 'name': 'tg__0.DropPackets'},
62              {'data': [548, ], 'name': 'tg__0.LatencyAvg.5'},]
63
64         This data is converted into the format required for jsTree to group and
65         display the metrics in a hierarchial fashion, including creating a
66         number of parent nodes e.g.::
67
68             [{"id": "tg__0", "text": "tg__0", "parent": "#"},
69              {"id": "tg__0.DropPackets", "text": "DropPackets", "parent": "tg__0"},
70              {"id": "tg__0.LatencyAvg", "text": "LatencyAvg", "parent": "tg__0"},
71              {"id": "tg__0.LatencyAvg.5", "text": "5", "parent": "tg__0.LatencyAvg"},]
72
73         :param data: (list) data to be converted
74         :return: list
75         """
76         self._created_nodes = ['#']
77         self.jstree_data = []
78
79         for item in data:
80             self._create_node(item["name"])
81
82         return self.jstree_data
83
84
85 class Report(object):
86     """Report commands.
87
88     Set of commands to manage benchmark tasks.
89     """
90
91     def __init__(self):
92         self.Timestamp = []
93         self.yaml_name = ""
94         self.task_id = ""
95
96     def _validate(self, yaml_name, task_id):
97         if re.match(r"^[\w-]+$", yaml_name):
98             self.yaml_name = yaml_name
99         else:
100             raise ValueError("invalid yaml_name", yaml_name)
101
102         if uuidutils.is_uuid_like(task_id):
103             task_id = '{' + task_id + '}'
104             task_uuid = (uuid.UUID(task_id))
105             self.task_id = task_uuid
106         else:
107             raise ValueError("invalid task_id", task_id)
108
109     def _get_fieldkeys(self):
110         fieldkeys_cmd = "show field keys from \"%s\""
111         fieldkeys_query = fieldkeys_cmd % (self.yaml_name)
112         query_exec = influx.query(fieldkeys_query)
113         if query_exec:
114             return query_exec
115         else:
116             raise KeyError("Test case not found.")
117
118     def _get_tasks(self):
119         task_cmd = "select * from \"%s\" where task_id= '%s'"
120         task_query = task_cmd % (self.yaml_name, self.task_id)
121         query_exec = influx.query(task_query)
122         if query_exec:
123             return query_exec
124         else:
125             raise KeyError("Task ID or Test case not found.")
126
127     @cliargs("task_id", type=str, help=" task id", nargs=1)
128     @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
129     def generate(self, args):
130         """Start report generation."""
131         self._validate(args.yaml_name[0], args.task_id[0])
132
133         self.db_fieldkeys = self._get_fieldkeys()
134
135         self.db_task = self._get_tasks()
136
137         field_keys = []
138         datasets = []
139         table_vals = {}
140
141         field_keys = [encodeutils.to_utf8(field['fieldKey'])
142                       for field in self.db_fieldkeys]
143
144         for key in field_keys:
145             self.Timestamp = []
146             values = []
147             for task in self.db_task:
148                 task_time = encodeutils.to_utf8(task['time'])
149                 if not isinstance(task_time, str):
150                     task_time = str(task_time, 'utf8')
151                     key = str(key, 'utf8')
152                 task_time = task_time[11:]
153                 head, _, tail = task_time.partition('.')
154                 task_time = head + "." + tail[:6]
155                 self.Timestamp.append(task_time)
156                 if task[key] is None:
157                     values.append(None)
158                 elif isinstance(task[key], (int, float)) is True:
159                     values.append(task[key])
160                 else:
161                     values.append(ast.literal_eval(task[key]))
162             datasets.append({'label': key, 'data': values})
163             table_vals['Timestamp'] = self.Timestamp
164             table_vals[key] = values
165
166         template_dir = consts.YARDSTICK_ROOT_PATH + "yardstick/common"
167         template_environment = jinja2.Environment(
168             autoescape=False,
169             loader=jinja2.FileSystemLoader(template_dir),
170             trim_blocks=False)
171
172         context = {
173             "datasets": datasets,
174             "Timestamps": self.Timestamp,
175             "task_id": self.task_id,
176             "table": table_vals,
177         }
178
179         template_html = template_environment.get_template("report.html.j2")
180
181         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
182             file_open.write(template_html.render(context))
183
184         print("Report generated. View /tmp/yardstick.htm")