保存
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user