3d3f2001a3b8a6ce5c35b2aad06987512c4ea463
[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     logging.basicConfig(
31         format=C.LOG_FORMAT,
32         datefmt=C.LOG_DATE_FORMAT,
33         level=C.LOG_LEVEL
34     )
35
36     app = Flask(__name__, instance_relative_config=False)
37     app.logger.info("Application started.")
38     app.config.from_object("config.Config")
39
40     with app.app_context():
41         # Import parts of our core Flask app.
42         from . import routes
43
44         assets = Environment()
45         assets.init_app(app)
46
47         # Compile static assets.
48         sass_bundle = Bundle(
49             "sass/lux.scss",
50             filters="libsass",
51             output="dist/css/bootstrap.css",
52             depends="**/*.scss",
53             extra={
54                 "rel": "stylesheet"
55             }
56         )
57         assets.register("sass_all", sass_bundle)
58         sass_bundle.build()
59
60         if C.TIME_PERIOD is None or C.TIME_PERIOD > C.MAX_TIME_PERIOD:
61             time_period = C.MAX_TIME_PERIOD
62         else:
63             time_period = C.TIME_PERIOD
64
65         data = Data(
66             data_spec_file=C.DATA_SPEC_FILE,
67         ).read_all_data(days=time_period)
68
69         # Import Dash applications.
70         logging.info("\n\nStarting the applications:\n" + "-" * 26 + "\n")
71         if data["statistics"].empty or data["trending"].empty:
72             logging.error(
73                 f'"{C.NEWS_TITLE}" application not loaded, no data available.'
74             )
75             logging.error(
76                 f'"{C.STATS_TITLE}" application not loaded, no data available.'
77             )
78         else:
79             logging.info(C.NEWS_TITLE)
80             from .news.news import init_news
81             app = init_news(
82                 app,
83                 data_stats=data["statistics"],
84                 data_trending=data["trending"]
85             )
86
87             logging.info(C.STATS_TITLE)
88             from .stats.stats import init_stats
89             app = init_stats(
90                 app,
91                 data_stats=data["statistics"],
92                 data_trending=data["trending"]
93             )
94
95         if data["trending"].empty:
96             logging.error(
97                 f'"{C.TREND_TITLE}" application not loaded, no data available.'
98             )
99         else:
100             logging.info(C.TREND_TITLE)
101             from .trending.trending import init_trending
102             app = init_trending(app, data_trending=data["trending"])
103
104         if data["iterative"].empty:
105             logging.error(
106                 f'"{C.REPORT_TITLE}" application not loaded, no data available.'
107             )
108             logging.error(
109                 f'"{C.COMP_TITLE}" application not loaded, no data available.'
110             )
111         else:
112             logging.info(C.REPORT_TITLE)
113             from .report.report import init_report
114             app = init_report(app, data_iterative=data["iterative"])
115
116             logging.info(C.COMP_TITLE)
117             from .comparisons.comparisons import init_comparisons
118             app = init_comparisons(app, data_iterative=data["iterative"])
119
120         if data["coverage"].empty:
121             logging.error((
122                 f'"{C.COVERAGE_TITLE}" application not loaded, '
123                 'no data available.'
124             ))
125         else:
126             logging.info(C.COVERAGE_TITLE)
127             from .coverage.coverage import init_coverage
128             app = init_coverage(app, data_coverage=data["coverage"])
129
130         if all((data["trending"].empty, data["iterative"].empty,
131                 data["coverage"].empty)):
132             logging.error((
133                 f'"{C.SEARCH_TITLE}" application not loaded, '
134                 'no data available.'
135             ))
136         else:
137             logging.info(C.SEARCH_TITLE)
138             from .search.search import init_search
139             app = init_search(app, data)
140
141     return app
142
143
144 app = init_app()