refactor: alert rule name duplicate check

This commit is contained in:
710leo 2021-07-30 13:30:50 +08:00
parent 853053f56d
commit ffe3dd6bca
3 changed files with 19 additions and 0 deletions

View File

@ -46,6 +46,7 @@ var (
"No such alert rule": "告警规则不存在",
"No such alert rule group": "告警规则分组不存在",
"No such alert event": "告警事件不存在",
"Alert rule %s already exists": "告警规则(%s)已存在",
"No such collect rule": "采集规则不存在",
"Decoded metric description empty": "导入的指标释义列表为空",
"User disabled": "用户已被禁用",

View File

@ -89,6 +89,15 @@ func alertRulePut(c *gin.Context) {
arg := AlertRuleGroup(ar.GroupId)
alertRuleWritePermCheck(arg, me)
if ar.Name != f.Name {
num, err := models.AlertRuleCount("group_id=? and name=? and id<>?", ar.GroupId, f.Name, ar.Id)
dangerous(err)
if num > 0 {
bomb(200, "Alert rule %s already exists", f.Name)
}
}
ar.Name = f.Name
ar.Note = f.Note
ar.Type = f.Type

View File

@ -262,6 +262,15 @@ func (ar *AlertRule) Add() error {
return err
}
num, err := AlertRuleCount("group_id=? and name=?", ar.GroupId, ar.Name)
if err != nil {
return err
}
if num > 0 {
return _e("Alert rule %s already exists", ar.Name)
}
now := time.Now().Unix()
ar.CreateAt = now
ar.UpdateAt = now