1 #############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka
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 ##############################################################################
11 """ Handler for yardstick command 'report' """
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
28 Set of commands to manage benchmark tasks.
36 def _validate(self, yaml_name, task_id):
37 if re.match(r"^[\w-]+$", yaml_name):
38 self.yaml_name = yaml_name
40 raise ValueError("invalid yaml_name", yaml_name)
42 if uuidutils.is_uuid_like(task_id):
43 task_id = '{' + task_id + '}'
44 task_uuid = (uuid.UUID(task_id))
45 self.task_id = task_uuid
47 raise ValueError("invalid task_id", task_id)
49 def _get_fieldkeys(self):
50 fieldkeys_cmd = "show field keys from \"%s\""
51 fieldkeys_query = fieldkeys_cmd % (self.yaml_name)
52 query_exec = influx.query(fieldkeys_query)
56 raise KeyError("Test case not found.")
59 task_cmd = "select * from \"%s\" where task_id= '%s'"
60 task_query = task_cmd % (self.yaml_name, self.task_id)
61 query_exec = influx.query(task_query)
65 raise KeyError("Task ID or Test case not found.")
67 @cliargs("task_id", type=str, help=" task id", nargs=1)
68 @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
69 def generate(self, args):
70 """Start report generation."""
71 self._validate(args.yaml_name[0], args.task_id[0])
73 self.db_fieldkeys = self._get_fieldkeys()
75 self.db_task = self._get_tasks()
81 field_keys = [encodeutils.to_utf8(field['fieldKey'])
82 for field in self.db_fieldkeys]
84 for key in field_keys:
88 for task in self.db_task:
89 task_time = encodeutils.to_utf8(task['time'])
90 if not isinstance(task_time, str):
91 task_time = str(task_time, 'utf8')
92 key = str(key, 'utf8')
93 task_time = task_time[11:]
94 head, _, tail = task_time.partition('.')
95 task_time = head + "." + tail[:6]
96 self.Timestamp.append(task_time)
99 elif isinstance(task[key], (int, float)) is True:
100 values.append(task[key])
102 values.append(ast.literal_eval(task[key]))
103 table_vals['Timestamp'] = self.Timestamp
104 table_vals[key] = values
106 series['data'] = values
107 temp_series.append(series)
109 template_dir = consts.YARDSTICK_ROOT_PATH + "yardstick/common"
110 template_environment = jinja2.Environment(
112 loader=jinja2.FileSystemLoader(template_dir),
116 "series": temp_series,
117 "Timestamps": self.Timestamp,
118 "task_id": self.task_id,
122 template_html = template_environment.get_template("report.html.j2")
124 with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
125 file_open.write(template_html.render(context))
127 print("Report generated. View /tmp/yardstick.htm")