Upload the contribution of vstf as bottleneck network framework.
[bottlenecks.git] / vstf / vstf / controller / reporters / reporter.py
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 # author: wly
4 # date: 2015-05-29
5 # see license for license details
6 import os
7 import argparse
8 import logging
9 import time
10
11 from vstf.controller.reporters.report.pdf.pdfcreator import PdfvSwitchCreator
12 from vstf.controller.reporters.report.html.htmlcreator import HtmlvSwitchCreator
13 from vstf.controller.reporters.report.data_factory import CommonData, TaskData, ScenarioData, HistoryData
14 from vstf.controller.database.dbinterface import DbManage
15 from vstf.controller.settings.mail_settings import MailSettings
16 from vstf.controller.reporters.mail.sendmail import SendMail
17 from vstf.controller.settings.html_settings import HtmlSettings
18 from vstf.controller.reporters.report.provider.html_provider import StyleProvider
19 import vstf.common.constants as cst
20
21
22 __version__ = ''' '''
23 LOG = logging.getLogger(__name__)
24
25
26 class Report(object):
27     def __init__(self, dbase, rpath):
28         """
29
30         :type dbase: object DbManage
31         """
32         self._dbase = dbase
33         self._rpath = "."
34         if os.path.exists(rpath):
35             self._rpath = rpath
36
37     def create_pdf(self, taskid):
38         common_data = CommonData(taskid, self._dbase)
39         scenario_list = common_data.get_scenariolist()
40         history_data = HistoryData(taskid, self._dbase)
41         attach_list = []
42         for scenario in scenario_list:
43             out_file = os.path.join(self._rpath, "vstf_report_%s_%s.pdf" % (scenario, time.strftime(cst.TIME_STR)))
44             LOG.info(out_file)
45             scenario_data = ScenarioData(taskid, self._dbase, scenario)
46             pdf = PdfvSwitchCreator(out_file, common_data, scenario_data, history_data)
47             if pdf:
48                 pdf.create()
49                 attach_list.append(out_file)
50         if attach_list:
51             self._mail_settings.mset_attach(attach_list)
52
53     def create_html(self, taskid):
54         task_data = TaskData(taskid, self._dbase)
55
56         html_settings = HtmlSettings()
57         LOG.info(html_settings.settings)
58
59         provider = StyleProvider(html_settings.settings)
60         out_file = os.path.join(self._rpath, "mail.html")
61         LOG.info(out_file)
62
63         html = HtmlvSwitchCreator(task_data, provider, out_file)
64         content = html.create()
65
66         self._mail_settings.mset_subtype('html')
67         self._mail_settings.mset_content(content)
68
69     def report(self, taskid, mail_off):
70         self._mail_settings = MailSettings()
71         mail = SendMail(self._mail_settings.settings)
72         self.create_pdf(taskid)
73         self.create_html(taskid)
74         if not mail_off:
75             mail.send()
76
77
78 def main():
79     from vstf.common.log import setup_logging
80     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-reporter.log", clevel=logging.INFO)
81
82     parser = argparse.ArgumentParser(add_help=True)
83     parser.add_argument('-rpath',
84                         action='store',
85                         default='./',
86                         type=str,
87                         help=" the path name of test results  "
88                         )
89     parser.add_argument('-mail_off',
90                         action='store_true',
91                         help="is need send mail the for the report"
92                         )
93     parser.add_argument('--taskid',
94                         action='store',
95                         default=-1,
96                         help="report depand of a history task id."
97                         )
98     args = parser.parse_args()
99     dbase = DbManage()
100
101     report = Report(dbase, args.rpath)
102     if args.taskid == -1:
103         taskid = dbase.get_last_taskid()
104     else:
105         taskid = args.taskid
106     report.report(taskid, args.mail_off)
107
108
109 if __name__ == '__main__':
110     main()