Refactor health check HTML generation logic
Centralized health check HTML generation into a new static `HealthCheckHtmlGenerator` class to improve modularity and maintainability. This class encapsulates the logic for generating HTML, including navigation, overall status, summary cards, individual checks, and refresh information. Replaced the inline `GenerateHealthCheckHtml` method in `Program.cs` with the new `HealthCheckHtmlGenerator.Generate` method, removing redundant code and improving separation of concerns. Updated `Program.cs` to include the necessary namespace. Simplified the `/health-ui` endpoint to use the new utility class, reducing code duplication and improving readability.
This commit is contained in:
257
ECMJobRunner.WebCron/HealthCheck/HealthCheckHtmlGenerator.cs
Normal file
257
ECMJobRunner.WebCron/HealthCheck/HealthCheckHtmlGenerator.cs
Normal file
@@ -0,0 +1,257 @@
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using System.Text;
|
||||
|
||||
namespace ECMJobRunner.WebCron.HealthCheck;
|
||||
|
||||
/// <summary>
|
||||
/// Generates HTML representation of health check reports.
|
||||
/// </summary>
|
||||
public static class HealthCheckHtmlGenerator
|
||||
{
|
||||
public static string Generate(HealthReport report)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine(@"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=""UTF-8"">
|
||||
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
|
||||
<title>Health Check - ECMJobRunner</title>
|
||||
<link rel=""stylesheet"" href=""https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"" />
|
||||
<link rel=""stylesheet"" href=""/css/health-ui.css"" />
|
||||
<script src=""/js/health-ui.js""></script>
|
||||
</head>
|
||||
<body>");
|
||||
|
||||
// Navigation
|
||||
AppendNavigation(sb);
|
||||
|
||||
// Overall Status Header
|
||||
AppendOverallStatus(sb, report);
|
||||
|
||||
// Summary Card
|
||||
AppendSummaryCard(sb, report);
|
||||
|
||||
// Individual Checks
|
||||
AppendIndividualChecks(sb, report);
|
||||
|
||||
// Refresh Info
|
||||
AppendRefreshInfo(sb);
|
||||
|
||||
sb.AppendLine(@"
|
||||
</body>
|
||||
</html>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static void AppendNavigation(StringBuilder sb)
|
||||
{
|
||||
sb.Append(@"
|
||||
<div class=""container-fluid"">
|
||||
<div class=""nav-links"">");
|
||||
|
||||
// Append emojis separately (not in verbatim string)
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/\">🏠 Home</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/hangfire\">🔧 Hangfire Dashboard</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/serilog-ui\">📋 Logs (Serilog.UI)</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/health\">📊 Health API</a>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(" </div>");
|
||||
}
|
||||
|
||||
private static void AppendOverallStatus(StringBuilder sb, HealthReport report)
|
||||
{
|
||||
var statusClass = GetStatusClass(report.Status);
|
||||
var badgeClass = GetBadgeClass(report.Status);
|
||||
var icon = GetStatusIcon(report.Status);
|
||||
|
||||
sb.Append($@"
|
||||
<div class=""header-card"">
|
||||
<div class=""d-flex justify-content-between align-items-center"">
|
||||
<div>
|
||||
<h1 class=""mb-2"">");
|
||||
sb.Append(icon);
|
||||
sb.Append($@" Health Check Status</h1>
|
||||
<p class=""text-muted mb-0"">Last checked: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</p>
|
||||
</div>
|
||||
<div>
|
||||
<h2><span class=""badge {badgeClass} fs-3"">{report.Status}</span></h2>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""row"">
|
||||
<div class=""col-md-4"">");
|
||||
}
|
||||
|
||||
private static void AppendSummaryCard(StringBuilder sb, HealthReport report)
|
||||
{
|
||||
var statusClass = GetStatusClass(report.Status);
|
||||
|
||||
sb.Append(@"
|
||||
<div class=""card"">
|
||||
<div class=""card-header bg-primary text-white"">");
|
||||
sb.AppendLine();
|
||||
sb.Append(" 📊 Summary");
|
||||
sb.AppendLine();
|
||||
sb.Append($@" </div>
|
||||
<div class=""card-body"">
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Overall Status:</span>
|
||||
<strong class=""{statusClass}"">{report.Status}</strong>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Total Duration:</span>
|
||||
<span class=""metric-value"">{report.TotalDuration.TotalMilliseconds:F0} ms</span>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Checks:</span>
|
||||
<span class=""metric-value"">{report.Entries.Count}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""col-md-8"">");
|
||||
}
|
||||
|
||||
private static void AppendIndividualChecks(StringBuilder sb, HealthReport report)
|
||||
{
|
||||
foreach (var entry in report.Entries)
|
||||
{
|
||||
var checkStatusClass = GetStatusClass(entry.Value.Status);
|
||||
var checkBadgeClass = GetBadgeClass(entry.Value.Status);
|
||||
var checkIcon = GetStatusIcon(entry.Value.Status);
|
||||
|
||||
sb.Append($@"
|
||||
<div class=""card"">
|
||||
<div class=""card-header bg-light"">
|
||||
<div class=""d-flex justify-content-between align-items-center"">
|
||||
<span>");
|
||||
sb.Append(checkIcon);
|
||||
sb.Append($@" <strong>{entry.Key}</strong></span>
|
||||
<span class=""badge {checkBadgeClass}"">{entry.Value.Status}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class=""card-body"">
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Description:</span>
|
||||
<span class=""metric-value"">{entry.Value.Description ?? "N/A"}</span>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Duration:</span>
|
||||
<span class=""metric-value"">{entry.Value.Duration.TotalMilliseconds:F0} ms</span>
|
||||
</div>");
|
||||
|
||||
if (entry.Value.Exception != null)
|
||||
{
|
||||
sb.AppendLine($@"
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Exception:</span>
|
||||
<span class=""metric-value text-danger"" style=""word-break: break-all;"">{System.Net.WebUtility.HtmlEncode(entry.Value.Exception.Message)}</span>
|
||||
</div>");
|
||||
}
|
||||
|
||||
if (entry.Value.Data.Any())
|
||||
{
|
||||
AppendCheckDetails(sb, entry.Value.Data);
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</div>
|
||||
</div>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</div>
|
||||
</div>
|
||||
</div>");
|
||||
}
|
||||
|
||||
private static void AppendCheckDetails(StringBuilder sb, IReadOnlyDictionary<string, object> data)
|
||||
{
|
||||
sb.AppendLine(@"
|
||||
<div class=""mt-3"">
|
||||
<strong class=""metric-label"">Details:</strong>
|
||||
<table class=""table table-sm table-borderless mt-2"">");
|
||||
|
||||
foreach (var item in data)
|
||||
{
|
||||
var value = FormatDataValue(item.Value);
|
||||
|
||||
sb.AppendLine($@"
|
||||
<tr>
|
||||
<td class=""text-muted"" style=""width: 40%;"">{item.Key}</td>
|
||||
<td><strong>{value}</strong></td>
|
||||
</tr>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</table>
|
||||
</div>");
|
||||
}
|
||||
|
||||
private static void AppendRefreshInfo(StringBuilder sb)
|
||||
{
|
||||
sb.AppendLine(@"
|
||||
<div class=""refresh-info"">");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <span class=\"spinner\"></span> Auto-refresh in <strong><span id=\"countdown\">10</span>s</strong>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(@" </div>");
|
||||
}
|
||||
|
||||
private static string GetStatusClass(HealthStatus status)
|
||||
{
|
||||
return status switch
|
||||
{
|
||||
HealthStatus.Healthy => "status-healthy",
|
||||
HealthStatus.Degraded => "status-degraded",
|
||||
HealthStatus.Unhealthy => "status-unhealthy",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetBadgeClass(HealthStatus status)
|
||||
{
|
||||
return status switch
|
||||
{
|
||||
HealthStatus.Healthy => "badge-healthy",
|
||||
HealthStatus.Degraded => "badge-degraded",
|
||||
HealthStatus.Unhealthy => "badge-unhealthy",
|
||||
_ => "badge-secondary"
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetStatusIcon(HealthStatus status)
|
||||
{
|
||||
return status switch
|
||||
{
|
||||
HealthStatus.Healthy => "✅",
|
||||
HealthStatus.Degraded => "⚠️",
|
||||
HealthStatus.Unhealthy => "❌",
|
||||
_ => "❓"
|
||||
};
|
||||
}
|
||||
|
||||
private static string FormatDataValue(object? value)
|
||||
{
|
||||
if (value == null)
|
||||
return "N/A";
|
||||
|
||||
if (value is DateTime dt)
|
||||
return dt.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
if (value is TimeSpan ts)
|
||||
return $"{ts.TotalSeconds:F1}s";
|
||||
|
||||
return value.ToString() ?? "N/A";
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using ECMJobRunner.Application;
|
||||
using ECMJobRunner.Infrastructure;
|
||||
using ECMJobRunner.WebCron;
|
||||
using ECMJobRunner.WebCron.HealthCheck;
|
||||
using ECMJobRunner.WebCron.ProfileWorker;
|
||||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
@@ -10,7 +11,6 @@ using Serilog;
|
||||
using Serilog.Ui.Core.Extensions;
|
||||
using Serilog.Ui.SqliteDataProvider.Extensions;
|
||||
using Serilog.Ui.Web.Extensions;
|
||||
using System.Text;
|
||||
|
||||
// Enable Serilog self-diagnostics
|
||||
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine($"[SERILOG] {msg}"));
|
||||
@@ -148,7 +148,7 @@ app.MapGet("/health-ui", async (HealthCheckService healthCheckService, HttpConte
|
||||
{
|
||||
var report = await healthCheckService.CheckHealthAsync(context.RequestAborted);
|
||||
|
||||
var html = GenerateHealthCheckHtml(report);
|
||||
var html = HealthCheckHtmlGenerator.Generate(report);
|
||||
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(html);
|
||||
@@ -204,216 +204,3 @@ finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
// Helper method to generate health check HTML
|
||||
static string GenerateHealthCheckHtml(HealthReport report)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine(@"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=""UTF-8"">
|
||||
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
|
||||
<title>Health Check - ECMJobRunner</title>
|
||||
<link rel=""stylesheet"" href=""https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"" />
|
||||
<link rel=""stylesheet"" href=""/css/health-ui.css"" />
|
||||
<script src=""/js/health-ui.js""></script>
|
||||
</head>
|
||||
<body>");
|
||||
|
||||
// Navigation
|
||||
sb.Append(@"
|
||||
<div class=""container-fluid"">
|
||||
<div class=""nav-links"">");
|
||||
|
||||
// Append emojis separately (not in verbatim string)
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/\">🏠 Home</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/hangfire\">🔧 Hangfire Dashboard</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/serilog-ui\">📋 Logs (Serilog.UI)</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/health\">📊 Health API</a>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(" </div>");
|
||||
|
||||
// Overall Status Header
|
||||
var statusClass = report.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "status-healthy",
|
||||
HealthStatus.Degraded => "status-degraded",
|
||||
HealthStatus.Unhealthy => "status-unhealthy",
|
||||
_ => ""
|
||||
};
|
||||
|
||||
var badgeClass = report.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "badge-healthy",
|
||||
HealthStatus.Degraded => "badge-degraded",
|
||||
HealthStatus.Unhealthy => "badge-unhealthy",
|
||||
_ => "badge-secondary"
|
||||
};
|
||||
|
||||
var icon = report.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "✅",
|
||||
HealthStatus.Degraded => "⚠️",
|
||||
HealthStatus.Unhealthy => "❌",
|
||||
_ => "❓"
|
||||
};
|
||||
|
||||
sb.Append($@"
|
||||
<div class=""header-card"">
|
||||
<div class=""d-flex justify-content-between align-items-center"">
|
||||
<div>
|
||||
<h1 class=""mb-2"">");
|
||||
sb.Append(icon);
|
||||
sb.Append($@" Health Check Status</h1>
|
||||
<p class=""text-muted mb-0"">Last checked: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</p>
|
||||
</div>
|
||||
<div>
|
||||
<h2><span class=""badge {badgeClass} fs-3"">{report.Status}</span></h2>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""row"">
|
||||
<div class=""col-md-4"">
|
||||
<div class=""card"">
|
||||
<div class=""card-header bg-primary text-white"">");
|
||||
sb.AppendLine();
|
||||
sb.Append(" 📊 Summary");
|
||||
sb.AppendLine();
|
||||
sb.Append($@" </div>
|
||||
<div class=""card-body"">
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Overall Status:</span>
|
||||
<strong class=""{statusClass}"">{report.Status}</strong>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Total Duration:</span>
|
||||
<span class=""metric-value"">{report.TotalDuration.TotalMilliseconds:F0} ms</span>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Checks:</span>
|
||||
<span class=""metric-value"">{report.Entries.Count}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""col-md-8"">");
|
||||
|
||||
// Individual Checks
|
||||
foreach (var entry in report.Entries)
|
||||
{
|
||||
var checkStatusClass = entry.Value.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "status-healthy",
|
||||
HealthStatus.Degraded => "status-degraded",
|
||||
HealthStatus.Unhealthy => "status-unhealthy",
|
||||
_ => ""
|
||||
};
|
||||
|
||||
var checkBadgeClass = entry.Value.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "badge-healthy",
|
||||
HealthStatus.Degraded => "badge-degraded",
|
||||
HealthStatus.Unhealthy => "badge-unhealthy",
|
||||
_ => "badge-secondary"
|
||||
};
|
||||
|
||||
var checkIcon = entry.Value.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "✅",
|
||||
HealthStatus.Degraded => "⚠️",
|
||||
HealthStatus.Unhealthy => "❌",
|
||||
_ => "❓"
|
||||
};
|
||||
|
||||
sb.Append($@"
|
||||
<div class=""card"">
|
||||
<div class=""card-header bg-light"">
|
||||
<div class=""d-flex justify-content-between align-items-center"">
|
||||
<span>");
|
||||
sb.Append(checkIcon);
|
||||
sb.Append($@" <strong>{entry.Key}</strong></span>
|
||||
<span class=""badge {checkBadgeClass}"">{entry.Value.Status}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class=""card-body"">
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Description:</span>
|
||||
<span class=""metric-value"">{entry.Value.Description ?? "N/A"}</span>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Duration:</span>
|
||||
<span class=""metric-value"">{entry.Value.Duration.TotalMilliseconds:F0} ms</span>
|
||||
</div>");
|
||||
|
||||
if (entry.Value.Exception != null)
|
||||
{
|
||||
sb.AppendLine($@"
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Exception:</span>
|
||||
<span class=""metric-value text-danger"" style=""word-break: break-all;"">{System.Net.WebUtility.HtmlEncode(entry.Value.Exception.Message)}</span>
|
||||
</div>");
|
||||
}
|
||||
|
||||
if (entry.Value.Data.Any())
|
||||
{
|
||||
sb.AppendLine(@"
|
||||
<div class=""mt-3"">
|
||||
<strong class=""metric-label"">Details:</strong>
|
||||
<table class=""table table-sm table-borderless mt-2"">");
|
||||
|
||||
foreach (var data in entry.Value.Data)
|
||||
{
|
||||
var value = data.Value?.ToString() ?? "N/A";
|
||||
|
||||
// Format DateTime values
|
||||
if (data.Value is DateTime dt)
|
||||
{
|
||||
value = dt.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
else if (data.Value is TimeSpan ts)
|
||||
{
|
||||
value = $"{ts.TotalSeconds:F1}s";
|
||||
}
|
||||
|
||||
sb.AppendLine($@"
|
||||
<tr>
|
||||
<td class=""text-muted"" style=""width: 40%;"">{data.Key}</td>
|
||||
<td><strong>{value}</strong></td>
|
||||
</tr>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</table>
|
||||
</div>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</div>
|
||||
</div>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""refresh-info"">");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <span class=\"spinner\"></span> Auto-refresh in <strong><span id=\"countdown\">10</span>s</strong>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(@" </div>
|
||||
|
||||
</body>
|
||||
</html>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user