From eeedfbc3985ec99989b1cba38e351e07d077f5a7 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 25 Jun 2021 11:52:01 +0200 Subject: [PATCH] Impiego health check in index page! --- GWMS.Data/GWMSContext.cs | 10 +++--- GWMS.UI/Health/Checks.cs | 73 +++++++++++++++++++++++++++++++++++++++ GWMS.UI/Pages/Index.razor | 6 +++- GWMS.UI/Startup.cs | 17 ++++++--- GWMS.UI/_Imports.razor | 1 + Resources/ChangeLog.html | 2 +- Resources/VersNum.txt | 2 +- Resources/manifest.xml | 2 +- 8 files changed, 100 insertions(+), 13 deletions(-) diff --git a/GWMS.Data/GWMSContext.cs b/GWMS.Data/GWMSContext.cs index ce1bae2..362207c 100644 --- a/GWMS.Data/GWMSContext.cs +++ b/GWMS.Data/GWMSContext.cs @@ -75,12 +75,12 @@ namespace GWMS.Data // tento setup da config try { - string server = _configuration["DbConfig:Server"]; - string nKey = _configuration["DbConfig:nKey"]; - string sKey = _configuration["DbConfig:sKey"]; + //string server = _configuration["DbConfig:Server"]; + //string nKey = _configuration["DbConfig:nKey"]; + //string sKey = _configuration["DbConfig:sKey"]; - DbConfig.InitDb(server, nKey, sKey); - DbConfig.CheckUser(nKey, sKey); + //DbConfig.InitDb(server, nKey, sKey); + //DbConfig.CheckUser(nKey, sKey); // uso conn string calcolata connString = DbConfig.CONNECTION_STRING; diff --git a/GWMS.UI/Health/Checks.cs b/GWMS.UI/Health/Checks.cs index 0fc5ed7..f065239 100644 --- a/GWMS.UI/Health/Checks.cs +++ b/GWMS.UI/Health/Checks.cs @@ -11,6 +11,79 @@ namespace GWMS.UI.Health { #region Public Methods + public static async Task DbPlantLogTable(string connStringDb) + { + using (var appDb = new GWMS.Data.GWMSContext()) + { + string description = "Try check Table PlantLog"; + List recordList = new List(); + var healthCheckData = new Dictionary(); + try + { + // provo a controllare se ho tab utenti + recordList = await Task.FromResult(appDb.DbSetPlantLog.ToList()).ConfigureAwait(false); + if (recordList.Count > 0) + { + description = $"Check PlantLog table, found {recordList.Count} records"; + return HealthCheckResult.Healthy(description, healthCheckData); + } + } + catch + { } + + return HealthCheckResult.Unhealthy(description + $" {connStringDb}", null, healthCheckData); + } + } + + public static async Task DbUserApp(string connStringDb) + { + using (var appDb = new GWMS.Data.GWMSContext()) + { + string description = "Try check Table AppUser"; + List userList = new List(); + var healthCheckData = new Dictionary(); + try + { + // provo a controllare se ho tab utenti + userList = appDb.DbSetUser.ToList(); + //userList = await Task.FromResult(appDb.DbSetUser.ToList()).ConfigureAwait(false); + if (userList.Count > 0) + { + description = $"Check AppUser table, found {userList.Count} records"; + return HealthCheckResult.Healthy(description, healthCheckData); + } + } + catch + { } + + return HealthCheckResult.Unhealthy(description + $" {connStringDb}", null, healthCheckData); + } + } + + public static async Task DbUserRoot(string connStringDb) + { + using (var adminDb = new GWMS.Data.AdminContext()) + { + string description = "Try check MySql User table"; + List userList = new List(); + var healthCheckData = new Dictionary(); + try + { + // provo a controllare se ho tab utenti + userList = await Task.FromResult(adminDb.UserList.ToList()).ConfigureAwait(false); + if (userList.Count > 0) + { + description = $"Check MySql User table, found {userList.Count} records"; + return HealthCheckResult.Healthy(description, healthCheckData); + } + } + catch + { } + + return HealthCheckResult.Unhealthy(description + $" {connStringDb}", null, healthCheckData); + } + } + public static async Task PingCheck(string hostName) { using (var thePing = new Ping()) diff --git a/GWMS.UI/Pages/Index.razor b/GWMS.UI/Pages/Index.razor index 49fa3c5..508dfe7 100644 --- a/GWMS.UI/Pages/Index.razor +++ b/GWMS.UI/Pages/Index.razor @@ -1,5 +1,6 @@ @page "/" +@using GWMS.Data @using GWMS.UI.Data @using GWMS.UI.Components @@ -54,6 +55,9 @@ AppMService.ShowSearch = false; AppMService.PageName = "Home"; AppMService.PageIcon = "fas fa-home pr-2"; - DataOk = await DataService.HasPlantLog(); + var resultUser = await Health.Checks.DbUserApp(DbConfig.CONNECTION_STRING); + var resultLog = await Health.Checks.DbPlantLogTable(DbConfig.CONNECTION_STRING); + DataOk = (resultUser.Status == HealthStatus.Healthy && resultLog.Status == HealthStatus.Healthy); + //DataOk = await DataService.HasPlantLog(); } } \ No newline at end of file diff --git a/GWMS.UI/Startup.cs b/GWMS.UI/Startup.cs index 0aba886..c0cb752 100644 --- a/GWMS.UI/Startup.cs +++ b/GWMS.UI/Startup.cs @@ -106,12 +106,15 @@ namespace GWMS.UI // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { - // init DB + // init info x DB string dbServerAddr = Configuration["DbConfig:Server"]; string nKey = Configuration["DbConfig:nKey"]; string sKey = Configuration["DbConfig:sKey"]; - DbConfig.InitDb(dbServerAddr, nKey, sKey); + // inizializzo il DB e creo (se necessario) l'utente + DbConfig.CheckUser(nKey, sKey); + + // altri parametri per check vari string connStringDB = DbConfig.CONNECTION_STRING; string connStringRedis = Configuration.GetConnectionString("Redis"); string redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":")); @@ -126,7 +129,11 @@ namespace GWMS.UI .AddAsyncCheck($"QrCode PING ({qrCodeAddr})", () => Health.Checks.PingCheck(qrCodeAddr)) .AddProcessAllocatedMemoryHealthCheck(512, "Max Process memory (<512MB)", failureStatus: HealthStatus.Degraded) // 512 MB max allocated memory .AddRedis(Configuration.GetConnectionString("Redis"), "Redis", failureStatus: HealthStatus.Degraded) - .AddUrlGroup(new Uri(Configuration["ZCodeUrl"]), name: $"QrCode Gen ({Configuration["ZCodeUrl"]})", failureStatus: HealthStatus.Degraded); + .AddUrlGroup(new Uri(Configuration["ZCodeUrl"]), name: $"QrCode Gen ({Configuration["ZCodeUrl"]})", failureStatus: HealthStatus.Degraded) + .AddAsyncCheck($"MySql Root User", () => Health.Checks.DbUserRoot(DbConfig.ADMIN_CONNECTION_STRING)) + .AddAsyncCheck($"MySql App Users", () => Health.Checks.DbUserApp(DbConfig.CONNECTION_STRING)) + .AddAsyncCheck($"MySql PlantLog", () => Health.Checks.DbPlantLogTable(DbConfig.CONNECTION_STRING)) + ; //.AddDiskStorageHealthCheck(s => s.AddDrive("C:\\", 1024)) // 1024 MB (1 GB) free minimum //.AddProcessHealthCheck("ProcessName", p => p.Length > 0) // check if process is running //.AddWindowsServiceHealthCheck("someservice", s => s.Status == ServiceControllerStatus.Running); @@ -135,7 +142,9 @@ namespace GWMS.UI .AddHealthChecksUI(s => { s.AddHealthCheckEndpoint("GWMS_Services", "health"); - s.SetEvaluationTimeInSeconds(5); + s.SetEvaluationTimeInSeconds(3); + //s.SetEvaluationTimeInSeconds(30); + s.SetMinimumSecondsBetweenFailureNotifications(60); s.SetApiMaxActiveRequests(5); s.SetHeaderText("GWMS Health Check Status"); }) diff --git a/GWMS.UI/_Imports.razor b/GWMS.UI/_Imports.razor index 17757ac..e0cf32e 100644 --- a/GWMS.UI/_Imports.razor +++ b/GWMS.UI/_Imports.razor @@ -5,6 +5,7 @@ @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web.Virtualization +@using Microsoft.Extensions.Diagnostics.HealthChecks @using Microsoft.JSInterop @using GWMS.UI @using GWMS.UI.Shared diff --git a/Resources/ChangeLog.html b/Resources/ChangeLog.html index 8a2da4b..8fac491 100644 --- a/Resources/ChangeLog.html +++ b/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo statistiche MAPO -

Versione: 1.0.2106.2510

+

Versione: 1.0.2106.2511


Note di rilascio:
    diff --git a/Resources/VersNum.txt b/Resources/VersNum.txt index dfbd944..6f7d172 100644 --- a/Resources/VersNum.txt +++ b/Resources/VersNum.txt @@ -1 +1 @@ -1.0.2106.2510 +1.0.2106.2511 diff --git a/Resources/manifest.xml b/Resources/manifest.xml index 6f37d68..126ce04 100644 --- a/Resources/manifest.xml +++ b/Resources/manifest.xml @@ -1,6 +1,6 @@ - 1.0.2106.2510 + 1.0.2106.2511 http://nexus.steamware.net/repository/SWS/MP-STATS/stable/0/GWMS.UI.zip http://nexus.steamware.net/repository/SWS/MP-STATS/stable/0/ChangeLog.html false