102 lines
3.9 KiB
C#
102 lines
3.9 KiB
C#
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using NLog;
|
|
using System.Net.NetworkInformation;
|
|
using WebDoorCreator.Data;
|
|
using WebDoorCreator.UI.Components.Users;
|
|
|
|
namespace WebDoorCreator.UI.Health
|
|
{
|
|
public class Checks
|
|
{
|
|
#region Public Methods
|
|
|
|
public static async Task<HealthCheckResult> ConfigCount(IConfiguration _config)
|
|
{
|
|
string description = "Try check Config table";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
using (WDCDataContext localDbCtx = new WDCDataContext(_config))
|
|
{
|
|
var dbCount = localDbCtx
|
|
.DbSetConfig
|
|
.Count();
|
|
if (dbCount > 0)
|
|
{
|
|
description = $"Check Config table, found {dbCount} records";
|
|
healthCheckData.Add("Count", dbCount);
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
|
|
await Task.Delay(1);
|
|
return HealthCheckResult.Unhealthy(description + $" NO RECORD found", null, healthCheckData);
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> DoorsCount(IConfiguration _config)
|
|
{
|
|
string description = "Try check DOOR table";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
using (WDCDataContext localDbCtx = new WDCDataContext(_config))
|
|
{
|
|
var dbCount = localDbCtx
|
|
.DbSetDoor
|
|
.Count();
|
|
if (dbCount > 0)
|
|
{
|
|
description = $"Check DOOR table, found {dbCount} records";
|
|
healthCheckData.Add("Count", dbCount);
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
|
|
await Task.Delay(1);
|
|
return HealthCheckResult.Unhealthy(description + $" NO RECORD found", null, healthCheckData);
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> OrdersCount(IConfiguration _config)
|
|
{
|
|
string description = "Try check ORDER table";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
using (WDCDataContext localDbCtx = new WDCDataContext(_config))
|
|
{
|
|
var dbCount = localDbCtx
|
|
.DbSetOrders
|
|
.Count();
|
|
if (dbCount > 0)
|
|
{
|
|
description = $"Check ORDER table, found {dbCount} records";
|
|
healthCheckData.Add("Count", dbCount);
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
|
|
await Task.Delay(1);
|
|
return HealthCheckResult.Unhealthy(description + $" NO RECORD found", null, healthCheckData);
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> PingCheck(string hostName)
|
|
{
|
|
var description = $"Ping to {hostName}";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
using (var thePing = new Ping())
|
|
{
|
|
var pingResult = await thePing.SendPingAsync(hostName);
|
|
healthCheckData.Add("RoundTripMS", pingResult.RoundtripTime);
|
|
healthCheckData.Add("ActualIPAddress", pingResult.Address.ToString());
|
|
if (pingResult.Status == IPStatus.Success)
|
|
{
|
|
description += $" - {pingResult.RoundtripTime}ms";
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
return HealthCheckResult.Unhealthy(description + $" {hostName}", null, healthCheckData);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |