update alerting ignore api

This commit is contained in:
liugq 2022-05-25 11:19:56 +08:00
parent 3ce0621ef7
commit 61f025be9f
1 changed files with 29 additions and 5 deletions

View File

@ -8,8 +8,10 @@ import (
"fmt" "fmt"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
"infini.sh/console/model/alerting" "infini.sh/console/model/alerting"
alerting2 "infini.sh/console/service/alerting"
httprouter "infini.sh/framework/core/api/router" httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/kv"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
"net/http" "net/http"
@ -20,7 +22,7 @@ import (
func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
body := struct { body := struct {
MessageIDs []string `json:"ids"` Messages []alerting.AlertMessage `json:"messages"`
}{} }{}
err := h.DecodeJSON(req, &body) err := h.DecodeJSON(req, &body)
if err != nil { if err != nil {
@ -28,14 +30,31 @@ func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request,
return return
} }
if len(body.MessageIDs) == 0 { if len(body.Messages) == 0 {
h.WriteError(w, "alert ids should not be empty", http.StatusInternalServerError) h.WriteError(w, "alert ids should not be empty", http.StatusInternalServerError)
return return
} }
messageIDs := make([]string, 0, len(body.Messages))
for _, msg := range body.Messages {
messageIDs = append(messageIDs, msg.ID)
}
queryDsl := util.MapStr{ queryDsl := util.MapStr{
"query": util.MapStr{ "query": util.MapStr{
"bool": util.MapStr{
"must": []util.MapStr{
{
"terms": util.MapStr{ "terms": util.MapStr{
"_id": body.MessageIDs, "_id": messageIDs,
},
},
{
"term": util.MapStr{
"status": util.MapStr{
"value": alerting.MessageStateAlerting,
},
},
},
},
}, },
}, },
"script": util.MapStr{ "script": util.MapStr{
@ -48,9 +67,14 @@ func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request,
log.Error(err) log.Error(err)
return return
} }
//delete kv cache
for _, msg := range body.Messages {
_ = kv.DeleteKey(alerting2.KVLastMessageState, []byte(msg.RuleID))
}
h.WriteJSON(w, util.MapStr{ h.WriteJSON(w, util.MapStr{
"ids": body.MessageIDs, "ids": messageIDs,
"result": "updated", "result": "updated",
}, 200) }, 200)
} }