diff --git a/center/router/router_alert_rule.go b/center/router/router_alert_rule.go index c306c9d16..5c9a73cf9 100644 --- a/center/router/router_alert_rule.go +++ b/center/router/router_alert_rule.go @@ -1,6 +1,7 @@ package router import ( + "encoding/json" "fmt" "net/http" "strconv" @@ -275,6 +276,32 @@ func (rt *Router) alertRulePutFields(c *gin.Context) { continue } + if f.Action == "annotations_add" { + if annotations, has := f.Fields["annotations"]; has { + annotationsMap := annotations.(map[string]interface{}) + for k, v := range annotationsMap { + ar.AnnotationsJSON[k] = v.(string) + } + b, err := json.Marshal(ar.AnnotationsJSON) + ginx.Dangerous(err) + ginx.Dangerous(ar.UpdateFieldsMap(rt.Ctx, map[string]interface{}{"annotations": string(b)})) + continue + } + } + + if f.Action == "annotations_del" { + if annotations, has := f.Fields["annotations"]; has { + annotationsKeys := annotations.(map[string]interface{}) + for key := range annotationsKeys { + delete(ar.AnnotationsJSON, key) + } + b, err := json.Marshal(ar.AnnotationsJSON) + ginx.Dangerous(err) + ginx.Dangerous(ar.UpdateFieldsMap(rt.Ctx, map[string]interface{}{"annotations": string(b)})) + continue + } + } + if f.Action == "callback_add" { // 增加一个 callback 地址 if callbacks, has := f.Fields["callbacks"]; has { diff --git a/models/alert_rule.go b/models/alert_rule.go index a4be07579..523fc68df 100644 --- a/models/alert_rule.go +++ b/models/alert_rule.go @@ -422,6 +422,19 @@ func (ar *AlertRule) UpdateColumn(ctx *ctx.Context, column string, value interfa return DB(ctx).Model(ar).UpdateColumn("annotations", string(b)).Error } + if column == "annotations" { + newAnnotations := value.(map[string]interface{}) + ar.AnnotationsJSON = make(map[string]string) + for k, v := range newAnnotations { + ar.AnnotationsJSON[k] = v.(string) + } + b, err := json.Marshal(ar.AnnotationsJSON) + if err != nil { + return err + } + return DB(ctx).Model(ar).UpdateColumn("annotations", string(b)).Error + } + return DB(ctx).Model(ar).UpdateColumn(column, value).Error }