Merge "Document for Euphrates test case results"
[yardstick.git] / yardstick / benchmark / core / report.py
1 #############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka
3 #
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 ##############################################################################
10
11 """ Handler for yardstick command 'report' """
12
13 from __future__ import print_function
14
15 from __future__ import absolute_import
16
17 import ast
18 import re
19 import uuid
20
21 from api.utils import influx
22
23 from django.conf import settings
24 from django.template import Context
25 from django.template import Template
26
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
32
33 settings.configure()
34
35
36 class Report(object):
37     """Report commands.
38
39     Set of commands to manage benchmark tasks.
40     """
41
42     def __init__(self):
43         self.Timestamp = []
44         self.yaml_name = ""
45         self.task_id = ""
46
47     def _validate(self, yaml_name, task_id):
48         if re.match(r"^[\w-]+$", yaml_name):
49             self.yaml_name = yaml_name
50         else:
51             raise ValueError("invalid yaml_name", yaml_name)
52
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
57         else:
58             raise ValueError("invalid task_id", task_id)
59
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)
64         if query_exec:
65             return query_exec
66         else:
67             raise KeyError("Task ID or Test case not found..")
68
69     def _get_tasks(self):
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)
73         if query_exec:
74             return query_exec
75         else:
76             raise KeyError("Task ID or Test case not found..")
77
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])
83
84         self.db_fieldkeys = self._get_fieldkeys()
85
86         self.db_task = self._get_tasks()
87
88         field_keys = []
89         temp_series = []
90         table_vals = {}
91
92         field_keys = [encodeutils.to_utf8(field['fieldKey'])
93                       for field in self.db_fieldkeys]
94
95         for key in field_keys:
96             self.Timestamp = []
97             series = {}
98             values = []
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, _, tail = task_time.partition('.')
106                 task_time = head + "." + tail[:6]
107                 self.Timestamp.append(task_time)
108                 if task[key] is None:
109                     values.append('')
110                 elif isinstance(task[key], (int, float)) is True:
111                     values.append(task[key])
112                 else:
113                     values.append(ast.literal_eval(task[key]))
114             table_vals['Timestamp'] = self.Timestamp
115             table_vals[key] = values
116             series['name'] = key
117             series['data'] = values
118             temp_series.append(series)
119
120         Template_html = Template(template)
121         Context_html = Context({"series": temp_series,
122                                 "Timestamp": self.Timestamp,
123                                 "task_id": self.task_id,
124                                 "table": table_vals})
125         with open(consts.DEFAULT_HTML_FILE, "w") as file_open:
126             file_open.write(Template_html.render(Context_html))
127
128         print("Report generated. View /tmp/yardstick.htm")