diff --git a/src/wattanalyse/external_interface.py b/src/wattanalyse/external_interface.py index b3513eb..fa397e6 100644 --- a/src/wattanalyse/external_interface.py +++ b/src/wattanalyse/external_interface.py @@ -1,6 +1,8 @@ +from __future__ import annotations + import sys import time -from typing import Final +from typing import TYPE_CHECKING, Final import dopt_basics.datetime as dopt_dt import oracledb @@ -11,28 +13,13 @@ from wattanalyse import db, pipelines from wattanalyse.constants import USER_CFG from wattanalyse.logging import logger_base, logger_database, logger_pipeline -try: - ORACLE_CONN = oracledb.connect( - user=USER_CFG.Datenbank.Nutzer, - password=USER_CFG.Datenbank.Passwort, - host=USER_CFG.Datenbank.Host, - port=USER_CFG.Datenbank.Port, - service_name=USER_CFG.Datenbank.Service_Name, - ) -except OperationalError as err: - logger_base.error( - ( - "[Oracle Database] Could not establish connection. Check if the database " - "online, fully functional and reachable. Check the configuration parameters.\n" - ">>> Exception:\n%s" - ), - err, - stack_info=True, - ) - sys.exit(1) +if TYPE_CHECKING: + from wattanalyse.pipelines import OracleConnection -def pipeline_KPI_calculation() -> int: +def pipeline_KPI_calculation( + conn: OracleConnection, +) -> int: return_code: int = 0 PIPELINE_NAME: Final[str] = "KPI_calculation" logger_pipeline.info("Start pipeline >%s<", PIPELINE_NAME) @@ -40,7 +27,7 @@ def pipeline_KPI_calculation() -> int: start = dopt_dt.current_time_tz() t1 = time.perf_counter_ns() - res_pipe = pipelines.KPI_calculation(ORACLE_CONN) + res_pipe = pipelines.KPI_calculation(conn) t2 = time.perf_counter_ns() @@ -89,14 +76,34 @@ def pipeline_KPI_calculation() -> int: return return_code -if __name__ == "__main__": +def main() -> None: try: - code = pipeline_KPI_calculation() + ORACLE_CONN = oracledb.connect( + user=USER_CFG.Datenbank.Nutzer, + password=USER_CFG.Datenbank.Passwort, + host=USER_CFG.Datenbank.Host, + port=USER_CFG.Datenbank.Port, + service_name=USER_CFG.Datenbank.Service_Name, + ) + except OperationalError as err: + logger_base.critical( + ( + "[Oracle Database] Could not establish connection. Check if the database " + "is online, fully functional and reachable. Check the configuration " + "parameters.\n>>> Exception:\n%s" + ), + err, + stack_info=True, + ) + sys.exit(1) + + try: + code = pipeline_KPI_calculation(ORACLE_CONN) sys.exit(code) except Exception as err: - logger_base.error( + logger_base.critical( ( - "[BASE ERROR] An unexpected and unwrapped error occurred during the " + "[BASE] An unexpected and unwrapped error occurred during the " "execution of the pipeline function.\n>>> Exception:\n%s" ), err, @@ -105,3 +112,7 @@ if __name__ == "__main__": sys.exit(1) finally: ORACLE_CONN.close() + + +if __name__ == "__main__": + main()