feat: support to deal with alert_event (#777)

This commit is contained in:
Istil 2021-08-23 16:39:07 +08:00 committed by GitHub
parent 12e3b3a490
commit 754ad160dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 17 deletions

View File

@ -159,6 +159,7 @@ func configRoutes(r *gin.Engine) {
pages.DELETE("/alert-events", login(), alertEventsDel)
pages.GET("/alert-event/:id", login(), alertEventGet)
pages.DELETE("/alert-event/:id", login(), alertEventDel)
pages.PUT("/alert-event/:id", login(), alertEventNotePut)
pages.GET("/history-alert-events", login(), historyAlertEventGets)
pages.GET("/history-alert-event/:id", login(), historyAlertEventGet)

View File

@ -1,6 +1,7 @@
package http
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
@ -54,6 +55,23 @@ func alertEventGet(c *gin.Context) {
renderData(c, ae, nil)
}
type alertEventNoteForm struct {
EventNote string `json:"event_note"`
}
func alertEventNotePut(c *gin.Context) {
var f alertEventNoteForm
bind(c, &f)
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))
}
func alertEventDel(c *gin.Context) {
loginUser(c).MustPerm("alert_event_delete")
renderMessage(c, AlertEvent(urlParamInt64(c, "id")).Del())

View File

@ -17,6 +17,9 @@ type AlertEvent struct {
RuleId int64 `json:"rule_id"`
RuleName string `json:"rule_name"`
RuleNote string `json:"rule_note"`
ProcessorUid int64 `json:"processor_uid"`
ProcessorObjs 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小时数据
LastSend bool `json:"last_sent" xorm:"-"` // true 代表上次发了false代表还没发:给prometheus做for判断的
@ -114,6 +117,14 @@ func (ae *AlertEvent) FillObjs() error {
ae.NotifyUserObjs = users
}
if ae.ProcessorUid != 0 {
processor, err := UserGetById(ae.ProcessorUid)
if err != nil {
return err
}
ae.ProcessorObjs = *processor
}
return nil
}
@ -245,6 +256,7 @@ func AlertEventGets(stime, etime int64, query string, status, priority int, limi
func AlertEventGet(where string, args ...interface{}) (*AlertEvent, error) {
var obj AlertEvent
has, err := DB.Where(where, args...).Get(&obj)
if err != nil {
logger.Errorf("mysql.error: query alert_event(%s)%+v fail: %s", where, args, err)
return nil, internalServerError
@ -256,3 +268,24 @@ func AlertEventGet(where string, args ...interface{}) (*AlertEvent, error) {
return &obj, nil
}
func AlertEventUpdateEventNote(id int64, hashId string, note string, uid int64) error {
session := DB.NewSession()
defer session.Close()
if err := session.Begin(); err != nil {
return err
}
if _, err := session.Exec("UPDATE alert_event SET event_note = ?, processor_uid = ? WHERE id = ?", note, uid, id); err != nil {
logger.Errorf("mysql.error: update alert_event event_note fail: %v", err)
return err
}
if _, err := session.Exec("UPDATE history_alert_event SET event_note = ?, processor_uid = ? WHERE hash_id = ?", note, uid, hashId); err != nil {
logger.Errorf("mysql.error: update history_alert_event event_note fail: %v", err)
return err
}
return session.Commit()
}

View File

@ -15,6 +15,9 @@ type HistoryAlertEvent struct {
RuleId int64 `json:"rule_id"`
RuleName string `json:"rule_name"`
RuleNote string `json:"rule_note"`
ProcessorUid int64 `json:"processor_uid"`
ProcessorObjs 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小时数据
ResClasspaths string `json:"res_classpaths"`
@ -79,6 +82,14 @@ func (hae *HistoryAlertEvent) FillObjs() error {
hae.NotifyUserObjs = users
}
if hae.ProcessorUid != 0 {
processor, err := UserGetById(hae.ProcessorUid)
if err != nil {
return err
}
hae.ProcessorObjs = *processor
}
return nil
}

View File

@ -99,6 +99,7 @@ insert into `role_operation`(role_name, operation) values('Standard', 'alert_rul
insert into `role_operation`(role_name, operation) values('Standard', 'alert_rule_modify');
insert into `role_operation`(role_name, operation) values('Standard', 'alert_rule_delete');
insert into `role_operation`(role_name, operation) values('Standard', 'alert_event_delete');
insert into `role_operation`(role_name, operation) values('Standard', 'alert_event_modify');
insert into `role_operation`(role_name, operation) values('Standard', 'collect_rule_create');
insert into `role_operation`(role_name, operation) values('Standard', 'collect_rule_modify');
insert into `role_operation`(role_name, operation) values('Standard', 'collect_rule_delete');
@ -295,6 +296,8 @@ CREATE TABLE `alert_event` (
`rule_id` bigint unsigned not null,
`rule_name` varchar(255) not null,
`rule_note` varchar(512) not null default 'alert rule note',
`processor_uid` bigint NOT NULL default 0,
`event_note` varchar(512) not null default 'alert event note',
`res_classpaths` varchar(1024) not null default '' comment 'belong classpaths',
`priority` tinyint(1) not null,
`status` tinyint(1) not null,
@ -314,24 +317,26 @@ CREATE TABLE `alert_event` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `history_alert_event` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`hash_id` varchar(255) NOT NULL COMMENT 'rule_id + point_pk',
`rule_id` bigint unsigned NOT NULL,
`rule_name` varchar(255) NOT NULL,
`rule_note` varchar(512) NOT NULL DEFAULT 'alert rule note',
`res_classpaths` varchar(1024) NOT NULL DEFAULT '' COMMENT 'belong classpaths',
`priority` tinyint(1) NOT NULL,
`status` tinyint(1) NOT NULL,
`is_prome_pull` tinyint(1) NOT NULL,
`is_recovery` tinyint(1) NOT NULL,
`id` bigint unsigned not null AUTO_INCREMENT,
`hash_id` varchar(255) not null COMMENT 'rule_id + point_pk',
`rule_id` bigint unsigned not null,
`rule_name` varchar(255) not null,
`rule_note` varchar(512) not null default 'alert rule note',
`processor_uid` bigint not null default 0,
`event_note` varchar(512) not null default 'alert event note',
`res_classpaths` varchar(1024) not null default '' COMMENT 'belong classpaths',
`priority` tinyint(1) not null,
`status` tinyint(1) not null,
`is_prome_pull` tinyint(1) not null,
`is_recovery` tinyint(1) not null,
`history_points` text COMMENT 'metric, history points',
`trigger_time` bigint NOT NULL,
`notify_channels` varchar(255) NOT NULL DEFAULT '',
`notify_groups` varchar(255) NOT NULL DEFAULT '',
`notify_users` varchar(255) NOT NULL DEFAULT '',
`runbook_url` varchar(255) DEFAULT NULL,
`readable_expression` varchar(1024) NOT NULL COMMENT 'e.g. mem.bytes.used.percent(all,60s) > 0',
`tags` varchar(1024) NOT NULL DEFAULT 'merge data_tags rule_tags and res_tags',
`trigger_time` bigint not null,
`notify_channels` varchar(255) not null default '',
`notify_groups` varchar(255) not null default '',
`notify_users` varchar(255) not null default '',
`runbook_url` varchar(255) default NULL,
`readable_expression` varchar(1024) not null COMMENT 'e.g. mem.bytes.used.percent(all,60s) > 0',
`tags` varchar(1024) not null default 'merge data_tags rule_tags and res_tags',
PRIMARY KEY (`id`),
KEY `hash_id` (`hash_id`),
KEY `rule_id` (`rule_id`),