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' """
17 from api.utils import influx
19 from django.conf import settings
20 from django.template import Context
21 from django.template import Template
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
35 Set of commands to manage benchmark tasks.
43 def _validate(self, yaml_name, task_id):
44 if re.match(r"^[\w-]+$", yaml_name):
45 self.yaml_name = yaml_name
47 raise ValueError("invalid yaml_name", yaml_name)
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
54 raise ValueError("invalid task_id", task_id)
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)
63 raise KeyError("Test case not found.")
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)
72 raise KeyError("Task ID or Test case not found.")
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])
80 self.db_fieldkeys = self._get_fieldkeys()
82 self.db_task = self._get_tasks()
88 field_keys = [encodeutils.to_utf8(field['fieldKey'])
89 for field in self.db_fieldkeys]
91 for key in field_keys:
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:
106 elif isinstance(task[key], (int, float)) is True:
107 values.append(task[key])
109 values.append(ast.literal_eval(task[key]))
110 table_vals['Timestamp'] = self.Timestamp
111 table_vals[key] = values
113 series['data'] = values
114 temp_series.append(series)
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))
124 print("Report generated. View /tmp/yardstick.htm")