Merge "Move daisy jobs of master branch to zte-pod3"
[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         if version != 'master':
41             new_version = "stable/{}".format(version)
42         else:
43             new_version = version
44         scenario_results = rp_utils.getScenarios("bottlenecks",
45                                                  "posca_factor_ping",
46                                                  installer,
47                                                  new_version)
48         LOGGER.info("scenario_results: %s", scenario_results)
49
50         scenario_stats = rp_utils.getScenarioStats(scenario_results)
51         LOGGER.info("scenario_stats: %s", scenario_stats)
52         items = {}
53         scenario_result_criteria = {}
54
55         # From each scenarios get results list
56         for s, s_result in scenario_results.items():
57             LOGGER.info("---------------------------------")
58             LOGGER.info("installer %s, version %s, scenario %s", installer,
59                         version, s)
60             ten_criteria = len(s_result)
61
62             ten_score = 0
63             for v in s_result:
64                 if "PASS" in v['criteria']:
65                     ten_score += 1
66
67             LOGGER.info("ten_score: %s / %s", (ten_score, ten_criteria))
68
69             four_score = 0
70             try:
71                 LASTEST_TESTS = rp_utils.get_config(
72                     'general.nb_iteration_tests_success_criteria')
73                 s_result.sort(key=lambda x: x['start_date'])
74                 four_result = s_result[-LASTEST_TESTS:]
75                 LOGGER.debug("four_result: {}".format(four_result))
76                 LOGGER.debug("LASTEST_TESTS: {}".format(LASTEST_TESTS))
77                 # logger.debug("four_result: {}".format(four_result))
78                 four_criteria = len(four_result)
79                 for v in four_result:
80                     if "PASS" in v['criteria']:
81                         four_score += 1
82                 LOGGER.info("4 Score: %s / %s ", (four_score,
83                                                   four_criteria))
84             except Exception:
85                 LOGGER.error("Impossible to retrieve the four_score")
86
87             try:
88                 s_status = (four_score * 100) / four_criteria
89             except Exception:
90                 s_status = 0
91             LOGGER.info("Score percent = %s", str(s_status))
92             s_four_score = str(four_score) + '/' + str(four_criteria)
93             s_ten_score = str(ten_score) + '/' + str(ten_criteria)
94             s_score_percent = str(s_status)
95
96             LOGGER.debug(" s_status: %s", s_status)
97             if s_status == 100:
98                 LOGGER.info(">>>>> scenario OK, save the information")
99             else:
100                 LOGGER.info(">>>> scenario not OK, last 4 iterations = %s, \
101                              last 10 days = %s", (s_four_score, s_ten_score))
102
103             s_url = ""
104             if len(s_result) > 0:
105                 build_tag = s_result[len(s_result)-1]['build_tag']
106                 LOGGER.debug("Build tag: %s", build_tag)
107                 s_url = s_url = rp_utils.getJenkinsUrl(build_tag)
108                 LOGGER.info("last jenkins url: %s", s_url)
109
110             # Save daily results in a file
111             path_validation_file = ("./display/" + version +
112                                     "/bottlenecks/scenario_history.txt")
113
114             if not os.path.exists(path_validation_file):
115                 with open(path_validation_file, 'w') as f:
116                     info = 'date,scenario,installer,details,score\n'
117                     f.write(info)
118
119             with open(path_validation_file, "a") as f:
120                 info = (reportingDate + "," + s + "," + installer +
121                         "," + s_ten_score + "," +
122                         str(s_score_percent) + "\n")
123                 f.write(info)
124
125             scenario_result_criteria[s] = sr.ScenarioResult(s_status,
126                                                             s_four_score,
127                                                             s_ten_score,
128                                                             s_score_percent,
129                                                             s_url)
130
131             LOGGER.info("--------------------------")
132
133         templateLoader = jinja2.FileSystemLoader(".")
134         templateEnv = jinja2.Environment(loader=templateLoader,
135                                          autoescape=True)
136
137         TEMPLATE_FILE = ("./reporting/bottlenecks/template"
138                          "/index-status-tmpl.html")
139         template = templateEnv.get_template(TEMPLATE_FILE)
140
141         outputText = template.render(scenario_results=scenario_result_criteria,
142                                      installer=installer,
143                                      period=PERIOD,
144                                      version=version,
145                                      date=reportingDate)
146
147         with open("./display/" + version +
148                   "/bottlenecks/status-" + installer + ".html", "wb") as fh:
149             fh.write(outputText)