Files
BeWoPlaner/Service/ServiceImplementations/Enhanced/StatusServiceImp.cs

138 lines
3.0 KiB
C#
Raw 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-07 11:37:48 +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
{
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
result.DatabaseOk = CheckDatabase();
// Einzelne Subsysteme prüfen
//result.Services.Add(CheckService("Mail", CheckMail()));
result.Services.Add(CheckService("AIModule", CheckAiModule()));
2026-05-04 14:19:35 +02:00
result.Services.Add(CheckService("Web.config", CheckWebConfig()));
2026-03-30 13:06:03 +02:00
2026-05-07 11:37:48 +02:00
var allHealthy = result.DatabaseOk && 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-03-30 13:06:03 +02:00
private bool CheckDatabase()
{
try
{
// Leichter DB-Ping z.B. eine einfache Abfrage über DAOFactory
DAOFactory.GenericDAO.GetAll<ApplicationUser>();
return true;
}
catch
{
return false;
}
}
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
{
try
{
// #1
var configPath = ConfigReader.GetConfigPath(SpecialConfig.CustomPreSystemPrompt);
if (!File.Exists(configPath))
2026-04-07 12:01:30 +02:00
return "SpecialConfig.CustomPreSystemPrompt wurde nicht korrekt konfiguriert";
2026-03-30 13:06:03 +02:00
2026-04-07 12:01:30 +02:00
// #2
if (string.IsNullOrEmpty(MergedConfig.GetSecretSetting(WebSecretConfigSetting.Ollama2Key)))
return "Ollama2Key wurde nicht korrekt konfiguriert";
return null;
2026-03-30 13:06:03 +02:00
}
catch
{
2026-04-07 12:01:30 +02:00
return "Fehler";
2026-03-30 13:06:03 +02:00
}
}
2026-05-04 14:19:35 +02:00
private string CheckWebConfig()
{
try
{
// #1
var url = MergedConfig.GetSetting(WebConfigSetting.AiPromptsUrl);
2026-05-07 11:37:48 +02:00
if (string.IsNullOrWhiteSpace(url))
2026-05-04 14:19:35 +02:00
return "AiPromptsUrl wurde nicht korrekt konfiguriert";
return null;
}
catch
{
return "Fehler";
}
}
2026-04-07 12:01:30 +02:00
private ServiceStatusDC CheckService(string name, string error)
{
var ok = error is null;
return CheckService(name, ok, error);
}
private ServiceStatusDC CheckService(string name, bool ok, string 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
};
}
}
}