mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
feat(monitor): set alert_channel to metata (#23755)
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/rbacscope"
|
||||
"yunion.io/x/sqlchemy"
|
||||
@@ -36,7 +37,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
AlertMetadataTitle = "alert_title"
|
||||
AlertMetadataTitle = "alert_title"
|
||||
AlertMetadataChannel = "alert_channel"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -179,6 +181,22 @@ func (alert *SAlert) GetTitle() string {
|
||||
return alert.GetMetadata(context.Background(), AlertMetadataTitle, nil)
|
||||
}
|
||||
|
||||
func (alert *SAlert) SetChannel(ctx context.Context, channel []string) error {
|
||||
return alert.SetMetadata(ctx, AlertMetadataChannel, jsonutils.Marshal(channel), nil)
|
||||
}
|
||||
|
||||
func (alert *SAlert) GetChannel() []string {
|
||||
channelJson := alert.GetMetadataJson(context.Background(), AlertMetadataChannel, nil)
|
||||
if channelJson == nil {
|
||||
return []string{}
|
||||
}
|
||||
channel := []string{}
|
||||
if err := channelJson.Unmarshal(&channel); err != nil {
|
||||
log.Warningf("get channel failed when unmarshal: %v", err)
|
||||
}
|
||||
return channel
|
||||
}
|
||||
|
||||
func (alert *SAlert) ShouldUpdateState(newState monitor.AlertStateType) bool {
|
||||
return monitor.AlertStateType(alert.State) != newState
|
||||
}
|
||||
|
||||
@@ -379,6 +379,7 @@ func (alert *SCommonAlert) customizeCreateNotis(ctx context.Context, userCred mc
|
||||
return alert.createAlertNoti(ctx, userCred, input.Name, s, input.SilentPeriod, true)
|
||||
}
|
||||
|
||||
alert.SetChannel(ctx, input.Channel)
|
||||
for _, channel := range input.Channel {
|
||||
s := &monitor.NotificationSettingOneCloud{
|
||||
Channel: channel,
|
||||
@@ -1096,6 +1097,7 @@ func (alert *SCommonAlert) PostUpdate(
|
||||
if err := alert.UpdateNotification(ctx, userCred, query, data); err != nil {
|
||||
log.Errorf("update notification error: %v", err)
|
||||
}
|
||||
alert.SetChannel(ctx, updateInput.Channel)
|
||||
}
|
||||
if _, err := data.GetString("scope"); err == nil {
|
||||
_, err = alert.PerformSetScope(ctx, userCred, query, data)
|
||||
|
||||
Reference in New Issue
Block a user