refactor: event process

This commit is contained in:
710leo 2021-08-23 18:35:54 +08:00
parent 8e5b72c833
commit 25cad60ef8
4 changed files with 11 additions and 9 deletions

View File

@ -1,7 +1,6 @@
package http
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
@ -65,9 +64,6 @@ func alertEventNotePut(c *gin.Context) {
me := loginUser(c).MustPerm("alert_event_modify")
ae := AlertEvent(urlParamInt64(c, "id"))
if len(f.EventNote) == 0 {
bomb(http.StatusBadRequest, "event note is empty")
}
renderMessage(c, models.AlertEventUpdateEventNote(ae.Id, ae.HashId, f.EventNote, me.Id))
}

View File

@ -527,6 +527,12 @@ func sendEventIfNeed(status []bool, event *models.AlertEvent, stra *models.Alert
func SendEvent(event *models.AlertEvent) {
// update last event
LastEvents.Set(event)
ae, err := models.AlertEventGet("hash_id = ?", event.HashId)
if err == nil && ae != nil {
logger.Debugf("[event exists do not send again][type:%+v][event:%+v]", event.IsPromePull, event)
return
}
ok := EventQueue.PushFront(event)
if !ok {
logger.Errorf("push event:%v err", event)

View File

@ -18,7 +18,7 @@ type AlertEvent struct {
RuleName string `json:"rule_name"`
RuleNote string `json:"rule_note"`
ProcessorUid int64 `json:"processor_uid"`
ProcessorObjs User `json:"processor_user_objs" xorm:"-"`
ProcessorObj User `json:"processor_user_obj" xorm:"-"`
EventNote string `json:"event_note"`
HashId string `json:"hash_id"` // 唯一标识
IsPromePull int `json:"is_prome_pull"` // 代表是否是prometheus pull告警为1时前端使用 ReadableExpression 拉取最近1小时数据
@ -122,7 +122,7 @@ func (ae *AlertEvent) FillObjs() error {
if err != nil {
return err
}
ae.ProcessorObjs = *processor
ae.ProcessorObj = *processor
}
return nil
@ -282,7 +282,7 @@ func AlertEventUpdateEventNote(id int64, hashId string, note string, uid int64)
return err
}
if _, err := session.Exec("UPDATE history_alert_event SET event_note = ?, processor_uid = ? WHERE hash_id = ?", note, uid, hashId); err != nil {
if _, err := session.Exec("UPDATE history_alert_event SET event_note = ?, processor_uid = ? WHERE hash_id = ? ORDER BY id DESC LIMIT 1", note, uid, hashId); err != nil {
logger.Errorf("mysql.error: update history_alert_event event_note fail: %v", err)
return err
}

View File

@ -16,7 +16,7 @@ type HistoryAlertEvent struct {
RuleName string `json:"rule_name"`
RuleNote string `json:"rule_note"`
ProcessorUid int64 `json:"processor_uid"`
ProcessorObjs User `json:"processor_user_objs" xorm:"-"`
ProcessorObj User `json:"processor_user_objs" xorm:"-"`
EventNote string `json:"event_note"`
HashId string `json:"hash_id"` // 唯一标识
IsPromePull int `json:"is_prome_pull"` // 代表是否是prometheus pull告警为1时前端使用 ReadableExpression 拉取最近1小时数据
@ -87,7 +87,7 @@ func (hae *HistoryAlertEvent) FillObjs() error {
if err != nil {
return err
}
hae.ProcessorObjs = *processor
hae.ProcessorObj = *processor
}
return nil