Add bottlenecks to reporting
[releng.git] / utils / test / reporting / reporting / bottlenecks / 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 import reporting.utils.reporting_utils as rp_utils
15 import reporting.utils.scenarioResult as sr
16
17 INSTALLERS = rp_utils.get_config('general.installers')
18 VERSIONS = rp_utils.get_config('general.versions')
19 PERIOD = rp_utils.get_config('general.period')
20
21 # Logger
22 LOGGER = rp_utils.getLogger("Bottlenecks-Status")
23 reportingDate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
24
25 LOGGER.info("*******************************************")
26 LOGGER.info("*   Generating reporting scenario status  *")
27 LOGGER.info("*   Data retention = %s days              *", PERIOD)
28 LOGGER.info("*                                         *")
29 LOGGER.info("*******************************************")
30
31 # retrieve the list of bottlenecks tests
32 BOTTLENECKS_TESTS = rp_utils.get_config('bottlenecks.test_list')
33 LOGGER.info("Bottlenecks tests: %s", BOTTLENECKS_TESTS)
34
35 # For all the versions
36 for version in VERSIONS:
37     # For all the installers
38     for installer in INSTALLERS:
39         # get scenarios results data
40         scenario_results = rp_utils.getScenarios("bottlenecks",
41                                                  "posca_factor_ping",
42                                                  installer,
43                                                  version)
44         LOGGER.info("scenario_results: %s", scenario_results)
45
46         scenario_stats = rp_utils.getScenarioStats(scenario_results)
47         LOGGER.info("scenario_stats: %s", scenario_stats)
48         items = {}
49         scenario_result_criteria = {}
50
51         # From each scenarios get results list
52         for s, s_result in scenario_results.items():
53             LOGGER.info("---------------------------------")
54             LOGGER.info("installer %s, version %s, scenario %s", installer,
55                         version, s)
56             ten_criteria = len(s_result)
57
58             ten_score = 0
59             for v in s_result:
60                 if "PASS" in v['criteria']:
61                     ten_score += 1
62
63             LOGGER.info("ten_score: %s / %s", (ten_score, ten_criteria))
64
65             four_score = 0
66             try:
67                 LASTEST_TESTS = rp_utils.get_config(
68                     'general.nb_iteration_tests_success_criteria')
69                 s_result.sort(key=lambda x: x['start_date'])
70                 four_result = s_result[-LASTEST_TESTS:]
71                 LOGGER.debug("four_result: {}".format(four_result))
72                 LOGGER.debug("LASTEST_TESTS: {}".format(LASTEST_TESTS))
73                 # logger.debug("four_result: {}".format(four_result))
74                 four_criteria = len(four_result)
75                 for v in four_result:
76                     if "PASS" in v['criteria']:
77                         four_score += 1
78                 LOGGER.info("4 Score: %s / %s ", (four_score,
79                                                   four_criteria))
80             except Exception:
81                 LOGGER.error("Impossible to retrieve the four_score")
82
83             try:
84                 s_status = (four_score * 100) / four_criteria
85             except Exception:
86                 s_status = 0
87             LOGGER.info("Score percent = %s", str(s_status))
88             s_four_score = str(four_score) + '/' + str(four_criteria)
89             s_ten_score = str(ten_score) + '/' + str(ten_criteria)
90             s_score_percent = str(s_status)
91
92             LOGGER.debug(" s_status: %s", s_status)
93             if s_status == 100:
94                 LOGGER.info(">>>>> scenario OK, save the information")
95             else:
96                 LOGGER.info(">>>> scenario not OK, last 4 iterations = %s, \
97                              last 10 days = %s", (s_four_score, s_ten_score))
98
99             s_url = ""
100             if len(s_result) > 0:
101                 build_tag = s_result[len(s_result)-1]['build_tag']
102                 LOGGER.debug("Build tag: %s", build_tag)
103                 s_url = s_url = rp_utils.getJenkinsUrl(build_tag)
104                 LOGGER.info("last jenkins url: %s", s_url)
105
106             # Save daily results in a file
107             path_validation_file = ("./display/" + version +
108                                     "/bottlenecks/scenario_history.txt")
109
110             if not os.path.exists(path_validation_file):
111                 with open(path_validation_file, 'w') as f:
112                     info = 'date,scenario,installer,details,score\n'
113                     f.write(info)
114
115             with open(path_validation_file, "a") as f:
116                 info = (reportingDate + "," + s + "," + installer +
117                         "," + s_ten_score + "," +
118                         str(s_score_percent) + "\n")
119                 f.write(info)
120
121             scenario_result_criteria[s] = sr.ScenarioResult(s_status,
122                                                             s_four_score,
123                                                             s_ten_score,
124                                                             s_score_percent,
125                                                             s_url)
126
127             LOGGER.info("--------------------------")
128
129         templateLoader = jinja2.FileSystemLoader(".")
130         templateEnv = jinja2.Environment(loader=templateLoader,
131                                          autoescape=True)
132
133         TEMPLATE_FILE = ("./reporting/bottlenecks/template"
134                          "/index-status-tmpl.html")
135         template = templateEnv.get_template(TEMPLATE_FILE)
136
137         outputText = template.render(scenario_results=scenario_result_criteria,
138                                      installer=installer,
139                                      period=PERIOD,
140                                      version=version,
141                                      date=reportingDate)
142
143         with open("./display/" + version +
144                   "/bottlenecks/status-" + installer + ".html", "wb") as fh:
145             fh.write(outputText)