11package api
22
33import (
4+ "fmt"
45 "net/http"
56
67 "github.com/fox-gonic/fox"
@@ -10,6 +11,8 @@ import (
1011// setupAlertRouters 设置告警相关路由
1112func (api * Api ) setupAlertRouters (router * fox.Engine ) {
1213 router .POST ("/v1/alert-rules/sync" , api .SyncRules )
14+ router .PUT ("/v1/alert-rules/:rule_name" , api .UpdateRule )
15+ router .PUT ("/v1/alert-rules/meta" , api .UpdateRuleMeta )
1316}
1417
1518// SyncRules 同步规则到Prometheus
@@ -34,3 +37,86 @@ func (api *Api) SyncRules(c *fox.Context) {
3437 "message" : "Rules synced to Prometheus" ,
3538 })
3639}
40+
41+ // UpdateRule 更新单个规则模板
42+ // 只更新指定的规则,系统会自动查找所有使用该规则的元信息并重新生成
43+ func (api * Api ) UpdateRule (c * fox.Context ) {
44+ ruleName := c .Param ("rule_name" )
45+ if ruleName == "" {
46+ SendErrorResponse (c , http .StatusBadRequest , model .ErrorCodeInvalidParameter ,
47+ "Rule name is required" , nil )
48+ return
49+ }
50+
51+ var req model.UpdateAlertRuleRequest
52+ if err := c .ShouldBindJSON (& req ); err != nil {
53+ SendErrorResponse (c , http .StatusBadRequest , model .ErrorCodeInvalidParameter ,
54+ "Invalid request body: " + err .Error (), nil )
55+ return
56+ }
57+
58+ // 构建完整的规则对象
59+ rule := model.AlertRule {
60+ Name : ruleName ,
61+ Description : req .Description ,
62+ Expr : req .Expr ,
63+ Op : req .Op ,
64+ Severity : req .Severity ,
65+ }
66+
67+ err := api .alertService .UpdateRule (rule )
68+ if err != nil {
69+ SendErrorResponse (c , http .StatusInternalServerError , model .ErrorCodeInternalError ,
70+ "Failed to update rule: " + err .Error (), nil )
71+ return
72+ }
73+
74+ // 获取受影响的元信息数量
75+ affectedCount := api .alertService .GetAffectedMetas (ruleName )
76+
77+ c .JSON (http .StatusOK , map [string ]interface {}{
78+ "status" : "success" ,
79+ "message" : fmt .Sprintf ("Rule '%s' updated and synced to Prometheus" , ruleName ),
80+ "affected_metas" : affectedCount ,
81+ })
82+ }
83+
84+ // UpdateRuleMeta 更新单个规则元信息
85+ // 通过 alert_name + labels 唯一确定一个元信息记录
86+ func (api * Api ) UpdateRuleMeta (c * fox.Context ) {
87+ var req model.UpdateAlertRuleMetaRequest
88+ if err := c .ShouldBindJSON (& req ); err != nil {
89+ SendErrorResponse (c , http .StatusBadRequest , model .ErrorCodeInvalidParameter ,
90+ "Invalid request body: " + err .Error (), nil )
91+ return
92+ }
93+
94+ // alert_name 和 labels 是必填的
95+ if req .AlertName == "" || req .Labels == "" {
96+ SendErrorResponse (c , http .StatusBadRequest , model .ErrorCodeInvalidParameter ,
97+ "alert_name and labels are required" , nil )
98+ return
99+ }
100+
101+ // 构建完整的元信息对象
102+ meta := model.AlertRuleMeta {
103+ AlertName : req .AlertName ,
104+ Labels : req .Labels ,
105+ Threshold : req .Threshold ,
106+ WatchTime : req .WatchTime ,
107+ }
108+
109+ err := api .alertService .UpdateRuleMeta (meta )
110+ if err != nil {
111+ SendErrorResponse (c , http .StatusInternalServerError , model .ErrorCodeInternalError ,
112+ "Failed to update rule meta: " + err .Error (), nil )
113+ return
114+ }
115+
116+ c .JSON (http .StatusOK , map [string ]interface {}{
117+ "status" : "success" ,
118+ "message" : "Rule meta updated and synced to Prometheus" ,
119+ "alert_name" : req .AlertName ,
120+ "labels" : req .Labels ,
121+ })
122+ }
0 commit comments