feat(ai): 告警洞察生成逻辑 + 事件消费者增强
- engine.rs 新增 generate_anomaly_insights(过滤 info 级别) - copilot_consumer 在风险评分后自动生成 warning/critical 告警洞察
This commit is contained in:
@@ -58,8 +58,45 @@ async fn process_event(db: &sea_orm::DatabaseConnection, event: &erp_core::event
|
||||
},
|
||||
None => return,
|
||||
};
|
||||
let _ =
|
||||
crate::service::risk_service::RiskService::compute_risk(db, tenant_id, patient_id).await;
|
||||
// 风险评分计算
|
||||
if let Ok(risk) =
|
||||
crate::service::risk_service::RiskService::compute_risk(db, tenant_id, patient_id).await
|
||||
{
|
||||
// 异常检测:告警级规则匹配生成洞察
|
||||
let matched_with_severity: Vec<_> = risk
|
||||
.matched_rules
|
||||
.into_iter()
|
||||
.map(|r| (r.rule_id, r.name, r.score, r.severity, r.suggestion))
|
||||
.collect();
|
||||
let anomaly_insights = crate::copilot::engine::generate_anomaly_insights(
|
||||
&patient_id.to_string(),
|
||||
&matched_with_severity,
|
||||
);
|
||||
for insight_data in anomaly_insights {
|
||||
let severity = insight_data["severity"]
|
||||
.as_str()
|
||||
.unwrap_or("warning")
|
||||
.to_string();
|
||||
let title = insight_data["title"]
|
||||
.as_str()
|
||||
.unwrap_or("异常告警")
|
||||
.to_string();
|
||||
let _ = crate::service::insight_service::InsightService::create_insight(
|
||||
db,
|
||||
tenant_id,
|
||||
patient_id,
|
||||
"anomaly".into(),
|
||||
"rule".into(),
|
||||
Some(severity),
|
||||
title,
|
||||
insight_data,
|
||||
None,
|
||||
168, // 7 天过期
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
let _ = erp_core::events::mark_event_processed(db, event.id, "copilot_consumer").await;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user