feat: support batch edit notify-users, notify-channels and append-tags of alert rule (#757)

This commit is contained in:
Istil 2021-08-03 14:20:22 +08:00 committed by GitHub
parent 5064e5b0b1
commit 2ef85d9aae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 3 deletions

View File

@ -144,6 +144,8 @@ func configRoutes(r *gin.Engine) {
pages.POST("/alert-rules", login(), alertRuleAdd)
pages.PUT("/alert-rules/status", login(), alertRuleStatusPut)
pages.PUT("/alert-rules/notify-groups", login(), alertRuleNotifyGroupsPut)
pages.PUT("/alert-rules/notify-channels", login(), alertRuleNotifyChannelsPut)
pages.PUT("/alert-rules/append-tags", login(), alertRuleAppendTagsPut)
pages.GET("/alert-rule/:id", login(), alertRuleGet)
pages.PUT("/alert-rule/:id", login(), alertRulePut)
pages.DELETE("/alert-rule/:id", login(), alertRuleDel)
@ -270,6 +272,8 @@ func configRoutes(r *gin.Engine) {
v1.POST("/alert-rules", login(), alertRuleAdd)
v1.PUT("/alert-rules/status", login(), alertRuleStatusPut)
v1.PUT("/alert-rules/notify-groups", login(), alertRuleNotifyGroupsPut)
v1.PUT("/alert-rules/notify-channels", login(), alertRuleNotifyChannelsPut)
v1.PUT("/alert-rules/append-tags", login(), alertRuleAppendTagsPut)
v1.GET("/alert-rule/:id", login(), alertRuleGet)
v1.PUT("/alert-rule/:id", login(), alertRulePut)
v1.DELETE("/alert-rule/:id", login(), alertRuleDel)

View File

@ -171,6 +171,7 @@ func alertRuleStatusPut(c *gin.Context) {
type alertRuleNotifyGroupsForm struct {
Ids []int64 `json:"ids"`
NotifyGroups string `json:"notify_groups"`
NotifyUsers string `json:"notify_users"`
}
func alertRuleNotifyGroupsPut(c *gin.Context) {
@ -189,7 +190,51 @@ func alertRuleNotifyGroupsPut(c *gin.Context) {
alertRuleWritePermCheck(arg, me)
}
renderMessage(c, models.AlertRuleUpdateNotifyGroup(f.Ids, f.NotifyGroups))
renderMessage(c, models.AlertRuleUpdateNotifyGroups(f.Ids, f.NotifyGroups, f.NotifyUsers))
}
type alertRuleNotifyChannelsForm struct {
Ids []int64 `json:"ids"`
NotifyChannels string `json:"notify_channels"`
}
func alertRuleNotifyChannelsPut(c *gin.Context) {
var f alertRuleNotifyChannelsForm
bind(c, &f)
me := loginUser(c).MustPerm("alert_rule_modify")
if len(f.Ids) == 0 {
bomb(http.StatusBadRequest, "ids is empty")
}
for _, id := range f.Ids {
alertRule := AlertRule(id)
arg := AlertRuleGroup(alertRule.GroupId)
alertRuleWritePermCheck(arg, me)
}
renderMessage(c, models.AlertRuleUpdateNotifyChannels(f.Ids, f.NotifyChannels))
}
type alertRuleAppendTagsForm struct {
Ids []int64 `json:"ids"`
AppendTags string `json:"append_tags"`
}
func alertRuleAppendTagsPut(c *gin.Context) {
var f alertRuleAppendTagsForm
bind(c, &f)
me := loginUser(c).MustPerm("alert_rule_modify")
if len(f.Ids) == 0 {
bomb(http.StatusBadRequest, "ids is empty")
}
for _, id := range f.Ids {
alertRule := AlertRule(id)
arg := AlertRuleGroup(alertRule.GroupId)
alertRuleWritePermCheck(arg, me)
}
renderMessage(c, models.AlertRuleUpdateAppendTags(f.Ids, f.AppendTags))
}
func alertRuleDel(c *gin.Context) {

View File

@ -296,8 +296,18 @@ func AlertRuleUpdateStatus(ids []int64, status int) error {
return err
}
func AlertRuleUpdateNotifyGroup(ids []int64, notifyGroups string) error {
_, err := DB.Exec("UPDATE alert_rule SET notify_groups = ? where id in ("+str.IdsString(ids)+")", notifyGroups)
func AlertRuleUpdateNotifyGroups(ids []int64, notifyGroups string, notifyUsers string) error {
_, err := DB.Exec("UPDATE alert_rule SET notify_groups = ? , notify_users = ? where id in ("+str.IdsString(ids)+")", notifyGroups, notifyUsers)
return err
}
func AlertRuleUpdateNotifyChannels(ids []int64, notifyChannels string) error {
_, err := DB.Exec("UPDATE alert_rule SET notify_channels = ? where id in ("+str.IdsString(ids)+")", notifyChannels)
return err
}
func AlertRuleUpdateAppendTags(ids []int64, appendTags string) error {
_, err := DB.Exec("UPDATE alert_rule SET append_tags = ? where id in ("+str.IdsString(ids)+")", appendTags)
return err
}