feat(plugin): P1-P4 审计修复 — 第二批 (运行时监控 + 通知引擎 + 编号reset)
2.1 运行时监控:
- LoadedPlugin 新增 RuntimeMetrics (调用次数/错误/响应时间/燃料消耗)
- execute_wasm 自动采集每次调用的耗时和状态
- GET /admin/plugins/{id}/metrics 端点
2.2 通知规则引擎:
- notification.rs: 订阅 plugin.trigger.* 事件
- 触发时自动给管理员发送消息通知
- emit_trigger_events 增加 manifest_id 到 payload
2.3 编号 reset_rule:
- 替换 PostgreSQL SEQUENCE 为表行 + pg_advisory_xact_lock
- 支持 daily/monthly/yearly/never 重置周期
- 每个周期独立计数,切换时自动重置为 1
This commit is contained in:
@@ -47,6 +47,7 @@ async fn emit_trigger_events(
|
||||
data: Option<&serde_json::Value>,
|
||||
event_bus: &EventBus,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
manifest_id: &str,
|
||||
) {
|
||||
use crate::manifest::PluginTriggerOn;
|
||||
for trigger in triggers {
|
||||
@@ -62,13 +63,25 @@ async fn emit_trigger_events(
|
||||
"entity": entity_name,
|
||||
"record_id": record_id,
|
||||
"data": data,
|
||||
"plugin_id": manifest_id,
|
||||
"trigger_name": trigger.name,
|
||||
"action": action,
|
||||
});
|
||||
// 发布原始触发事件
|
||||
let event = erp_core::events::DomainEvent::new(
|
||||
&trigger.name,
|
||||
tenant_id,
|
||||
payload,
|
||||
payload.clone(),
|
||||
);
|
||||
event_bus.publish(event, db).await;
|
||||
|
||||
// 同时发布 plugin.trigger.{manifest_id} 事件用于通知引擎
|
||||
let notify_event = erp_core::events::DomainEvent::new(
|
||||
format!("plugin.trigger.{}.{}", manifest_id, trigger.name),
|
||||
tenant_id,
|
||||
payload,
|
||||
);
|
||||
event_bus.publish(notify_event, db).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,7 +142,9 @@ impl PluginDataService {
|
||||
|
||||
// 触发事件发布
|
||||
if let Ok(triggers) = find_trigger_events(plugin_id, entity_name, db).await {
|
||||
emit_trigger_events(&triggers, "create", entity_name, &result.id.to_string(), tenant_id, Some(&result.data), _event_bus, db).await;
|
||||
if let Ok(mid) = resolve_manifest_id(plugin_id, tenant_id, db).await {
|
||||
emit_trigger_events(&triggers, "create", entity_name, &result.id.to_string(), tenant_id, Some(&result.data), _event_bus, db, &mid).await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PluginDataResp {
|
||||
@@ -345,7 +360,9 @@ impl PluginDataService {
|
||||
|
||||
// 触发事件发布
|
||||
if let Ok(triggers) = find_trigger_events(plugin_id, entity_name, db).await {
|
||||
emit_trigger_events(&triggers, "update", entity_name, &result.id.to_string(), tenant_id, Some(&result.data), _event_bus, db).await;
|
||||
if let Ok(mid) = resolve_manifest_id(plugin_id, tenant_id, db).await {
|
||||
emit_trigger_events(&triggers, "update", entity_name, &result.id.to_string(), tenant_id, Some(&result.data), _event_bus, db, &mid).await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PluginDataResp {
|
||||
@@ -499,7 +516,9 @@ impl PluginDataService {
|
||||
|
||||
// 触发事件发布
|
||||
if let Ok(triggers) = find_trigger_events(plugin_id, entity_name, db).await {
|
||||
emit_trigger_events(&triggers, "delete", entity_name, &id.to_string(), tenant_id, None, _event_bus, db).await;
|
||||
if let Ok(mid) = resolve_manifest_id(plugin_id, tenant_id, db).await {
|
||||
emit_trigger_events(&triggers, "delete", entity_name, &id.to_string(), tenant_id, None, _event_bus, db, &mid).await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -693,11 +712,13 @@ impl PluginDataService {
|
||||
.await;
|
||||
|
||||
if let Ok(triggers) = find_trigger_events(plugin_id, entity_name, db).await {
|
||||
emit_trigger_events(
|
||||
&triggers, "create", entity_name,
|
||||
&format!("batch_import:{}", success_count),
|
||||
tenant_id, None, event_bus, db,
|
||||
).await;
|
||||
if let Ok(mid) = resolve_manifest_id(plugin_id, tenant_id, db).await {
|
||||
emit_trigger_events(
|
||||
&triggers, "create", entity_name,
|
||||
&format!("batch_import:{}", success_count),
|
||||
tenant_id, None, event_bus, db, &mid,
|
||||
).await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ImportResult {
|
||||
|
||||
Reference in New Issue
Block a user