Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / 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
20     def __init__(self, task):
21         self._task = task
22
23     def create(self, scenario):
24         context = {}
25
26         sn = 1
27         chapterid = 1
28         name = candy.tuple2text(sn, candy.chapter, chapterid)
29         context[name] = self.create_env()
30
31         sn += 1
32         chapterid += 1
33         name = candy.tuple2text(sn, candy.chapter, chapterid)
34         context[name] = self.create_scenario(scenario)
35
36         template = TemplateSettings()
37         template.set_context(context)
38         LOG.info(template.settings)
39
40     def create_all(self):
41         context = {}
42
43         sn = 1
44         chapterid = 1
45         name = candy.tuple2text(sn, candy.chapter, chapterid)
46         context[name] = self.create_env()
47
48         scenarios = self._task.common.get_scenariolist()
49         for scenario in scenarios:
50             sn += 1
51             chapterid += 1
52             name = candy.tuple2text(sn, candy.chapter, chapterid)
53             context[name] = self.create_scenario(scenario)
54
55         template = TemplateSettings()
56         template.set_context(context)
57         LOG.info(template.settings)
58
59     def create_env(self):
60         env = {
61             "01##title#1": ["System Environment"],
62             "02##table#2": self._task.common.get_systeminfo()
63         }
64         return env
65
66     def create_scenario(self, scenario):
67         scenario_chapter = {
68             "01##title#1": ["Scenario Result"]
69         }
70         scenario_data = getattr(self._task, scenario)
71         test_list = scenario_data.get_testlist()
72         sectionid = 0
73         sn = 1
74         for test in test_list:
75             sn += 1
76             sectionid += 1
77             name = candy.tuple2text(sn, candy.section, sectionid)
78             testid = test.TestID
79             case = test.CaseTag.decode()
80             ttype = test.Type.decode()
81
82             params_info = [
83                 "  Case: " + case,
84                 "  Test tool: " + test.Tools.decode(),
85                 "  vSwitch: " + test.Switch.decode(),
86                 "  Protocol: " + test.Protocol.decode(),
87                 "  Type: " + ttype
88             ]
89             if ttype in ["frameloss", "throughput"]:
90                 draw = {
91                     "style": 1,
92                     "node": candy.plot,
93                     "data": scenario_data.get_framerate_chartdata(case, ttype)
94                 }
95                 table = scenario_data.get_ratedata(testid, ttype)
96             else:
97                 draw = {
98                     "style": 1,
99                     "node": candy.chart,
100                     "data": scenario_data.get_latency_bardata(case)
101                 }
102                 table = scenario_data.get_latency_tabledata(case)
103             test_section = self.create_test(
104                 sectionid, params_info, table, draw)
105             scenario_chapter[name] = test_section
106
107         return scenario_chapter
108
109     def create_test(self, section, info, table, draw):
110         """
111
112         :rtype : dict
113         """
114         sn = 7
115         draw_name = candy.tuple2text(sn, draw["node"], draw["style"])
116         case_section = {
117             "01##title#2": ["Test ID: %s" % section],
118             "02##paragraph#2": ["Parameter"],
119             "03##paragraph#3": info,
120             "04##paragraph#2": ["Result"],
121             "05##table#2": table,
122             "06##space#2": 2,
123             draw_name: draw["data"]
124         }
125         return case_section
126
127
128 def main():
129     from vstf.common.log import setup_logging
130     setup_logging(
131         level=logging.DEBUG,
132         log_file="/var/log/vstf/vstf-candy.log",
133         clevel=logging.INFO)
134
135     dbase = DbManage()
136     taskid = dbase.get_last_taskid()
137     task = TaskData(taskid, dbase)
138     creator = CandyGenerator(task)
139
140     creator.create("Tn")
141 if __name__ == '__main__':
142     main()