Files

181 lines
4.5 KiB
C#
Raw Permalink Normal View History

2026-03-30 13:06:03 +02:00
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Attributes;
using BeWo.Service.Core;
using BeWo.Service.ServiceContracts.Enhanced;
using BS.Shared.DataContracts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2026-05-26 13:03:08 +02:00
using System.Net;
2026-03-30 13:06:03 +02:00
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
namespace BeWo.Service.ServiceImplementations.Enhanced
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Single)]
public class StatusServiceImp : IStatusService
{
2026-05-26 13:03:08 +02:00
private readonly List<WebConfigSetting> RequiredWebConfigs = new List<WebConfigSetting>()
{
WebConfigSetting.OllamaUrl,
WebConfigSetting.Ollama2Url,
//WebConfigSetting.XRechnungApiUrl,
//WebConfigSetting.StoredFilesFolderPath,
//WebConfigSetting.StoredTenantFilesFolderPath,
//WebConfigSetting.HelloWorldPdfLocation,
//WebConfigSetting.BeWoPlanerServiceRoot,
WebConfigSetting.AiPromptsUrl
};
private readonly List<WebSecretConfigSetting> RequiredWebSecretConfigs = new List<WebSecretConfigSetting>()
{
WebSecretConfigSetting.Ollama2Key,
WebSecretConfigSetting.AppStatusApiKey
};
2026-03-30 13:06:03 +02:00
public ServerStatusDC GetStatus()
{
var result = new ServerStatusDC
{
ServerTime = DateTime.Now.ToString("o"),
Version = Assembly.GetExecutingAssembly()
.GetName().Version?.ToString() ?? "unbekannt",
Services = new List<ServiceStatusDC>()
};
// Datenbankverbindung prüfen
2026-05-26 13:03:08 +02:00
result.DatabaseOk = executeCheck("Datenbank", CheckDatabase);
2026-03-30 13:06:03 +02:00
// Einzelne Subsysteme prüfen
2026-05-26 13:03:08 +02:00
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;
}
2026-03-30 13:06:03 +02:00
return result;
}
2026-05-04 14:19:35 +02:00
2026-05-26 13:03:08 +02:00
private string CheckDatabase()
2026-03-30 13:06:03 +02:00
{
2026-05-26 13:03:08 +02:00
// Leichter DB-Ping z.B. eine einfache Abfrage über DAOFactory
DAOFactory.GenericDAO.GetAll<ApplicationUser>();
return null;
2026-03-30 13:06:03 +02:00
}
private bool CheckMail()
{
try
{
// z.B. SMTP-Verbindung kurz aufbauen und sofort schließen
// Kann auch weggelassen / später ergänzt werden
return true;
}
catch
{
return false;
}
}
2026-04-07 12:01:30 +02:00
private string CheckAiModule()
2026-03-30 13:06:03 +02:00
{
2026-05-26 13:03:08 +02:00
// #1
var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomPreSystemPrompt);
if (!File.Exists(configPath))
return "SpecialConfig.CustomPreSystemPrompt wurde nicht korrekt konfiguriert";
2026-03-30 13:06:03 +02:00
2026-06-19 11:21:50 +02:00
var configPath2 = ConfigReader.GetConfigPath(SpecialConfig.CustomFunctionPreSystemPrompt);
if (!File.Exists(configPath2))
return "SpecialConfig.CustomFunctionPreSystemPrompt wurde nicht korrekt konfiguriert";
2026-05-26 13:03:08 +02:00
// #2
if (string.IsNullOrEmpty(MergedConfig.GetSecretSetting(WebSecretConfigSetting.Ollama2Key)))
return "Ollama2Key wurde nicht korrekt konfiguriert";
2026-04-07 12:01:30 +02:00
2026-06-18 16:32:17 +02:00
// #3
var scripts = new string[]
{
"Changes_2026-05-22 AiConv ActionType",
"Changes_2026-06-03 AiRoutine RoutineType",
"Changes_2026-06-18 AiModel default updaten",
};
foreach (var script in scripts)
{
if (checkDatabaseScript(script) is string error)
return error;
}
2026-05-26 13:03:08 +02:00
return null;
2026-03-30 13:06:03 +02:00
}
2026-05-04 14:19:35 +02:00
private string CheckWebConfig()
{
2026-05-26 13:03:08 +02:00
foreach (var webConfig in RequiredWebConfigs)
2026-05-04 14:19:35 +02:00
{
2026-05-26 13:03:08 +02:00
var url = MergedConfig.GetSetting(webConfig);
if (string.IsNullOrWhiteSpace(url))
return $"'{webConfig}' wurde nicht korrekt konfiguriert";
2026-05-04 14:19:35 +02:00
}
2026-05-26 13:03:08 +02:00
return null;
}
private string CheckWebSecretConfig()
{
foreach (var webSecretConfig in RequiredWebSecretConfigs)
2026-05-04 14:19:35 +02:00
{
2026-05-26 13:03:08 +02:00
var key = MergedConfig.GetSecretSetting(webSecretConfig);
if (string.IsNullOrWhiteSpace(key))
return $"'{webSecretConfig}' wurde nicht korrekt konfiguriert";
2026-05-04 14:19:35 +02:00
}
2026-05-26 13:03:08 +02:00
return null;
2026-05-04 14:19:35 +02:00
}
2026-05-26 13:03:08 +02:00
private string checkDatabaseScript(string name)
2026-04-07 12:01:30 +02:00
{
2026-05-26 13:03:08 +02:00
var executed = DAOFactory.ScriptDAO.IsScriptExecuted(name);
2026-04-07 12:01:30 +02:00
2026-05-26 13:03:08 +02:00
return executed ? null : $"Script '{name}' wurde nicht ausgeführt";
2026-04-07 12:01:30 +02:00
}
2026-05-26 13:03:08 +02:00
private ServiceStatusDC executeCheck(string name, Func<string> validation)
2026-03-30 13:06:03 +02:00
{
2026-05-26 13:03:08 +02:00
string error;
try
{
error = validation();
}
catch (Exception ex)
{
error = ex.ToString();
}
var ok = string.IsNullOrEmpty(error);
2026-03-30 13:06:03 +02:00
return new ServiceStatusDC
{
Name = name,
IsOk = ok,
2026-04-07 12:01:30 +02:00
Message = ok ? "OK" : error
2026-03-30 13:06:03 +02:00
};
}
}
}