Merge "Add test case file and document of Tardstick TC056(HA_TC013)"
[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("^[a-z0-9_-]+$", 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, 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])
110                 else:
111                     values.append(ast.literal_eval(task[key]))
112             table_vals['Timestamp'] = self.Timestamp
113             table_vals[key] = values
114             series['name'] = key
115             series['data'] = values
116             temp_series.append(series)
117
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))
125
126         print("Report generated. View /tmp/yardstick.htm")