From f38bec4cf53c62ce46bafdd924c042fda17ea54f Mon Sep 17 00:00:00 2001 From: Rene Evertz Date: Tue, 26 May 2026 13:03:08 +0200 Subject: [PATCH] StatusService update + AiCheckRoutine --- Data/Access/ScriptDAO.cs | 9 ++ Service/BeWoServiceEnums.cs | 1 + .../Enhanced/StatusServiceImp.cs | 132 +++++++++++------- Shared/DataContracts/ServerStatusDC.cs | 2 +- 4 files changed, 95 insertions(+), 49 deletions(-) diff --git a/Data/Access/ScriptDAO.cs b/Data/Access/ScriptDAO.cs index b857adfe2..104c44f16 100644 --- a/Data/Access/ScriptDAO.cs +++ b/Data/Access/ScriptDAO.cs @@ -54,5 +54,14 @@ namespace BeWo.Data.Access return rtn; } + + public bool IsScriptExecuted(string script) + { + var sql = "SELECT Name FROM `.executedscripts`"; + + var rtn = Session.CreateSQLQuery(sql).List(); + + return rtn.Contains(script); + } } } diff --git a/Service/BeWoServiceEnums.cs b/Service/BeWoServiceEnums.cs index 7c59f8f81..f39e51168 100644 --- a/Service/BeWoServiceEnums.cs +++ b/Service/BeWoServiceEnums.cs @@ -30,6 +30,7 @@ namespace BeWo.Service OpenrouteServiceApiKey, GoogleDistanceMatrixApiKey, GkvSecretKey, + AppStatusApiKey } [DefaultValue(None)] diff --git a/Service/ServiceImplementations/Enhanced/StatusServiceImp.cs b/Service/ServiceImplementations/Enhanced/StatusServiceImp.cs index 73e4e7081..3c1f3b057 100644 --- a/Service/ServiceImplementations/Enhanced/StatusServiceImp.cs +++ b/Service/ServiceImplementations/Enhanced/StatusServiceImp.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using System.ServiceModel; using System.ServiceModel.Web; @@ -20,6 +21,24 @@ namespace BeWo.Service.ServiceImplementations.Enhanced ConcurrencyMode = ConcurrencyMode.Single)] public class StatusServiceImp : IStatusService { + private readonly List RequiredWebConfigs = new List() + { + WebConfigSetting.OllamaUrl, + WebConfigSetting.Ollama2Url, + //WebConfigSetting.XRechnungApiUrl, + //WebConfigSetting.StoredFilesFolderPath, + //WebConfigSetting.StoredTenantFilesFolderPath, + //WebConfigSetting.HelloWorldPdfLocation, + //WebConfigSetting.BeWoPlanerServiceRoot, + WebConfigSetting.AiPromptsUrl + }; + + private readonly List RequiredWebSecretConfigs = new List() + { + WebSecretConfigSetting.Ollama2Key, + WebSecretConfigSetting.AppStatusApiKey + }; + public ServerStatusDC GetStatus() { var result = new ServerStatusDC @@ -31,30 +50,31 @@ namespace BeWo.Service.ServiceImplementations.Enhanced }; // Datenbankverbindung prüfen - result.DatabaseOk = CheckDatabase(); + result.DatabaseOk = executeCheck("Datenbank", CheckDatabase); // Einzelne Subsysteme prüfen - //result.Services.Add(CheckService("Mail", CheckMail())); - result.Services.Add(CheckService("AIModule", CheckAiModule())); - result.Services.Add(CheckService("Web.config", CheckWebConfig())); + result.Services.Add(executeCheck("AIModule", CheckAiModule)); + result.Services.Add(executeCheck("Web.config", CheckWebConfig)); + result.Services.Add(executeCheck("Web.Secret.config", CheckWebSecretConfig)); + + var allHealthy = result.DatabaseOk.IsOk && result.Services.All(x => x.IsOk); + if (!allHealthy) + { + WebOperationContext.Current.OutgoingResponse.StatusCode = + HttpStatusCode.InternalServerError; + } return result; } - - private bool CheckDatabase() + + private string CheckDatabase() { - try - { - // Leichter DB-Ping – z.B. eine einfache Abfrage über DAOFactory - DAOFactory.GenericDAO.GetAll(); - return true; - } - catch - { - return false; - } + // Leichter DB-Ping – z.B. eine einfache Abfrage über DAOFactory + DAOFactory.GenericDAO.GetAll(); + + return null; } private bool CheckMail() @@ -73,51 +93,67 @@ namespace BeWo.Service.ServiceImplementations.Enhanced private string CheckAiModule() { - try - { - // #1 - var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomPreSystemPrompt); - if (!File.Exists(configPath)) - return "SpecialConfig.CustomPreSystemPrompt wurde nicht korrekt konfiguriert"; + // #1 + var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomPreSystemPrompt); + if (!File.Exists(configPath)) + return "SpecialConfig.CustomPreSystemPrompt wurde nicht korrekt konfiguriert"; - // #2 - if (string.IsNullOrEmpty(MergedConfig.GetSecretSetting(WebSecretConfigSetting.Ollama2Key))) - return "Ollama2Key wurde nicht korrekt konfiguriert"; + // #2 + if (string.IsNullOrEmpty(MergedConfig.GetSecretSetting(WebSecretConfigSetting.Ollama2Key))) + return "Ollama2Key wurde nicht korrekt konfiguriert"; - return null; - } - catch - { - return "Fehler"; - } + if (checkDatabaseScript("Changes_2026-05-22 AiConv ActionType") is string error) + return error; + + return null; } private string CheckWebConfig() { + foreach (var webConfig in RequiredWebConfigs) + { + var url = MergedConfig.GetSetting(webConfig); + if (string.IsNullOrWhiteSpace(url)) + return $"'{webConfig}' wurde nicht korrekt konfiguriert"; + } + + return null; + } + + private string CheckWebSecretConfig() + { + foreach (var webSecretConfig in RequiredWebSecretConfigs) + { + var key = MergedConfig.GetSecretSetting(webSecretConfig); + if (string.IsNullOrWhiteSpace(key)) + return $"'{webSecretConfig}' wurde nicht korrekt konfiguriert"; + } + + return null; + } + + private string checkDatabaseScript(string name) + { + var executed = DAOFactory.ScriptDAO.IsScriptExecuted(name); + + return executed ? null : $"Script '{name}' wurde nicht ausgeführt"; + } + + private ServiceStatusDC executeCheck(string name, Func validation) + { + string error; + try { - // #1 - var url = MergedConfig.GetSetting(WebConfigSetting.AiPromptsUrl); - if (!string.IsNullOrWhiteSpace(url)) - return "AiPromptsUrl wurde nicht korrekt konfiguriert"; - - return null; + error = validation(); } - catch + catch (Exception ex) { - return "Fehler"; + error = ex.ToString(); } - } - private ServiceStatusDC CheckService(string name, string error) - { - var ok = error is null; + var ok = string.IsNullOrEmpty(error); - return CheckService(name, ok, error); - } - - private ServiceStatusDC CheckService(string name, bool ok, string error) - { return new ServiceStatusDC { Name = name, diff --git a/Shared/DataContracts/ServerStatusDC.cs b/Shared/DataContracts/ServerStatusDC.cs index cbc1b4c53..1fb2c73b4 100644 --- a/Shared/DataContracts/ServerStatusDC.cs +++ b/Shared/DataContracts/ServerStatusDC.cs @@ -17,7 +17,7 @@ namespace BS.Shared.DataContracts public string Version { get; set; } [DataMember] - public bool DatabaseOk { get; set; } + public ServiceStatusDC DatabaseOk { get; set; } [DataMember] public List Services { get; set; }