50b3bc656b90d21e348ea2e8bbc78a4f1c173245
[bottlenecks.git] / vstf / vstf / controller / reporters / report / pdf / pdfcreator.py
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 # author: wly
4 # date: 2015-05-29
5 # see license for license details
6 __version__ = ''' '''
7
8 import os
9
10 from vstf.controller.reporters.report.pdf.styles import TemplateStyle
11 from vstf.controller.reporters.report.pdf.pdftemplate import PdfVswitch
12 from vstf.controller.reporters.report.pdf.story import TitleStory, SpaceStory, ImageStory, LineChartStory, \
13     LinePlotStory, uTableStory, Story, TableOfContentsStory, PageBreakStory, ParagraphStory, BarChartStory, cTableStory
14 from vstf.controller.reporters.report.data_factory import CommonData, ScenarioData, HistoryData
15 from vstf.controller.database.dbinterface import DbManage
16 import vstf.controller
17
18
19 class LetterOrder(object):
20     def __init__(self):
21         self.lettertable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22         self._cur = 0
23         self._len = len(self.lettertable)
24
25     def get(self):
26         return self.lettertable[self._cur]
27
28     def pre(self):
29         self._cur = (self._cur + self._len - 1) % self._len
30
31     def next(self):
32         self._cur = (self._cur + 1) % self._len
33
34
35 class PdfBase(object):
36     def __init__(self):
37         self._case = ''
38         self._ofile = ''
39         self._title = []
40         self._story = []
41         self._rootdir = os.path.dirname(vstf.controller.__file__) + '/'
42         self._pdf = None
43
44     def create_pdf(self):
45         style = TemplateStyle(name='default')
46         title = self._title
47         logo = [self._rootdir + "res/logo.jpg"]
48         header = ['']
49         footer = [""]
50         note = ['', '']
51         output = [self._ofile]
52         self._pdf = PdfFrameLoss(style, title, logo, header, footer, output, note)
53
54     def save_pdf(self):
55         self._pdf.generate(self._story)
56
57     def add_coverpage(self):
58         story = Story()
59         story = PageBreakStory(story)
60         self._story += story.storylist
61
62     def create_story(self):
63         raise NotImplementedError("abstract PdfBase")
64
65     def create(self):
66         self.create_pdf()
67         self.create_story()
68         self.save_pdf()
69
70
71 class PdfvSwitchCreator(PdfBase):
72     def __init__(self, ofile, common_data, scenario_data, history_data):
73         PdfBase.__init__(self)
74         self._common = common_data
75         self._result = scenario_data
76         self._history = history_data
77         self._ofile = ofile
78         self._chapterid = 0
79         self._appendixid = LetterOrder()
80
81     def create_pdf(self):
82         style = TemplateStyle(name='default')
83         title = self._result.get_covertitle()
84         logo = [self._rootdir + "res/logo.jpg"]
85         header = ['']
86         footer = [""]
87         note = ['', '']
88         output = [self._ofile]
89         self._pdf = PdfVswitch(style, title, logo, header, footer, output, note)
90
91     def get_chapterid(self):
92         self._chapterid = self._chapterid + 1
93         return self._chapterid
94
95     def create_story(self):
96         self.add_coverpage()
97         self.add_table_of_contents()
98         # self.add_contact()
99         # self.add_overview()
100         self.add_scenario()
101         # self.add_info()
102         # self.add_appendix()
103         self.add_historys()
104
105     def add_info(self):
106         self.add_systeminfo()
107         self.add_gitinfo()
108         self.add_profile_parameters()
109         self.add_testing_options()
110
111     def add_contact(self):
112         story = Story()
113         story = SpaceStory(story)
114         title = ["", "", "", "Reporter"]
115         body = self._common.get_contact()
116         story = TitleStory(story, data=title, style=7)
117         story = ParagraphStory(story, data=body)
118         self._story += story.storylist
119
120     def add_table_of_contents(self):
121         story = Story()
122         story = TableOfContentsStory(story)
123         self._story += story.storylist
124
125     def add_overview(self):
126         story = Story()
127         story = PageBreakStory(story)
128
129         chapterid = self.get_chapterid()
130         title = ["%d.Overview" % (chapterid)]
131         body = [""]
132         story = TitleStory(story, data=title, style=1)
133         story = ParagraphStory(story, data=body)
134
135         sectionid = 1
136         title = ["%d.%d Components under Test" % (chapterid, sectionid)]
137         body = self._common.get_components()
138         story = TitleStory(story, data=title, style=2)
139         story = ParagraphStory(story, data=body)
140
141         sectionid = sectionid + 1
142         title = ["%d.%d Test" % (chapterid, sectionid)]
143         body = self._result.get_test()
144         story = TitleStory(story, data=title, style=2)
145         story = ParagraphStory(story, data=body)
146
147         sectionid = sectionid + 1
148         title = ["%d.%d Configuration" % (chapterid, sectionid)]
149         story = TitleStory(story, data=title, style=2)
150
151         title = ["Software"]
152         body = self._common.get_software()
153         story = TitleStory(story, data=title, style=6)
154         story = ParagraphStory(story, data=body)
155
156         title = ["Hardware"]
157         body = self._common.get_hardware()
158         story = TitleStory(story, data=title, style=6)
159         story = ParagraphStory(story, data=body)
160         self._story += story.storylist
161
162     def add_scenario(self):
163         case_list = self._result.get_caselist()
164         for case in case_list:
165             self.add_case(case)
166
167     def add_case(self, case):
168         story = Story()
169         chapterid = self.get_chapterid()
170
171         title = ["%d. Case : %s (%s)" % (chapterid, case, self._common.get_casename(case))]
172
173         tools = self._result.get_test_tools(case)
174         pic = self._common.get_casefigure(case, tools)
175         print pic
176
177         story = TitleStory(story, data=title, style=1)
178         story = SpaceStory(story)
179         story = ImageStory(story, data=[self._rootdir + pic])
180         story = SpaceStory(story)
181
182         sectionid = 1
183         story = self.add_summary(story, chapterid, sectionid, case)
184         story = SpaceStory(story)
185
186         if self._result.is_throughput_start(case):
187             sectionid = sectionid + 1
188             story = self.add_throughput_result(story, chapterid, sectionid, case)
189
190         if self._result.is_frameloss_start(case):
191             sectionid = sectionid + 1
192             story = self.add_frameloss_result(story, chapterid, sectionid, case)
193
194         if self._result.is_latency_start(case):
195             sectionid = sectionid + 1
196             story = self.add_latency_result(story, chapterid, sectionid, case)
197
198         story = SpaceStory(story)
199         story = SpaceStory(story)
200         self._story += story.storylist
201
202     def add_summary(self, story, chapterid, sectionid, case):
203         title = ["%d.%d Summary" % (chapterid, sectionid)]
204         story = TitleStory(story, data=title, style=2)
205         provider_list = ["fastlink", "rdp", "l2switch"]
206         provider_dict = {"fastlink": "Fast Link", "l2switch": "L2Switch", "rdp": "Kernel RDP"}
207         unitid = 1
208         case_name = self._common.get_casename(case)
209         for provider in provider_list:
210             if self._result.is_provider_start(case, provider):
211                 title = ["%d.%d.%d %s (%s_%s)" % (
212                 chapterid, sectionid, unitid, provider_dict[provider], case_name, provider)]
213                 unitid = unitid + 1
214                 story = TitleStory(story, data=title, style=6)
215                 test_types = ["throughput", "frameloss"]
216                 for test_type in test_types:
217                     if self._result.is_type_provider_start(case, provider, test_type):
218                         story = self.add_summary_type(story, case, provider, test_type)
219         return story
220
221     def add_summary_type(self, story, case, provider, test_type):
222         bar_list = [test_type, "latency"]
223         for item in bar_list:
224             bar_data = self._result.get_bardata(case, provider, item)
225             story = SpaceStory(story)
226             story = BarChartStory(story, data=bar_data)
227
228         table_content = self._result.get_summary_tabledata(case, provider, test_type)
229         story = SpaceStory(story)
230         story = cTableStory(story, data=table_content, style=3)
231         story = SpaceStory(story)
232         return story
233
234     def add_throughput_result(self, story, chapterid, sectionid, case):
235         title = ["%d.%d Throughput " % (chapterid, sectionid)]
236         story = TitleStory(story, data=title, style=2)
237         unitid = 1
238         title = ["%d.%d.%d Summary" % (chapterid, sectionid, unitid)]
239         story = TitleStory(story, data=title, style=6)
240
241         test_type = "throughput"
242         unit = 'RX Frame Rate'
243         chart_data = self._result.get_frameloss_chartdata(case, test_type)
244         table_data = self._result.get_frameloss_tabledata(case, test_type)
245         title = [unit + ' (%)']
246         story = TitleStory(story, data=title, style=6)
247         #       story = SpaceStory(story)
248         #       story = LinePlotStory(story, data=chart_data)
249         story = SpaceStory(story)
250         story = uTableStory(story, data=table_data)
251         story = SpaceStory(story)
252
253         unit = 'Frame Loss Rate'
254         title = [unit + ' (Mpps)']
255
256         chart_data = self._result.get_framerate_chartdata(case, test_type)
257         table_data = self._result.get_framerate_tabledata(case, test_type)
258         story = TitleStory(story, data=title, style=6)
259         story = SpaceStory(story)
260         story = LinePlotStory(story, data=chart_data)
261         story = SpaceStory(story)
262         story = uTableStory(story, data=table_data)
263         story = SpaceStory(story)
264         return story
265
266     def add_frameloss_result(self, story, chapterid, sectionid, case):
267         title = ["%d.%d Frame Loss Rate " % (chapterid, sectionid)]
268         story = TitleStory(story, data=title, style=2)
269         unitid = 1
270         title = ["%d.%d.%d Summary" % (chapterid, sectionid, unitid)]
271         story = TitleStory(story, data=title, style=6)
272
273         test_type = "frameloss"
274         unit = 'RX Frame Rate'
275         chart_data = self._result.get_frameloss_chartdata(case, test_type)
276         table_data = self._result.get_frameloss_tabledata(case, test_type)
277         title = [unit + ' (%)']
278         story = TitleStory(story, data=title, style=6)
279         #       story = SpaceStory(story)
280         #       story = LineChartStory(story, data=chart_data)
281         story = SpaceStory(story)
282         story = uTableStory(story, data=table_data)
283         story = SpaceStory(story)
284
285         unit = 'Frame Loss Rate'
286         title = [unit + ' (Mpps)']
287
288         chart_data = self._result.get_framerate_chartdata(case, test_type)
289         table_data = self._result.get_framerate_tabledata(case, test_type)
290         story = TitleStory(story, data=title, style=6)
291         story = SpaceStory(story)
292         story = LineChartStory(story, data=chart_data)
293         story = SpaceStory(story)
294         story = uTableStory(story, data=table_data)
295         story = SpaceStory(story)
296         return story
297
298     def add_latency_result(self, story, chapterid, sectionid, case):
299         title = ["%d.%d Latency " % (chapterid, sectionid)]
300         story = TitleStory(story, data=title, style=2)
301         unitid = 1
302         title = ["%d.%d.%d Summary" % (chapterid, sectionid, unitid)]
303         story = TitleStory(story, data=title, style=6)
304
305         unit = 'Average Latency'
306         title = [unit + ' (uSec)']
307         #       chart_data = self._result.get_latency_chartdata(case)
308         bar_data = self._result.get_latency_bardata(case)
309         table_data = self._result.get_latency_tabledata(case)
310         story = TitleStory(story, data=title, style=6)
311         story = SpaceStory(story)
312         #       story = LineChartStory(story, data=chart_data)
313         story = BarChartStory(story, data=bar_data)
314
315         story = SpaceStory(story)
316         story = uTableStory(story, data=table_data)
317         story = SpaceStory(story)
318         return story
319
320     def add_systeminfo(self):
321         story = Story()
322         chapterid = self.get_chapterid()
323         story = SpaceStory(story)
324         title = ["%d. System Information " % (chapterid)]
325         story = PageBreakStory(story)
326         story = TitleStory(story, data=title, style=1)
327         table_content = self._common.get_systeminfo_tabledata()
328         story = SpaceStory(story)
329         story = cTableStory(story, data=table_content, style=0)
330         story = SpaceStory(story)
331         self._story += story.storylist
332
333     def add_gitinfo(self):
334         story = Story()
335         chapterid = self.get_chapterid()
336         title = ["%d. Git Repository Information " % (chapterid)]
337         story = TitleStory(story, data=title, style=1)
338
339         table_content = self._common.get_gitinfo_tabledata()
340         if table_content:
341             story = SpaceStory(story)
342             story = cTableStory(story, data=table_content, style=5)
343             story = SpaceStory(story)
344         self._story += story.storylist
345
346     def add_testing_options(self):
347         story = Story()
348         chapterid = self.get_chapterid()
349         story = SpaceStory(story)
350         title = ["%d. Testing Options" % (chapterid)]
351
352         story = TitleStory(story, data=title, style=1)
353         table_content = self._common.get_testingoptions_tabledata()
354         story = SpaceStory(story)
355         story = cTableStory(story, data=table_content, style=1)
356         story = SpaceStory(story)
357         self._story += story.storylist
358
359     def add_profile_parameters(self):
360         story = Story()
361         chapterid = self.get_chapterid()
362         story = PageBreakStory(story)
363         title = ["%d. " % (chapterid)]
364         story = TitleStory(story, data=title, style=1)
365         table_content = self._common.get_profileparameters_tabledData()
366         story = SpaceStory(story)
367         story = cTableStory(story, data=table_content, style=2)
368         story = SpaceStory(story)
369         self._story += story.storylist
370
371     def add_appendix(self):
372         story = Story()
373         story = PageBreakStory(story)
374
375         title = ["<b>Appendix %s: vSwitching Testing Methodology</b>" % (self._appendixid.get())]
376         self._appendixid.next()
377         story = TitleStory(story, data=title, style=1)
378         filename = "res/Traffic-types.jpg"
379         story = SpaceStory(story)
380         story = ImageStory(story, data=[self._rootdir + filename])
381         #       story = SpaceStory(story)
382
383         title = ["Traffic Patterns: "]
384         story = TitleStory(story, data=title, style=6)
385
386         body = [
387             "<b>Ti</b>  - South North Traffic",
388             "<b>Tu</b>  - East Eest Traffic",
389             "<b>Tn</b>  - Physical host or VM loop back",
390             "<b>Tnv</b>  - Virtual Machine loop back",
391         ]
392         story = ParagraphStory(story, data=body)
393
394         title = ["<b>Performance Testing Coverage </b> (version 0.1):"]
395         story = TitleStory(story, data=title, style=6)
396
397         table_content = self._common.get_introduct_tabledata()
398         story = SpaceStory(story)
399         story = cTableStory(story, data=table_content, style=4)
400         self._story += story.storylist
401
402     def add_historys(self):
403         case_list = self._result.get_caselist()
404         for case in case_list:
405             history = self._history.get_history_info(case)
406             if history:
407                 self.add_history(case, history)
408
409     def add_history(self, case, history):
410         story = Story()
411         story = PageBreakStory(story)
412
413         title = ["<b>Appendix %s : %s History Records</b>" % (self._appendixid.get(), case)]
414         story = TitleStory(story, data=title, style=1)
415
416         for i in range(len(history)):
417             title = ["%s.%s %s" % (self._appendixid.get(), i, history[i]["title"])]
418             story = TitleStory(story, data=title, style=2)
419
420             section = history[i]["data"]
421             for unit in section:
422                 title = [unit['title']]
423                 story = TitleStory(story, data=title, style=6)
424                 content = unit['data']
425                 story = uTableStory(story, data=content)
426
427         self._appendixid.next()
428         self._story += story.storylist
429
430
431 def main():
432     dbase = DbManage()
433     taskid = dbase.get_last_taskid()
434     common_data = CommonData(taskid, dbase)
435     scenario_list = common_data.get_scenariolist()
436     history_data = HistoryData(taskid, dbase)
437     for scenario in scenario_list:
438         out_file = "vstf_report_%s.pdf" % (scenario)
439         scenario_data = ScenarioData(taskid, dbase, scenario)
440         reporter = PdfvSwitchCreator(out_file, common_data, scenario_data, history_data)
441         if reporter:
442             reporter.create()
443
444
445 if __name__ == '__main__':
446     main()