feat(topology): Enable 2 QATs
[csit.git] / csit.infra.dash / app / cdash / __init__.py
1 # Copyright (c) 2024 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 """Initialize Flask app.
15 """
16
17 import logging
18 import pandas as pd
19
20 from flask import Flask
21 from flask_assets import Environment, Bundle
22
23 from .utils.constants import Constants as C
24 from .data.data import Data
25
26
27 def init_app():
28     """Construct core Flask application with embedded Dash app.
29     """
30
31     logging.basicConfig(
32         format=C.LOG_FORMAT,
33         datefmt=C.LOG_DATE_FORMAT,
34         level=C.LOG_LEVEL
35     )
36
37     app = Flask(__name__, instance_relative_config=False)
38     app.logger.info("Application started.")
39     app.config.from_object("config.Config")
40
41     with app.app_context():
42         # Import parts of our core Flask app.
43         from . import routes
44
45         assets = Environment()
46         assets.init_app(app)
47
48         # Compile static assets.
49         sass_bundle = Bundle(
50             "sass/lux.scss",
51             filters="libsass",
52             output="dist/css/bootstrap.css",
53             depends="**/*.scss",
54             extra={
55                 "rel": "stylesheet"
56             }
57         )
58         assets.register("sass_all", sass_bundle)
59         sass_bundle.build()
60
61         if C.TIME_PERIOD is None or C.TIME_PERIOD > C.MAX_TIME_PERIOD:
62             time_period = C.MAX_TIME_PERIOD
63         else:
64             time_period = C.TIME_PERIOD
65
66         data = Data(
67             data_spec_file=C.DATA_SPEC_FILE,
68         ).read_all_data(days=time_period)
69
70         # Import Dash applications.
71         err_msg = "Application not loaded, no data available."
72         logging.info("\n\nStarting the applications:\n" + "-" * 26 + "\n")
73
74         if C.START_FAILURES:
75             logging.info(C.NEWS_TITLE)
76             if data["statistics"].empty or data["trending"].empty:
77                 logging.error(err_msg)
78             else:
79                 from .news.news import init_news
80                 app = init_news(app, data["statistics"], data["trending"])
81         if C.START_STATISTICS:
82             logging.info(C.STATS_TITLE)
83             if data["statistics"].empty or data["trending"].empty:
84                 logging.error(err_msg)
85             else:
86                 from .stats.stats import init_stats
87                 app = init_stats(app, data["statistics"], data["trending"])
88         if C.START_TRENDING:
89             logging.info(C.TREND_TITLE)
90             if data["trending"].empty:
91                 logging.error(err_msg)
92             else:
93                 from .trending.trending import init_trending
94                 app = init_trending(app, data["trending"])
95         if C.START_REPORT:
96             logging.info(C.REPORT_TITLE)
97             if data["iterative"].empty:
98                 logging.error(err_msg)
99             else:
100                 from .report.report import init_report
101                 app = init_report(app, data["iterative"])
102         if C.START_COMPARISONS:
103             logging.info(C.COMP_TITLE)
104             if data["iterative"].empty:
105                 logging.error(err_msg)
106             else:
107                 from .comparisons.comparisons import init_comparisons
108                 app = init_comparisons(app, data["iterative"])
109         if C.START_COVERAGE:
110             logging.info(C.COVERAGE_TITLE)
111             if data["coverage"].empty:
112                 logging.error(err_msg)
113             else:
114                 from .coverage.coverage import init_coverage
115                 app = init_coverage(app, data["coverage"])
116         if C.START_SEARCH:
117             logging.info(C.SEARCH_TITLE)
118             if all((data["trending"].empty, data["iterative"].empty,
119                     data["coverage"].empty)):
120                 logging.error(err_msg)
121             else:
122                 from .search.search import init_search
123                 app = init_search(app, data)
124
125     return app
126
127
128 app = init_app()