77e26041234abb2ab93aacfe3055d8aa1bd1bf2e
[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, scenario, installer, ten_score, percent):
43     # Save daily results in a file
44     history_file = './display/{}/yardstick/scenario_history.txt'.format(
45         version)
46
47     if not os.path.exists(history_file):
48         with open(history_file, 'w') as f:
49             f.write('date,scenario,installer,details,score\n')
50
51     date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
52     with open(history_file, "a") as f:
53         info = '{},{},{},{},{}\n'.format(date,
54                                          scenario,
55                                          installer,
56                                          ten_score,
57                                          percent)
58         f.write(info)
59
60
61 def generate_page(scenario_data, installer, period, version, architecture):
62     date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
63
64     templateLoader = jinja2.FileSystemLoader(".")
65     template_env = jinja2.Environment(loader=templateLoader,
66                                       autoescape=True)
67
68     template_file = "./reporting/yardstick/template/index-status-tmpl.html"
69     template = template_env.get_template(template_file)
70
71     if installer == 'fuel':
72         installer = '{}@{}'.format(installer, architecture)
73
74     output_text = template.render(scenario_results=scenario_data,
75                                   installer=installer,
76                                   period=period,
77                                   version=version,
78                                   date=date)
79
80     page_file = './display/{}/yardstick/status-{}.html'.format(version,
81                                                                installer)
82     with open(page_file, 'wb') as f:
83         f.write(output_text)
84
85
86 def do_statistic(data):
87     ten_score = 0
88     for v in data:
89         ten_score += v
90
91     last_count = utils.get_config(
92         'general.nb_iteration_tests_success_criteria')
93     last_data = data[:last_count]
94     last_score = 0
95     for v in last_data:
96         last_score += v
97
98     percent = utils.get_percent(last_data, data)
99     status = str(percent)
100     last_score = '{}/{}'.format(last_score, len(last_data))
101     ten_score = '{}/{}'.format(ten_score, len(data))
102
103     if '100' == status:
104         LOG.info(">>>>> scenario OK, save the information")
105     else:
106         LOG.info(">>>> scenario not OK, last 4 iterations = %s, \
107                     last 10 days = %s" % (last_score, ten_score))
108
109     return last_score, ten_score, percent, status
110
111
112 def generate_reporting_page(version, installer, archi, scenarios, period):
113     scenario_data = {}
114
115     # From each scenarios get results list
116     for scenario, data in scenarios.items():
117         LOG.info("---------------------------------")
118
119         LOG.info("installer %s, version %s, scenario %s",
120                  installer,
121                  version,
122                  scenario)
123         last_score, ten_score, percent, status = do_statistic(data)
124         write_history_data(version, scenario, installer, ten_score, percent)
125         scenario_data[scenario] = ScenarioResult(status,
126                                                  last_score,
127                                                  ten_score,
128                                                  percent)
129
130         LOG.info("--------------------------")
131     if scenario_data:
132         generate_page(scenario_data, installer, period, version, archi)
133
134
135 def main():
136     installers = utils.get_config('general.installers')
137     versions = utils.get_config('general.versions')
138     period = utils.get_config('general.period')
139
140     LOG.info("*******************************************")
141     LOG.info("*   Generating reporting scenario status  *")
142     LOG.info("*   Data retention = %s days              *" % period)
143     LOG.info("*                                         *")
144     LOG.info("*******************************************")
145
146     # For all the versions
147     for version in versions:
148         # For all the installers
149         for installer in installers:
150             # get scenarios results data
151             scenarios = get_scenario_data(version, installer)
152             for k, v in scenarios.items():
153                 generate_reporting_page(version, installer, k, v, period)
154
155
156 if __name__ == '__main__':
157     main()