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' """
13 from __future__ import print_function
15 from __future__ import absolute_import
21 from api.utils import influx
23 from django.conf import settings
24 from django.template import Context
25 from django.template import Template
27 from oslo_utils import encodeutils
28 from oslo_utils import uuidutils
29 from yardstick.common import constants as consts
30 from yardstick.common.html_template import template
31 from yardstick.common.utils import cliargs
39 Set of commands to manage benchmark tasks.
47 def _validate(self, yaml_name, task_id):
48 if re.match("^[a-z0-9_-]+$", yaml_name):
49 self.yaml_name = yaml_name
51 raise ValueError("invalid yaml_name", yaml_name)
53 if uuidutils.is_uuid_like(task_id):
54 task_id = '{' + task_id + '}'
55 task_uuid = (uuid.UUID(task_id))
56 self.task_id = task_uuid
58 raise ValueError("invalid task_id", task_id)
60 def _get_fieldkeys(self):
61 fieldkeys_cmd = "show field keys from \"%s\""
62 fieldkeys_query = fieldkeys_cmd % (self.yaml_name)
63 query_exec = influx.query(fieldkeys_query)
67 raise KeyError("Task ID or Test case not found..")
70 task_cmd = "select * from \"%s\" where task_id= '%s'"
71 task_query = task_cmd % (self.yaml_name, self.task_id)
72 query_exec = influx.query(task_query)
76 raise KeyError("Task ID or Test case not found..")
78 @cliargs("task_id", type=str, help=" task id", nargs=1)
79 @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
80 def generate(self, args):
81 """Start report generation."""
82 self._validate(args.yaml_name[0], args.task_id[0])
84 self.db_fieldkeys = self._get_fieldkeys()
86 self.db_task = self._get_tasks()
92 field_keys = [encodeutils.to_utf8(field['fieldKey'])
93 for field in self.db_fieldkeys]
95 for key in field_keys:
99 for task in self.db_task:
100 task_time = encodeutils.to_utf8(task['time'])
101 if not isinstance(task_time, str):
102 task_time = str(task_time, 'utf8')
103 key = str(key, 'utf8')
104 task_time = task_time[11:]
105 head, sep, tail = task_time.partition('.')
106 task_time = head + "." + tail[:6]
107 self.Timestamp.append(task_time)
108 if isinstance(task[key], float) is True:
109 values.append(task[key])
111 values.append(ast.literal_eval(task[key]))
112 table_vals['Timestamp'] = self.Timestamp
113 table_vals[key] = values
115 series['data'] = values
116 temp_series.append(series)
118 Template_html = Template(template)
119 Context_html = Context({"series": temp_series,
120 "Timestamp": self.Timestamp,
121 "task_id": self.task_id,
122 "table": table_vals})
123 with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
124 file_open.write(Template_html.render(Context_html))
126 print("Report generated. View /tmp/yardstick.htm")