Merge "Add qtip job to pod zte-virtual6"
[releng.git] / utils / test / reporting / reporting / yardstick / reporting-status.py
1 #!/usr/bin/python
2 #
3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 import datetime
10 import os
11
12 import jinja2
13
14 from reporting.utils.scenarioResult import ScenarioResult
15 from reporting.utils import reporting_utils as utils
16 from scenarios import config as blacklist
17
18
19 # Logger
20 LOG = utils.getLogger("Yardstick-Status")
21
22
23 def get_scenario_data(version, installer):
24     scenarios = utils.getScenarioStatus(installer, version)
25
26     if 'colorado' == version:
27         data = utils.getScenarioStatus(installer, 'stable/colorado')
28         for archi, value in data.items():
29             for k, v in value.items():
30                 if k not in scenarios[archi]:
31                     scenarios[archi][k] = []
32                 scenarios[archi][k].extend(data[archi][k])
33
34     for archi, value in scenarios.items():
35         for scenario in value:
36             if installer in blacklist and scenario in blacklist[installer]:
37                 scenarios[archi].pop(scenario)
38
39     return scenarios
40
41
42 def write_history_data(version,
43                        scenario,
44                        installer,
45                        archi,
46                        ten_score,
47                        percent):
48     # Save daily results in a file
49     history_file = './display/{}/yardstick/scenario_history.txt'.format(
50         version)
51
52     if not os.path.exists(history_file):
53         with open(history_file, 'w') as f:
54             f.write('date,scenario,installer,details,score\n')
55
56     date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
57     if installer == 'fuel':
58         installer = '{}@{}'.format(installer, archi)
59     with open(history_file, "a") as f:
60         info = '{},{},{},{},{}\n'.format(date,
61                                          scenario,
62                                          installer,
63                                          ten_score,
64                                          percent)
65         f.write(info)
66
67
68 def generate_page(scenario_data, installer, period, version, architecture):
69     date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
70
71     templateLoader = jinja2.FileSystemLoader(".")
72     template_env = jinja2.Environment(loader=templateLoader,
73                                       autoescape=True)
74
75     template_file = "./reporting/yardstick/template/index-status-tmpl.html"
76     template = template_env.get_template(template_file)
77
78     if installer == 'fuel':
79         installer = '{}@{}'.format(installer, architecture)
80
81     output_text = template.render(scenario_results=scenario_data,
82                                   installer=installer,
83                                   period=period,
84                                   version=version,
85                                   date=date)
86
87     page_file = './display/{}/yardstick/status-{}.html'.format(version,
88                                                                installer)
89     with open(page_file, 'wb') as f:
90         f.write(output_text)
91
92
93 def do_statistic(data):
94     ten_score = 0
95     for v in data:
96         ten_score += v
97
98     last_count = utils.get_config(
99         'general.nb_iteration_tests_success_criteria')
100     last_data = data[:last_count]
101     last_score = 0
102     for v in last_data:
103         last_score += v
104
105     percent = utils.get_percent(last_data, data)
106     status = str(percent)
107     last_score = '{}/{}'.format(last_score, len(last_data))
108     ten_score = '{}/{}'.format(ten_score, len(data))
109
110     if '100' == status:
111         LOG.info(">>>>> scenario OK, save the information")
112     else:
113         LOG.info(">>>> scenario not OK, last 4 iterations = %s, \
114                     last 10 days = %s" % (last_score, ten_score))
115
116     return last_score, ten_score, percent, status
117
118
119 def generate_reporting_page(version, installer, archi, scenarios, period):
120     scenario_data = {}
121
122     # From each scenarios get results list
123     for scenario, data in scenarios.items():
124         LOG.info("---------------------------------")
125
126         LOG.info("installer %s, version %s, scenario %s",
127                  installer,
128                  version,
129                  scenario)
130         last_score, ten_score, percent, status = do_statistic(data)
131         write_history_data(version,
132                            scenario,
133                            installer,
134                            archi,
135                            ten_score,
136                            percent)
137         scenario_data[scenario] = ScenarioResult(status,
138                                                  last_score,
139                                                  ten_score,
140                                                  percent)
141
142         LOG.info("--------------------------")
143     if scenario_data:
144         generate_page(scenario_data, installer, period, version, archi)
145
146
147 def main():
148     installers = utils.get_config('general.installers')
149     versions = utils.get_config('general.versions')
150     period = utils.get_config('general.period')
151
152     LOG.info("*******************************************")
153     LOG.info("*   Generating reporting scenario status  *")
154     LOG.info("*   Data retention = %s days              *" % period)
155     LOG.info("*                                         *")
156     LOG.info("*******************************************")
157
158     # For all the versions
159     for version in versions:
160         # For all the installers
161         for installer in installers:
162             # get scenarios results data
163             scenarios = get_scenario_data(version, installer)
164             for k, v in scenarios.items():
165                 generate_reporting_page(version, installer, k, v, period)
166
167
168 if __name__ == '__main__':
169     main()