feat: implement V2 alert center and scoped alert governance

This commit is contained in:
Yvan
2026-11-05 20:08:29 +08:00
parent f8e26c5baf
commit 999c9a6315
57 changed files with 2711 additions and 216 deletions
+58 -6
View File
@@ -40,6 +40,7 @@ func TestAlertRecordHTTPCompleteWorkflowAndVisibility(t *testing.T) {
"INSERT INTO tab_promote VALUES(100,'lx',14,1,0),(100,'hw',14,1,1),(100,'yq',14,1,1)",
"INSERT INTO sys_user_bind_platform(user_id,source_db,platform_busier_id,status) VALUES(9,'lx',14,'0'),(10,'hw',14,'0'),(99,'yq',14,'0')",
"INSERT INTO sys_alert_record(id,platform_code,target_promote_id,business_id,rule_name,status,created_at,updated_at) VALUES(9007199254740993,'lx',100,7,'目标规则',1,'2026-11-03 08:00:00','2026-11-03 08:00:00'),(2,'hw',100,7,'平级规则',1,'2026-11-03 08:00:00','2026-11-03 08:00:00'),(3,'lx',100,99,'归属错误',1,'2026-11-03 08:00:00','2026-11-03 08:00:00'),(4,'yq',100,99,'其他规则',1,'2026-11-03 08:00:00','2026-11-03 08:00:00'),(5,'lx',100,7,'关闭规则',5,'2026-11-03 08:00:00','2026-11-03 08:00:00'),(6,'lx',100,7,'待审规则',3,'2026-11-03 08:00:00','2026-11-03 08:00:00')",
"ALTER TABLE tab_promote ADD COLUMN account TEXT DEFAULT '测试渠道'",
"INSERT INTO media_file_upload_and_downloads(id,url) VALUES(1,'/files/one.png'),(2,'/files/two.png')",
} {
if err := db.Exec(sql).Error; err != nil {
@@ -60,6 +61,7 @@ func TestAlertRecordHTTPCompleteWorkflowAndVisibility(t *testing.T) {
c.Next()
})
router.RegisterAlertRecord(engine.Group(""), handler.NewAlertRecord(service))
router.RegisterAlertGovernance(engine.Group(""), handler.NewAlertGovernance(businessservice.NewAlertGovernanceService(businessbiz.NewAlertGovernanceUsecase(businessdata.NewAlertGovernanceRepo(provider), accounts))))
server := httptest.NewServer(engine)
defer server.Close()
call := func(user int, method, path, body string, expected int) map[string]any {
@@ -94,6 +96,10 @@ func TestAlertRecordHTTPCompleteWorkflowAndVisibility(t *testing.T) {
}
list(1, "", 6)
list(7, "", 4)
platforms := call(8, "GET", "/sw/alertRecord/platforms", "", 0)["data"].([]any)
if len(platforms) != 1 || platforms[0] != "lx" {
t.Fatal("平台下拉必须按当前可见绑定过滤")
}
list(8, "", 3)
list(9, "", 3)
list(7, "?businessId=8", 3)
@@ -101,8 +107,8 @@ func TestAlertRecordHTTPCompleteWorkflowAndVisibility(t *testing.T) {
list(1, "?businessId=8", 4)
list(8, "?platformCodes=hw", 0)
data := list(8, "?status=1&ruleName=%E7%9B%AE%E6%A0%87", 1)
if len(data["statusCount"].(map[string]any)) != 3 {
t.Fatal("汇总错误继承列表筛选")
if len(data["statusCount"].(map[string]any)) != 1 {
t.Fatal("汇总应继承名称筛选")
}
const id = "9007199254740993"
detailPath := "/sw/alertRecord/detail/" + id
@@ -139,15 +145,61 @@ func TestAlertRecordHTTPCompleteWorkflowAndVisibility(t *testing.T) {
t.Fatalf("完整处理审核日志错误: %+v", data)
}
call(9, "POST", "/sw/alertRecord/addNote", `{"alertId":"`+id+`","noteContent":" ","attachmentIds":[1]}`, 7)
call(9, "POST", "/sw/alertRecord/addNote", `{"alertId":"`+id+`","noteContent":"关闭后补充","attachmentIds":[2,"1"]}`, 0)
data = call(9, "GET", detailPath, "", 0)["data"].(map[string]any)
call(9, "POST", "/sw/alertRecord/addNote", `{"alertId":"`+id+`","noteContent":"关闭后补充","attachmentIds":[2,"1"]}`, 7)
call(9, "POST", "/sw/alertRecord/addNote", `{"alertId":"6","noteContent":"待审核补充","attachmentIds":[2,"1"]}`, 0)
data = call(9, "GET", "/sw/alertRecord/detail/6", "", 0)["data"].(map[string]any)
logs := data["operationLogs"].([]any)
latest := logs[0].(map[string]any)
if len(logs) != 9 || latest["operatorName"] != "leaf" || latest["logType"] != float64(2) || latest["attachments"].([]any)[0] != "/files/two.png" || data["alertRecord"].(map[string]any)["status"] != float64(5) {
t.Fatalf("备注附件或状态错误: %+v", data)
if len(logs) != 1 || latest["attachments"].([]any)[0] != "/files/two.png" || data["alertRecord"].(map[string]any)["status"] != float64(3) {
t.Fatalf("独立备注或附件错误: %+v", data)
}
call(9, "POST", "/sw/alertRecord/ignore", `{"ids":["6"]}`, 7)
call(7, "POST", "/sw/alertRecord/ignore", `{"ids":["6","4"]}`, 7)
data = call(7, "GET", "/sw/alertRecord/detail/6", "", 0)["data"].(map[string]any)
if data["alertRecord"].(map[string]any)["status"] != float64(3) {
t.Fatal("混入越权 ID 时不能部分忽略")
}
call(7, "POST", "/sw/alertRecord/ignore", `{"ids":["6","6"]}`, 0)
call(7, "POST", "/sw/alertRecord/ignore", `{"ids":["6"]}`, 0)
data = call(7, "GET", "/sw/alertRecord/detail/6", "", 0)["data"].(map[string]any)
if data["alertRecord"].(map[string]any)["status"] != float64(6) || len(data["operationLogs"].([]any)) != 2 {
t.Fatal("忽略必须幂等留痕")
}
call(99, "POST", "/sw/alertRecord/addNote", `{"alertId":"`+id+`","noteContent":"越权"}`, 7)
call(1, "POST", "/sw/alertRecord/startProcess", `{"alertId":0}`, 7)
call(9, "PUT", "/sw/alertRule/gap", `{"enabled":true,"days":7}`, 7)
call(7, "PUT", "/sw/alertRule/gap", `{"enabled":true,"days":0}`, 7)
call(7, "PUT", "/sw/alertRule/gap", `{"enabled":true,"days":1000}`, 0)
gap := call(9, "GET", "/sw/alertRule/gap", "", 0)["data"].(map[string]any)
if gap["days"] != float64(1000) || gap["enabled"] != true {
t.Fatal("下级应看到其一级商务设置的统一间隔")
}
call(9, "GET", "/sw/alertExemption/candidates?keyword=test", "", 7)
choices := call(7, "GET", "/sw/alertExemption/candidates", "", 0)["data"].([]any)
if len(choices) != 2 {
t.Fatalf("候选渠道越权或丢失: %+v", choices)
}
for _, choice := range choices {
if _, ok := choice.(map[string]any)["id"].(string); !ok {
t.Fatal("渠道 ID 应保持字符串精度")
}
}
call(9, "POST", "/sw/alertExemption/add", `{"sourceDb":"lx","promoteId":"100","reason":"测试"}`, 7)
call(7, "POST", "/sw/alertExemption/add", `{"sourceDb":"yq","promoteId":"100","reason":"越权"}`, 7)
call(7, "POST", "/sw/alertExemption/add", `{"sourceDb":"lx","promoteId":"100","reason":"测试免预警"}`, 0)
call(7, "POST", "/sw/alertExemption/add", `{"sourceDb":"lx","promoteId":"100","reason":"重复"}`, 7)
call(9, "GET", "/sw/alertExemption/list", "", 7)
ex := call(7, "GET", "/sw/alertExemption/list?active=true", "", 0)["data"].(map[string]any)
entry := ex["rows"].([]any)[0].(map[string]any)
eid := entry["id"].(string)
call(99, "PUT", "/sw/alertExemption/remove/"+eid, "", 7)
call(7, "PUT", "/sw/alertExemption/remove/"+eid, "", 0)
ex = call(7, "GET", "/sw/alertExemption/list?active=false", "", 0)["data"].(map[string]any)
entry = ex["rows"].([]any)[0].(map[string]any)
if entry["removedName"] != "owner" || entry["removeReason"] != "手动移出" || entry["reason"] != "测试免预警" {
t.Fatal("移出后应保留完整审计记录")
}
if err := db.Exec("DROP TABLE tab_promote").Error; err != nil {
t.Fatal(err)
}