Skip to content

Commit

Permalink
feat: alert rule batch update support annotations (#2074)
Browse files Browse the repository at this point in the history
* feat: batch update annotation (#2072)

* fix: annotations_del

---------

Co-authored-by: Xu Bin <[email protected]>
  • Loading branch information
710leo and Reditiny authored Aug 2, 2024
1 parent da66401 commit 4764cc2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
27 changes: 27 additions & 0 deletions center/router/router_alert_rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions models/alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 4764cc2

Please sign in to comment.