保存
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
#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
|
||||
@@ -0,0 +1,113 @@
|
||||
#if false
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using OrpaonVision.SiteApp.Runtime.Services;
|
||||
|
||||
namespace OrpaonVision.SiteApp.Runtime.HealthChecks;
|
||||
|
||||
/// <summary>
|
||||
/// 规则引擎健康检查。
|
||||
/// </summary>
|
||||
public sealed class RuleEngineHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly IRuleEngineService _ruleEngineService;
|
||||
private readonly ILogger<RuleEngineHealthCheck> _logger;
|
||||
|
||||
public RuleEngineHealthCheck(IRuleEngineService ruleEngineService, ILogger<RuleEngineHealthCheck> logger)
|
||||
{
|
||||
_ruleEngineService = ruleEngineService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查规则引擎服务是否可用
|
||||
var rules = await _ruleEngineService.GetRulesAsync(cancellationToken);
|
||||
|
||||
if (!rules.IsSuccess)
|
||||
{
|
||||
return HealthCheckResult.Unhealthy(
|
||||
"规则引擎服务不可用",
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
["error"] = rules.Message,
|
||||
["trace_id"] = rules.TraceId ?? string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
// 检查是否有规则配置
|
||||
var ruleCount = rules.Data?.Count ?? 0;
|
||||
if (ruleCount == 0)
|
||||
{
|
||||
return HealthCheckResult.Degraded(
|
||||
"规则引擎可用但无规则配置",
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
["rule_count"] = ruleCount,
|
||||
["status"] = "no_rules"
|
||||
});
|
||||
}
|
||||
|
||||
// 执行简单的规则评估测试
|
||||
var testResult = await _ruleEngineService.EvaluateRulesAsync(new Core.RuleEngine.RuleEvaluationRequest
|
||||
{
|
||||
SessionId = Guid.NewGuid(),
|
||||
Inference = new Core.RuleEngine.InferenceResultDto
|
||||
{
|
||||
SessionId = Guid.NewGuid(),
|
||||
Timestamp = DateTime.UtcNow,
|
||||
Detections = new List<Core.RuleEngine.InferenceDetectionDto>
|
||||
{
|
||||
new()
|
||||
{
|
||||
ClassName = "test",
|
||||
Confidence = 0.9f,
|
||||
CenterX = 100,
|
||||
CenterY = 100,
|
||||
Width = 50,
|
||||
Height = 50
|
||||
}
|
||||
}
|
||||
}
|
||||
}, cancellationToken);
|
||||
|
||||
if (!testResult.IsSuccess)
|
||||
{
|
||||
return HealthCheckResult.Unhealthy(
|
||||
"规则引擎评估测试失败",
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
["error"] = testResult.Message,
|
||||
["trace_id"] = testResult.TraceId ?? string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
var data = new Dictionary<string, object>
|
||||
{
|
||||
["rule_count"] = ruleCount,
|
||||
["test_evaluation_time_ms"] = testResult.Data?.EvaluationElapsedMs ?? 0,
|
||||
["test_result_pass"] = testResult.Data?.OverallResult == Core.RuleEngine.RuleResult.Pass,
|
||||
["status"] = "healthy"
|
||||
};
|
||||
|
||||
_logger.LogDebug("规则引擎健康检查通过:规则数量={RuleCount},测试评估耗时={ElapsedMs}ms",
|
||||
ruleCount, testResult.Data?.EvaluationElapsedMs ?? 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
|
||||
@@ -0,0 +1,104 @@
|
||||
#if false
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using OrpaonVision.SiteApp.Runtime.Services;
|
||||
|
||||
namespace OrpaonVision.SiteApp.Runtime.HealthChecks;
|
||||
|
||||
/// <summary>
|
||||
/// 状态机健康检查。
|
||||
/// </summary>
|
||||
public sealed class StateMachineHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly IRuntimeStateMachineService _stateMachineService;
|
||||
private readonly ILogger<StateMachineHealthCheck> _logger;
|
||||
|
||||
public StateMachineHealthCheck(IRuntimeStateMachineService stateMachineService, ILogger<StateMachineHealthCheck> logger)
|
||||
{
|
||||
_stateMachineService = stateMachineService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查状态机当前状态
|
||||
var currentState = _stateMachineService.GetCurrentState();
|
||||
var currentLayer = _stateMachineService.GetCurrentLayer();
|
||||
|
||||
// 检查状态机是否响应
|
||||
var canInitialize = _stateMachineService.CanExecuteOperation(Core.Common.StateTrigger.Initialize);
|
||||
var canStart = _stateMachineService.CanExecuteOperation(Core.Common.StateTrigger.Start);
|
||||
|
||||
// 获取事件历史
|
||||
var historyResult = await _stateMachineService.GetEventHistoryAsync(10, cancellationToken);
|
||||
|
||||
if (!historyResult.IsSuccess)
|
||||
{
|
||||
return HealthCheckResult.Unhealthy(
|
||||
"状态机事件历史获取失败",
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
["error"] = historyResult.Message,
|
||||
["trace_id"] = historyResult.TraceId ?? string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
var eventCount = historyResult.Data?.Count ?? 0;
|
||||
var lastEventTime = eventCount > 0 ? historyResult.Data?.FirstOrDefault()?.TimestampUtc : (DateTime?)null;
|
||||
|
||||
// 判断健康状态
|
||||
var data = new Dictionary<string, object>
|
||||
{
|
||||
["current_state"] = currentState.ToString(),
|
||||
["current_layer"] = currentLayer,
|
||||
["can_initialize"] = canInitialize,
|
||||
["can_start"] = canStart,
|
||||
["event_count"] = eventCount,
|
||||
["last_event_time"] = lastEventTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "无事件",
|
||||
["status"] = "healthy"
|
||||
};
|
||||
|
||||
// 检查是否处于异常状态
|
||||
if (currentState == Core.Common.RuntimeState.Error || currentState == Core.Common.RuntimeState.Faulted)
|
||||
{
|
||||
return HealthCheckResult.Unhealthy(
|
||||
"状态机处于异常状态",
|
||||
new Dictionary<string, object>(data)
|
||||
{
|
||||
["status"] = "error_state"
|
||||
});
|
||||
}
|
||||
|
||||
// 检查是否长时间无活动(超过5分钟)
|
||||
if (lastEventTime.HasValue && DateTime.UtcNow.Subtract(lastEventTime.Value).TotalMinutes > 5)
|
||||
{
|
||||
return HealthCheckResult.Degraded(
|
||||
"状态机长时间无活动",
|
||||
new Dictionary<string, object>(data)
|
||||
{
|
||||
["status"] = "inactive",
|
||||
["inactive_minutes"] = DateTime.UtcNow.Subtract(lastEventTime.Value).TotalMinutes
|
||||
});
|
||||
}
|
||||
|
||||
_logger.LogDebug("状态机健康检查通过:当前状态={CurrentState},当前层级={CurrentLayer},事件数量={EventCount}",
|
||||
currentState, currentLayer, eventCount);
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user