PAL: Operational data
[csit.git] / resources / tools / presentation / pal.py
1 # Copyright (c) 2019 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 environment import Environment, clean_environment
23 from specification_parser import Specification
24 from input_data_parser import InputData
25 from generator_tables import generate_tables
26 from generator_plots import generate_plots
27 from generator_files import generate_files
28 from static_content import prepare_static_content
29 from generator_report import generate_report
30 from generator_cpta import generate_cpta
31 from generator_alerts import Alerting, AlertingError
32
33
34 def parse_args():
35     """Parse arguments from cmd line.
36
37     :returns: Parsed arguments.
38     :rtype: ArgumentParser
39     """
40
41     parser = argparse.ArgumentParser(description=__doc__,
42                                      formatter_class=argparse.
43                                      RawDescriptionHelpFormatter)
44     parser.add_argument(u"-s", u"--specification",
45                         required=True,
46                         type=argparse.FileType(u'r'),
47                         help=u"Specification YAML file.")
48     parser.add_argument(u"-r", u"--release",
49                         default=u"master",
50                         type=str,
51                         help=u"Release string of the product.")
52     parser.add_argument(u"-w", u"--week",
53                         default=u"1",
54                         type=str,
55                         help=u"Calendar week when the report is published.")
56     parser.add_argument(u"-l", u"--logging",
57                         choices=[u"DEBUG", u"INFO", u"WARNING",
58                                  u"ERROR", u"CRITICAL"],
59                         default=u"ERROR",
60                         help=u"Logging level.")
61     parser.add_argument(u"-f", u"--force",
62                         action=u"store_true",
63                         help=u"Force removing the old build(s) if present.")
64     parser.add_argument(u"-o", u"--print-all-oper-data",
65                         action=u"store_true",
66                         help=u"Print all operational data to console. Be "
67                              u"careful, the output can be really long.")
68
69     return parser.parse_args()
70
71
72 def main():
73     """Main function."""
74
75     log_levels = {u"NOTSET": logging.NOTSET,
76                   u"DEBUG": logging.DEBUG,
77                   u"INFO": logging.INFO,
78                   u"WARNING": logging.WARNING,
79                   u"ERROR": logging.ERROR,
80                   u"CRITICAL": logging.CRITICAL}
81
82     args = parse_args()
83     logging.basicConfig(format=u"%(asctime)s: %(levelname)s: %(message)s",
84                         datefmt=u"%Y/%m/%d %H:%M:%S",
85                         level=log_levels[args.logging])
86
87     logging.info(u"Application started.")
88     try:
89         spec = Specification(args.specification)
90         spec.read_specification()
91     except PresentationError:
92         logging.critical(u"Finished with error.")
93         return 1
94
95     if spec.output[u"output"] not in (u"report", u"CPTA"):
96         logging.critical(
97             f"The output {spec.output[u'output']} is not supported."
98         )
99         return 1
100
101     ret_code = 1
102     try:
103         env = Environment(spec.environment, args.force)
104         env.set_environment()
105
106         prepare_static_content(spec)
107
108         data = InputData(spec)
109         data.download_and_parse_data(repeat=1)
110         if args.print_all_oper_data:
111             data.print_all_oper_data()
112
113         generate_tables(spec, data)
114         generate_plots(spec, data)
115         generate_files(spec, data)
116
117         if spec.output[u"output"] == u"report":
118             generate_report(args.release, spec, args.week)
119         elif spec.output[u"output"] == u"CPTA":
120             sys.stdout.write(generate_cpta(spec, data))
121             try:
122                 alert = Alerting(spec)
123                 alert.generate_alerts()
124             except AlertingError as err:
125                 logging.warning(repr(err))
126
127         logging.info(u"Successfully finished.")
128         ret_code = 0
129
130     except AlertingError as err:
131         logging.critical(f"Finished with an alerting error.\n{repr(err)}")
132     except PresentationError as err:
133         logging.critical(f"Finished with an PAL error.\n{repr(err)}")
134     except (KeyError, ValueError) as err:
135         logging.critical(f"Finished with an error.\n{repr(err)}")
136     finally:
137         if spec is not None:
138             clean_environment(spec.environment)
139     return ret_code
140
141
142 if __name__ == u"__main__":
143     sys.exit(main())