nightingale1/models/alert_rule_group.go

198 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package models
import (
"fmt"
"sort"
"strings"
"time"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/str"
)
type AlertRuleGroup struct {
Id int64 `json:"id"`
Name string `json:"name"`
UserGroupIds string `json:"user_group_ids"`
CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"`
UpdateAt int64 `json:"update_at"`
UpdateBy string `json:"update_by"`
UserGroups []UserGroup `json:"user_groups" xorm:"-"`
}
func (arg *AlertRuleGroup) TableName() string {
return "alert_rule_group"
}
func (arg *AlertRuleGroup) Validate() error {
if str.Dangerous(arg.Name) {
return _e("AlertRuleGroup name has invalid characters")
}
return nil
}
func (arg *AlertRuleGroup) Add() error {
if err := arg.Validate(); err != nil {
return err
}
num, err := AlertRuleGroupCount("name=?", arg.Name)
if err != nil {
return err
}
if num > 0 {
return _e("AlertRuleGroup %s already exists", arg.Name)
}
now := time.Now().Unix()
arg.CreateAt = now
arg.UpdateAt = now
return DBInsertOne(arg)
}
func AlertRuleGroupCount(where string, args ...interface{}) (num int64, err error) {
num, err = DB.Where(where, args...).Count(new(AlertRuleGroup))
if err != nil {
logger.Errorf("mysql.error: count alert_rule_group fail: %v", err)
return num, internalServerError
}
return num, nil
}
func (arg *AlertRuleGroup) Update(cols ...string) error {
if err := arg.Validate(); err != nil {
return err
}
_, err := DB.Where("id=?", arg.Id).Cols(cols...).Update(arg)
if err != nil {
logger.Errorf("mysql.error: update alert_rule_group(id=%d) fail: %v", arg.Id, err)
return internalServerError
}
return nil
}
func (arg *AlertRuleGroup) FillUserGroups() error {
ids := strings.Fields(arg.UserGroupIds)
if len(ids) == 0 {
arg.UserGroups = []UserGroup{}
return nil
}
ugs, err := UserGroupGetsByIdsStr(ids)
if err != nil {
logger.Errorf("mysql.error: UserGroupGetsByIds fail: %v", err)
return internalServerError
}
arg.UserGroups = ugs
// 这里附一个清理逻辑如果某个团队已经删除了就顺带把这个团队id从arg中删除
ugslen := len(ugs)
idslst := make([]string, 0, ugslen)
for i := 0; i < ugslen; i++ {
idslst = append(idslst, fmt.Sprint(ugs[i].Id))
}
sort.Strings(idslst)
newids := strings.Join(idslst, " ")
// 把原来的ids也排个序两相比较如果发生变化就说明有团队已经被删了更新之
sort.Strings(ids)
oldids := strings.Join(ids, " ")
if newids != oldids {
arg.UserGroupIds = newids
arg.Update("user_group_ids")
}
return nil
}
func AlertRuleGroupTotal(query string) (num int64, err error) {
if query != "" {
q := "%" + query + "%"
num, err = DB.Where("name like ?", q).Count(new(AlertRuleGroup))
} else {
num, err = DB.Count(new(AlertRuleGroup))
}
if err != nil {
logger.Errorf("mysql.error: count alert_rule_group fail: %v", err)
return 0, internalServerError
}
return num, nil
}
func AlertRuleGroupGets(query string, limit, offset int) ([]AlertRuleGroup, error) {
session := DB.Limit(limit, offset).OrderBy("name")
if query != "" {
q := "%" + query + "%"
session = session.Where("name like ?", q)
}
var objs []AlertRuleGroup
err := session.Find(&objs)
if err != nil {
logger.Errorf("mysql.error: query alert_rule_group fail: %v", err)
return objs, internalServerError
}
if len(objs) == 0 {
return []AlertRuleGroup{}, nil
}
return objs, nil
}
func AlertRuleGroupGet(where string, args ...interface{}) (*AlertRuleGroup, error) {
var obj AlertRuleGroup
has, err := DB.Where(where, args...).Get(&obj)
if err != nil {
logger.Errorf("mysql.error: query alert_rule_group(%s)%+v fail: %s", where, args, err)
return nil, internalServerError
}
if !has {
return nil, nil
}
return &obj, nil
}
// Del AlertRuleGroup删除前提是下面没有AlertRule了
func (arg *AlertRuleGroup) Del() error {
ds, err := AlertRulesOfGroup(arg.Id)
if err != nil {
return err
}
if len(ds) > 0 {
return _e("There are still alert rules under the group")
}
session := DB.NewSession()
defer session.Close()
if err := session.Begin(); err != nil {
return err
}
if _, err := session.Exec("DELETE FROM alert_rule_group_favorite WHERE group_id=?", arg.Id); err != nil {
logger.Errorf("mysql.error: delete alert_rule_group_favorite fail: %v", err)
return err
}
if _, err := session.Exec("DELETE FROM alert_rule_group WHERE id=?", arg.Id); err != nil {
logger.Errorf("mysql.error: delete alert_rule_group fail: %v", err)
return err
}
return session.Commit()
}