5bbea297ef6c52a3c86c3dcc1338795d9cdc673e
[csit.git] / resources / tools / presentation / pal.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """CSIT Presentation and Analytics Layer.
15 """
16
17 import sys
18 import argparse
19 import logging
20
21 from pal_errors import PresentationError
22 from specification_parser import Specification
23 from environment import Environment, clean_environment
24 from static_content import prepare_static_content
25 from input_data_parser import InputData
26 from generator_tables import generate_tables
27 from generator_plots import generate_plots
28 from generator_files import generate_files
29 from generator_report import generate_report
30 from generator_cpta import generate_cpta
31 from generator_alerts import Alerting, AlertingError
32
33
34 OUTPUTS = (u"none", u"report", u"trending", u"convert_to_json")
35
36
37 def parse_args():
38     """Parse arguments from cmd line.
39
40     :returns: Parsed arguments.
41     :rtype: ArgumentParser
42     """
43
44     parser = argparse.ArgumentParser(
45         description=__doc__,
46         formatter_class=argparse.RawDescriptionHelpFormatter
47     )
48     parser.add_argument(
49         u"-s", u"--specification",
50         required=True,
51         type=str,
52         help=u"Specification YAML file."
53     )
54     parser.add_argument(
55         u"-r", u"--release",
56         default=u"master",
57         type=str,
58         help=u"Release string of the product."
59     )
60     parser.add_argument(
61         u"-w", u"--week",
62         default=u"1",
63         type=str,
64         help=u"Calendar week when the report is published."
65     )
66     parser.add_argument(
67         u"-l", u"--logging",
68         choices=[
69             u"NOTSET", u"DEBUG", u"INFO", u"WARNING", u"ERROR", u"CRITICAL"
70         ],
71         default=u"ERROR",
72         help=u"Logging level."
73     )
74     parser.add_argument(
75         u"-f", u"--force",
76         action=u"store_true",
77         help=u"Force removing the old build(s) if present."
78     )
79     parser.add_argument(
80         u"-o", u"--print-all-oper-data",
81         action=u"store_true",
82         help=u"Print all operational data to console. Be careful, the output "
83              u"can be really long."
84     )
85     parser.add_argument(
86         u"-i", u"--input-file",
87         type=str,
88         default=u"",
89         help=u"XML file generated by RobotFramework which will be processed "
90              u"instead of downloading the data from Nexus and/or Jenkins. In "
91              u"this case, the section 'input' in the specification file is "
92              u"ignored."
93     )
94     parser.add_argument(
95         u"-d", u"--input-directory",
96         type=str,
97         default=u"",
98         help=u"Directory with XML file(s) generated by RobotFramework or with "
99              u"sub-directories with XML file(s) which will be processed "
100              u"instead of downloading the data from Nexus and/or Jenkins. In "
101              u"this case, the section 'input' in the specification file is "
102              u"ignored."
103     )
104
105     return parser.parse_args()
106
107
108 def main():
109     """Main function."""
110
111     log_levels = {
112         u"NOTSET": logging.NOTSET,
113         u"DEBUG": logging.DEBUG,
114         u"INFO": logging.INFO,
115         u"WARNING": logging.WARNING,
116         u"ERROR": logging.ERROR,
117         u"CRITICAL": logging.CRITICAL
118     }
119
120     args = parse_args()
121     logging.basicConfig(
122         format=u"%(asctime)s: %(levelname)s: %(message)s",
123         datefmt=u"%Y/%m/%d %H:%M:%S",
124         level=log_levels[args.logging]
125     )
126
127     logging.info(u"Application started.")
128
129     try:
130         spec = Specification(args.specification)
131         spec.read_specification()
132     except PresentationError as err:
133         logging.critical(u"Finished with error.")
134         return 1
135
136     if spec.output[u"output"] not in OUTPUTS:
137         logging.critical(
138             f"The output {spec.output[u'output']} is not supported."
139         )
140         return 1
141
142     return_code = 1
143     try:
144         env = Environment(spec.environment, args.force)
145         env.set_environment()
146
147         prepare_static_content(spec)
148
149         data = InputData(spec)
150         if args.input_file:
151             data.process_local_file(args.input_file)
152         elif args.input_directory:
153             data.process_local_directory(args.input_directory)
154         else:
155             data.download_and_parse_data(repeat=1)
156
157         if args.print_all_oper_data:
158             data.print_all_oper_data()
159
160         generate_tables(spec, data)
161         generate_plots(spec, data)
162         generate_files(spec, data)
163
164         if spec.output[u"output"] == u"report":
165             generate_report(args.release, spec, args.week)
166         elif spec.output[u"output"] == u"trending":
167             sys.stdout.write(generate_cpta(spec, data))
168             try:
169                 alert = Alerting(spec)
170                 alert.generate_alerts()
171             except AlertingError as err:
172                 logging.warning(repr(err))
173         else:
174             logging.info("No output will be generated.")
175
176         logging.info(u"Successfully finished.")
177         return_code = 0
178
179     except AlertingError as err:
180         logging.critical(f"Finished with an alerting error.\n{repr(err)}")
181     except PresentationError as err:
182         logging.critical(f"Finished with a PAL error.\n{str(err)}")
183     except (KeyError, ValueError) as err:
184         logging.critical(f"Finished with an error.\n{repr(err)}")
185     finally:
186         if spec is not None:
187             clean_environment(spec.environment)
188     return return_code
189
190
191 if __name__ == u"__main__":
192     sys.exit(main())