feat(monitor): set alert_channel to metata (#23754)

This commit is contained in:
Zexi Li
2025-11-18 12:52:37 +08:00
committed by GitHub
parent a0d28c214f
commit 9099f682fb
2 changed files with 21 additions and 1 deletions
+19 -1
View File
@@ -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
}
+2
View File
@@ -377,6 +377,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,
@@ -1094,6 +1095,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)