JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / reporters / report / candy_generator.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from vstf.controller.settings.template_settings import TemplateSettings
11 from vstf.controller.reporters.report.data_factory import TaskData
12 from vstf.controller.database.dbinterface import DbManage
13 import vstf.common.candy_text as candy
14 import logging
15 LOG = logging.getLogger(__name__)
16
17
18 class CandyGenerator(object):
19     def __init__(self, task):
20         self._task = task
21
22     def create(self, scenario):
23         context = {}
24
25         sn = 1
26         chapterid = 1
27         name = candy.tuple2text(sn, candy.chapter, chapterid)
28         context[name] = self.create_env()
29
30         sn += 1
31         chapterid += 1
32         name = candy.tuple2text(sn, candy.chapter, chapterid)
33         context[name] = self.create_scenario(scenario)
34
35         template = TemplateSettings()
36         template.set_context(context)
37         LOG.info(template.settings)
38
39     def create_all(self):
40         context = {}
41
42         sn = 1
43         chapterid = 1
44         name = candy.tuple2text(sn, candy.chapter, chapterid)
45         context[name] = self.create_env()
46
47         scenarios = self._task.common.get_scenariolist()
48         for scenario in scenarios:
49             sn += 1
50             chapterid += 1
51             name = candy.tuple2text(sn, candy.chapter, chapterid)
52             context[name] = self.create_scenario(scenario)
53
54         template = TemplateSettings()
55         template.set_context(context)
56         LOG.info(template.settings)
57
58     def create_env(self):
59         env = {
60             "01##title#1": ["System Environment"],
61             "02##table#2": self._task.common.get_systeminfo()
62         }
63         return env
64
65     def create_scenario(self, scenario):
66         scenario_chapter = {
67             "01##title#1": ["Scenario Result"]
68         }
69         scenario_data = getattr(self._task, scenario)
70         test_list = scenario_data.get_testlist()
71         sectionid = 0
72         sn = 1
73         for test in test_list:
74             sn += 1
75             sectionid += 1
76             name = candy.tuple2text(sn, candy.section, sectionid)
77             testid = test.TestID
78             case = test.CaseTag.decode()
79             ttype = test.Type.decode()
80
81             params_info = [
82                 "  Case: " + case,
83                 "  Test tool: " + test.Tools.decode(),
84                 "  vSwitch: " + test.Switch.decode(),
85                 "  Protocol: " + test.Protocol.decode(),
86                 "  Type: " + ttype
87             ]
88             if ttype in ["frameloss", "throughput"]:
89                 draw = {
90                     "style": 1,
91                     "node": candy.plot,
92                     "data": scenario_data.get_framerate_chartdata(case, ttype)
93                 }
94                 table = scenario_data.get_ratedata(testid, ttype)
95             else:
96                 draw = {
97                     "style": 1,
98                     "node": candy.chart,
99                     "data": scenario_data.get_latency_bardata(case)
100                 }
101                 table = scenario_data.get_latency_tabledata(case)
102             test_section = self.create_test(sectionid, params_info, table, draw)
103             scenario_chapter[name] = test_section
104
105         return scenario_chapter
106
107     def create_test(self, section, info, table, draw):
108         """
109
110         :rtype : dict
111         """
112         sn = 7
113         draw_name = candy.tuple2text(sn, draw["node"], draw["style"])
114         case_section = {
115             "01##title#2": ["Test ID: %s" % section],
116             "02##paragraph#2": ["Parameter"],
117             "03##paragraph#3": info,
118             "04##paragraph#2": ["Result"],
119             "05##table#2": table,
120             "06##space#2": 2,
121             draw_name: draw["data"]
122         }
123         return case_section
124
125
126 def main():
127     from vstf.common.log import setup_logging
128     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-candy.log", clevel=logging.INFO)
129
130     dbase = DbManage()
131     taskid = dbase.get_last_taskid()
132     task = TaskData(taskid, dbase)
133     creator = CandyGenerator(task)
134
135     creator.create("Tn")
136 if __name__ == '__main__':
137     main()
138