code refactor

This commit is contained in:
Ulric Qin 2022-05-31 14:44:57 +08:00
parent ce35e23a0f
commit ab5e8c366e
1 changed files with 26 additions and 1 deletions

View File

@ -65,10 +65,35 @@ func alertRuleAddByService(c *gin.Context) {
if count == 0 {
ginx.Bomb(http.StatusBadRequest, "input json is empty")
}
reterr := alertRuleAdd(lst, "", 0, c.GetHeader("X-Language"))
reterr := alertRuleAddForService(lst, "")
ginx.NewRender(c).Data(reterr, nil)
}
func alertRuleAddForService(lst []models.AlertRule, username string) map[string]string {
count := len(lst)
// alert rule name -> error string
reterr := make(map[string]string)
for i := 0; i < count; i++ {
lst[i].Id = 0
if username != "" {
lst[i].CreateBy = username
lst[i].UpdateBy = username
}
if err := lst[i].FE2DB(); err != nil {
reterr[lst[i].Name] = err.Error()
continue
}
if err := lst[i].Add(); err != nil {
reterr[lst[i].Name] = err.Error()
} else {
reterr[lst[i].Name] = ""
}
}
return reterr
}
func alertRuleAdd(lst []models.AlertRule, username string, bgid int64, lang string) map[string]string {
count := len(lst)
// alert rule name -> error string