141 lines
5.5 KiB
C#
141 lines
5.5 KiB
C#
#if false
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using OrpaonVision.SiteApp.Runtime.Services;
|
|
|
|
namespace OrpaonVision.SiteApp.Runtime.HealthChecks;
|
|
|
|
/// <summary>
|
|
/// 报警系统健康检查。
|
|
/// </summary>
|
|
public sealed class AlarmSystemHealthCheck : IHealthCheck
|
|
{
|
|
private readonly IAlarmSystemService _alarmSystemService;
|
|
private readonly ILogger<AlarmSystemHealthCheck> _logger;
|
|
|
|
public AlarmSystemHealthCheck(IAlarmSystemService alarmSystemService, ILogger<AlarmSystemHealthCheck> logger)
|
|
{
|
|
_alarmSystemService = alarmSystemService;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
// 检查活跃报警数量
|
|
var activeAlarmsResult = await _alarmSystemService.GetActiveAlarmsAsync(cancellationToken);
|
|
|
|
if (!activeAlarmsResult.IsSuccess)
|
|
{
|
|
return HealthCheckResult.Unhealthy(
|
|
"报警系统服务不可用",
|
|
new Dictionary<string, object>
|
|
{
|
|
["error"] = activeAlarmsResult.Message,
|
|
["trace_id"] = activeAlarmsResult.TraceId ?? string.Empty
|
|
});
|
|
}
|
|
|
|
var activeAlarmCount = activeAlarmsResult.Data?.Count ?? 0;
|
|
|
|
// 检查报警栈状态
|
|
var criticalStackResult = await _alarmSystemService.GetAlarmStackAsync(Core.AlarmSystem.AlarmStackType.Critical, cancellationToken);
|
|
var highStackResult = await _alarmSystemService.GetAlarmStackAsync(Core.AlarmSystem.AlarmStackType.High, cancellationToken);
|
|
|
|
var criticalCount = criticalStackResult.IsSuccess ? criticalStackResult.Data?.Count ?? 0 : 0;
|
|
var highCount = highStackResult.IsSuccess ? highStackResult.Data?.Count ?? 0 : 0;
|
|
|
|
// 测试报警触发功能
|
|
var testAlarmId = Guid.NewGuid();
|
|
var testResult = await _alarmSystemService.TriggerAlarmAsync(new Core.AlarmSystem.AlarmRequest
|
|
{
|
|
RequestId = Guid.NewGuid(),
|
|
AlarmType = Core.AlarmSystem.AlarmType.SystemTest,
|
|
AlarmLevel = Core.AlarmSystem.AlarmLevel.Info,
|
|
Title = "健康检查测试报警",
|
|
Description = "用于健康检查的测试报警",
|
|
SessionId = Guid.NewGuid(),
|
|
AutoClear = true,
|
|
AutoClearAfterSeconds = 10
|
|
}, cancellationToken);
|
|
|
|
var testSuccess = testResult.IsSuccess;
|
|
var testAlarmIdResult = testResult.Data?.AlarmId ?? Guid.Empty;
|
|
|
|
// 如果测试成功,立即清除测试报警
|
|
if (testSuccess && testAlarmIdResult != Guid.Empty)
|
|
{
|
|
await _alarmSystemService.ClearAlarmAsync(new Core.AlarmSystem.AlarmClearRequest
|
|
{
|
|
AlarmId = testAlarmIdResult,
|
|
ClearUser = "HealthCheck",
|
|
ClearReason = "健康检查测试报警清除"
|
|
}, cancellationToken);
|
|
}
|
|
|
|
// 判断健康状态
|
|
var data = new Dictionary<string, object>
|
|
{
|
|
["active_alarm_count"] = activeAlarmCount,
|
|
["critical_stack_count"] = criticalCount,
|
|
["high_stack_count"] = highCount,
|
|
["test_alarm_success"] = testSuccess,
|
|
["test_alarm_trigger_time_ms"] = testResult.Data?.TriggerElapsedMs ?? 0,
|
|
["status"] = "healthy"
|
|
};
|
|
|
|
// 检查是否有过多活跃报警
|
|
if (activeAlarmCount > 100)
|
|
{
|
|
return HealthCheckResult.Degraded(
|
|
"活跃报警数量过多",
|
|
new Dictionary<string, object>(data)
|
|
{
|
|
["status"] = "too_many_alarms"
|
|
});
|
|
}
|
|
|
|
// 检查是否有严重报警
|
|
if (criticalCount > 0)
|
|
{
|
|
return HealthCheckResult.Degraded(
|
|
"存在严重报警",
|
|
new Dictionary<string, object>(data)
|
|
{
|
|
["status"] = "critical_alarms"
|
|
});
|
|
}
|
|
|
|
// 检查测试报警是否成功
|
|
if (!testSuccess)
|
|
{
|
|
return HealthCheckResult.Unhealthy(
|
|
"报警触发测试失败",
|
|
new Dictionary<string, object>(data)
|
|
{
|
|
["status"] = "test_failed",
|
|
["error"] = testResult.Message
|
|
});
|
|
}
|
|
|
|
_logger.LogDebug("报警系统健康检查通过:活跃报警={ActiveCount},严重报警={CriticalCount},高级报警={HighCount},测试耗时={ElapsedMs}ms",
|
|
activeAlarmCount, criticalCount, highCount, testResult.Data?.TriggerElapsedMs ?? 0);
|
|
|
|
return HealthCheckResult.Healthy("报警系统运行正常", data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "报警系统健康检查异常");
|
|
return HealthCheckResult.Unhealthy(
|
|
"报警系统健康检查异常",
|
|
new Dictionary<string, object>
|
|
{
|
|
["error"] = ex.Message,
|
|
["exception_type"] = ex.GetType().Name
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|