feat: alert-mute support edit and disable (#1144)

* batch query prom for single panel

* make code better:

1.extract server/api.go

2.make webapi reading prom with reusing server's API,not a new prom client

* clear code

* clear code

* format code
clear code

* move reader.go,reuse webapi/prom/prom.go clusterTypes clients cache

* clear code,extract common method

* feat: add edit and disabled for alert mute

* fix cr problem

* disabled add default 0
This commit is contained in:
SunnyBoy-WYH 2022-09-05 12:11:50 +08:00 committed by GitHub
parent e4e48cfda0
commit a7bad003f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 1 deletions

View File

@ -269,14 +269,18 @@ CREATE TABLE `alert_mute` (
`id` bigint unsigned not null auto_increment,
`group_id` bigint not null default 0 comment 'busi group id',
`prod` varchar(255) not null default '',
`note` varchar(1024) not null default '',
`cate` varchar(128) not null,
`cluster` varchar(128) not null,
`tags` varchar(4096) not null default '' comment 'json,map,tagkey->regexp|value',
`cause` varchar(255) not null default '',
`btime` bigint not null default 0 comment 'begin time',
`etime` bigint not null default 0 comment 'end time',
`disabled` tinyint(1) not null default 0 comment '0:enabled 1:disabled',
`create_at` bigint not null default 0,
`create_by` varchar(64) not null default '',
`update_at` bigint not null default 0,
`update_by` varchar(64) not null default '',
PRIMARY KEY (`id`),
KEY (`create_at`),
KEY (`group_id`)
@ -357,7 +361,7 @@ CREATE TABLE `recording_rule` (
`cluster` varchar(128) not null,
`name` varchar(255) not null comment 'new metric name',
`note` varchar(255) not null comment 'rule note',
`disabled` tinyint(1) not null comment '0:enabled 1:disabled',
`disabled` tinyint(1) not null default 0 comment '0:enabled 1:disabled',
`prom_ql` varchar(8192) not null comment 'promql',
`prom_eval_interval` int not null comment 'evaluate interval',
`append_tags` varchar(255) default '' comment 'split by space: service=n9e mod=api',

View File

@ -22,6 +22,7 @@ type TagFilter struct {
type AlertMute struct {
Id int64 `json:"id" gorm:"primaryKey"`
GroupId int64 `json:"group_id"`
Note string `json:"note"`
Cate string `json:"cate"`
Prod string `json:"prod"` // product empty means n9e
Cluster string `json:"cluster"` // take effect by clusters, seperated by space
@ -29,8 +30,11 @@ type AlertMute struct {
Cause string `json:"cause"`
Btime int64 `json:"btime"`
Etime int64 `json:"etime"`
Disabled int `json:"disabled"` // 0: enabled, 1: disabled
CreateBy string `json:"create_by"`
UpdateBy string `json:"update_by"`
CreateAt int64 `json:"create_at"`
UpdateAt int64 `json:"update_at"`
ITags []TagFilter `json:"-" gorm:"-"` // inner tags
}
@ -38,6 +42,24 @@ func (m *AlertMute) TableName() string {
return "alert_mute"
}
func AlertMuteGetById(id int64) (*AlertMute, error) {
return AlertMuteGet("id=?", id)
}
func AlertMuteGet(where string, args ...interface{}) (*AlertMute, error) {
var lst []*AlertMute
err := DB().Where(where, args...).Find(&lst).Error
if err != nil {
return nil, err
}
if len(lst) == 0 {
return nil, nil
}
return lst[0], nil
}
func AlertMuteGets(prods []string, bgid int64, query string) (lst []AlertMute, err error) {
session := DB().Where("group_id = ? and prod in (?)", bgid, prods)
@ -118,6 +140,25 @@ func (m *AlertMute) Add() error {
return Insert(m)
}
func (m *AlertMute) Update(arm AlertMute) error {
arm.Id = m.Id
arm.GroupId = m.GroupId
arm.CreateAt = m.CreateAt
arm.CreateBy = m.CreateBy
arm.UpdateAt = time.Now().Unix()
err := arm.Verify()
if err != nil {
return err
}
return DB().Model(m).Select("*").Updates(arm).Error
}
func (m *AlertMute) UpdateFieldsMap(fields map[string]interface{}) error {
return DB().Model(m).Updates(fields).Error
}
func AlertMuteDel(ids []int64) error {
if len(ids) == 0 {
return nil

View File

@ -22,6 +22,10 @@ func IsMuted(event *models.AlertCurEvent, clock ...int64) bool {
}
func matchMute(event *models.AlertCurEvent, mute *models.AlertMute, clock ...int64) bool {
if mute.Disabled == 1 {
return false
}
ts := event.TriggerTime
if len(clock) > 0 {
ts = clock[0]

View File

@ -251,6 +251,8 @@ func configRoute(r *gin.Engine, version string) {
pages.GET("/busi-group/:id/alert-mutes", auth(), user(), perm("/alert-mutes"), bgro(), alertMuteGetsByBG)
pages.POST("/busi-group/:id/alert-mutes", auth(), user(), perm("/alert-mutes/add"), bgrw(), alertMuteAdd)
pages.DELETE("/busi-group/:id/alert-mutes", auth(), user(), perm("/alert-mutes/del"), bgrw(), alertMuteDel)
pages.PUT("/busi-group/:id/alert-mute/:amid", auth(), user(), perm("/alert-mutes/put"), alertMutePutByFE)
pages.PUT("/busi-group/:id/alert-mutes/fields", auth(), user(), perm("/alert-mutes/put"), bgrw(), alertMutePutFields)
pages.GET("/busi-group/:id/alert-subscribes", auth(), user(), perm("/alert-subscribes"), bgro(), alertSubscribeGets)
pages.GET("/alert-subscribe/:sid", auth(), user(), perm("/alert-subscribes"), alertSubscribeGet)

View File

@ -1,7 +1,9 @@
package router
import (
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
@ -50,3 +52,52 @@ func alertMuteDel(c *gin.Context) {
ginx.NewRender(c).Message(models.AlertMuteDel(f.Ids))
}
func alertMutePutByFE(c *gin.Context) {
var f models.AlertMute
ginx.BindJSON(c, &f)
amid := ginx.UrlParamInt64(c, "amid")
am, err := models.AlertMuteGetById(amid)
ginx.Dangerous(err)
if am == nil {
ginx.NewRender(c, http.StatusNotFound).Message("No such AlertMute")
return
}
bgrwCheck(c, am.GroupId)
f.UpdateBy = c.MustGet("username").(string)
ginx.NewRender(c).Message(am.Update(f))
}
type alertMuteFieldForm struct {
Ids []int64 `json:"ids"`
Fields map[string]interface{} `json:"fields"`
}
func alertMutePutFields(c *gin.Context) {
var f alertMuteFieldForm
ginx.BindJSON(c, &f)
if len(f.Fields) == 0 {
ginx.Bomb(http.StatusBadRequest, "fields empty")
}
f.Fields["update_by"] = c.MustGet("username").(string)
f.Fields["update_at"] = time.Now().Unix()
for i := 0; i < len(f.Ids); i++ {
am, err := models.AlertMuteGetById(f.Ids[i])
ginx.Dangerous(err)
if am == nil {
continue
}
ginx.Dangerous(am.UpdateFieldsMap(f.Fields))
}
ginx.NewRender(c).Message(nil)
}