refactor: service api

This commit is contained in:
ning 2022-06-02 11:07:31 +08:00
parent f6591e80ea
commit 1dbfcd3dc8
6 changed files with 26 additions and 14 deletions

View File

@ -233,7 +233,7 @@ CREATE TABLE `alert_rule` (
`severity` tinyint(1) not null comment '1:Emergency 2:Warning 3:Notice',
`disabled` tinyint(1) not null comment '0:enabled 1:disabled',
`prom_for_duration` int not null comment 'prometheus for, unit:s',
`prom_ql` varchar(8192) not null comment 'promql',
`prom_ql` text not null comment 'promql',
`prom_eval_interval` int not null comment 'evaluate interval',
`enable_stime` char(5) not null default '00:00',
`enable_etime` char(5) not null default '23:59',
@ -259,6 +259,7 @@ CREATE TABLE `alert_rule` (
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 '',
`cluster` varchar(128) not null,
`tags` varchar(4096) not null default '' comment 'json,map,tagkey->regexp|value',
`cause` varchar(255) not null default '',

View File

@ -22,6 +22,7 @@ type TagFilter struct {
type AlertMute struct {
Id int64 `json:"id" gorm:"primaryKey"`
GroupId int64 `json:"group_id"`
Prod string `json:"prod"` // product empty means n9e
Cluster string `json:"cluster"`
Tags ormx.JSONArr `json:"tags"`
Cause string `json:"cause"`
@ -36,14 +37,14 @@ func (m *AlertMute) TableName() string {
return "alert_mute"
}
func AlertMuteGets(bgid int64, query string) (lst []AlertMute, err error) {
session := DB().Where("group_id = ?", bgid)
func AlertMuteGets(prods []string, bgid int64, query string) (lst []AlertMute, err error) {
session := DB().Where("group_id = ? and prod in (?)", bgid, prods)
if query != "" {
arr := strings.Fields(query)
for i := 0; i < len(arr); i++ {
qarg := "%\"" + arr[i] + "\"%"
session = session.Where("tags like ?", qarg, qarg)
qarg := "%" + arr[i] + "%"
session = session.Where("cause like ?", qarg, qarg)
}
}

View File

@ -289,8 +289,16 @@ func AlertRuleGetsByCluster(cluster string) ([]*AlertRule, error) {
return lst, err
}
func AlertRulesGetByProds(prods []string) ([]*AlertRule, error) {
session := DB().Where("disabled = ? and prod IN (?)", 0, prods)
func AlertRulesGetsBy(prods []string, query string) ([]*AlertRule, error) {
session := DB().Where("disabled = ? and prod in (?)", 0, prods)
if query != "" {
arr := strings.Fields(query)
for i := 0; i < len(arr); i++ {
qarg := "%" + arr[i] + "%"
session = session.Where("append_tags like ?", qarg)
}
}
var lst []*AlertRule
err := session.Find(&lst).Error

View File

@ -276,12 +276,11 @@ func configRoute(r *gin.Engine, version string) {
service.DELETE("/targets/tags", targetUnbindTagsByService)
service.PUT("/targets/note", targetUpdateNoteByService)
service.GET("/alert-rules", alertRuleGets)
service.POST("/alert-rules", alertRuleAddByService)
service.DELETE("/alert-rules", alertRuleDel)
service.PUT("/alert-rule/:arid", alertRulePutByService)
service.GET("/alert-rule/:arid", alertRuleGet)
service.GET("/alert-rules-get-by-prod", alertRulesGetByProds)
service.GET("/alert-rules", alertRulesGetByService)
service.GET("/alert-mutes", alertMuteGets)
service.POST("/alert-mutes", alertMuteAddByService)

View File

@ -25,11 +25,11 @@ func alertRuleGets(c *gin.Context) {
ginx.NewRender(c).Data(ars, err)
}
func alertRulesGetByProds(c *gin.Context) {
prods := ginx.QueryStr(c, "prods", "")
arr := strings.Split(prods, ",")
func alertRulesGetByService(c *gin.Context) {
prods := strings.Fields(ginx.QueryStr(c, "prods", ""))
query := ginx.QueryStr(c, "query", "")
ars, err := models.AlertRulesGetByProds(arr)
ars, err := models.AlertRulesGetsBy(prods, query)
if err == nil {
cache := make(map[int64]*models.UserGroup)
for i := 0; i < len(ars); i++ {

View File

@ -1,6 +1,8 @@
package router
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
@ -15,9 +17,10 @@ func alertMuteGetsByBG(c *gin.Context) {
}
func alertMuteGets(c *gin.Context) {
prods := strings.Fields(ginx.QueryStr(c, "prods", ""))
bgid := ginx.QueryInt64(c, "bgid", 0)
query := ginx.QueryStr(c, "query", "")
lst, err := models.AlertMuteGets(bgid, query)
lst, err := models.AlertMuteGets(prods, bgid, query)
ginx.NewRender(c).Data(lst, err)
}