From 4764cc2419aa23efa48088b565652e50fdc35db0 Mon Sep 17 00:00:00 2001 From: Yening Qin <710leo@gmail.com> Date: Fri, 2 Aug 2024 13:05:11 +0800 Subject: [PATCH] feat: alert rule batch update support annotations (#2074) * feat: batch update annotation (#2072) * fix: annotations_del --------- Co-authored-by: Xu Bin <140785332+Reditiny@users.noreply.github.com> --- center/router/router_alert_rule.go | 27 +++++++++++++++++++++++++++ models/alert_rule.go | 13 +++++++++++++ 2 files changed, 40 insertions(+) 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 }