4f9b24fedcb5048f22ea03bc2b245e43c339883a
[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 from convert_xml_json import convert_xml_to_json
33
34
35 OUTPUTS = (u"none", u"report", u"trending", u"convert-xml-to-json")
36
37
38 def parse_args():
39     """Parse arguments from cmd line.
40
41     :returns: Parsed arguments.
42     :rtype: ArgumentParser
43     """
44
45     parser = argparse.ArgumentParser(
46         description=__doc__,
47         formatter_class=argparse.RawDescriptionHelpFormatter
48     )
49     parser.add_argument(
50         u"-s", u"--specification",
51         required=True,
52         type=str,
53         help=u"Specification YAML file."
54     )
55     parser.add_argument(
56         u"-r", u"--release",
57         default=u"master",
58         type=str,
59         help=u"Release string of the product."
60     )
61     parser.add_argument(
62         u"-w", u"--week",
63         default=u"1",
64         type=str,
65         help=u"Calendar week when the report is published."
66     )
67     parser.add_argument(
68         u"-l", u"--logging",
69         choices=[
70             u"NOTSET", u"DEBUG", u"INFO", u"WARNING", u"ERROR", u"CRITICAL"
71         ],
72         default=u"ERROR",
73         help=u"Logging level."
74     )
75     parser.add_argument(
76         u"-f", u"--force",
77         action=u"store_true",
78         help=u"Force removing the old build(s) if present."
79     )
80     parser.add_argument(
81         u"-o", u"--print-all-oper-data",
82         action=u"store_true",
83         help=u"Print all operational data to console. Be careful, the output "
84              u"can be really long."
85     )
86     parser.add_argument(
87         u"-i", u"--input-file",
88         type=str,
89         default=u"",
90         help=u"XML file generated by RobotFramework which will be processed "
91              u"instead of downloading the data from Nexus and/or Jenkins. In "
92              u"this case, the section 'input' in the specification file is "
93              u"ignored."
94     )
95     parser.add_argument(
96         u"-d", u"--input-directory",
97         type=str,
98         default=u"",
99         help=u"Directory with XML file(s) generated by RobotFramework or with "
100              u"sub-directories with XML file(s) which will be processed "
101              u"instead of downloading the data from Nexus and/or Jenkins. In "
102              u"this case, the section 'input' in the specification file is "
103              u"ignored."
104     )
105
106     return parser.parse_args()
107
108
109 def main():
110     """Main function."""
111
112     log_levels = {
113         u"NOTSET": logging.NOTSET,
114         u"DEBUG": logging.DEBUG,
115         u"INFO": logging.INFO,
116         u"WARNING": logging.WARNING,
117         u"ERROR": logging.ERROR,
118         u"CRITICAL": logging.CRITICAL
119     }
120
121     args = parse_args()
122     logging.basicConfig(
123         format=u"%(asctime)s: %(levelname)s: %(message)s",
124         datefmt=u"%Y/%m/%d %H:%M:%S",
125         level=log_levels[args.logging]
126     )
127
128     logging.info(u"Application started.")
129
130     try:
131         spec = Specification(args.specification)
132         spec.read_specification()
133     except PresentationError as err:
134         logging.critical(u"Finished with error.")
135         logging.critical(repr(err))
136         return 1
137
138     if spec.output[u"output"] not in OUTPUTS:
139         logging.critical(
140             f"The output {spec.output[u'output']} is not supported."
141         )
142         return 1
143
144     return_code = 1
145     try:
146         env = Environment(spec.environment, args.force)
147         env.set_environment()
148
149         prepare_static_content(spec)
150
151         data = InputData(spec, spec.output[u"output"])
152         if args.input_file:
153             data.process_local_file(args.input_file)
154         elif args.input_directory:
155             data.process_local_directory(args.input_directory)
156         else:
157             data.download_and_parse_data(repeat=1)
158
159         if args.print_all_oper_data:
160             data.print_all_oper_data()
161
162         generate_tables(spec, data)
163         generate_plots(spec, data)
164         generate_files(spec, data)
165
166         if spec.output[u"output"] == u"report":
167             generate_report(args.release, spec, args.week)
168         elif spec.output[u"output"] == u"trending":
169             sys.stdout.write(generate_cpta(spec, data))
170             try:
171                 alert = Alerting(spec)
172                 alert.generate_alerts()
173             except AlertingError as err:
174                 logging.warning(repr(err))
175         elif spec.output[u"output"] == u"convert-xml-to-json":
176             convert_xml_to_json(spec, data)
177         else:
178             logging.info("No output will be generated.")
179
180         logging.info(u"Successfully finished.")
181         return_code = 0
182
183     except AlertingError as err:
184         logging.critical(f"Finished with an alerting error.\n{repr(err)}")
185     except PresentationError as err:
186         logging.critical(f"Finished with a PAL error.\n{str(err)}")
187     except (KeyError, ValueError) as err:
188         logging.critical(f"Finished with an error.\n{repr(err)}")
189     finally:
190         if spec is not None:
191             clean_environment(spec.environment)
192     return return_code
193
194
195 if __name__ == u"__main__":
196     sys.exit(main())