From 7e58612829d2f89d0a1c2c11e0b403cba498aed3 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 31 Mar 2026 20:03:45 +0800 Subject: [PATCH] fix(monitor): update silent_period when channel is empty (#24604) silent_period is stored as Frequency on the notification, not on the alert itself. PostUpdate only called UpdateNotification (which recreates notifications with the new silent_period) when Channel was non-empty. When channel was an empty array, the silent_period update was silently skipped. Add an else-if branch to directly update the existing notifications' Frequency when silent_period changes but channel is empty. --- pkg/monitor/models/commonalert.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index 2e8ece92c1..c1ca88f60f 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -1324,6 +1324,10 @@ func (alert *SCommonAlert) PostUpdate( log.Errorf("update notification error: %v", err) } alert.SetChannel(ctx, updateInput.Channel) + } else if len(updateInput.SilentPeriod) != 0 { + if err := alert.updateNotificationSilentPeriod(updateInput.SilentPeriod); err != nil { + log.Errorf("update notification silent_period error: %v", err) + } } if _, err := data.GetString("scope"); err == nil { _, err = alert.PerformSetScope(ctx, userCred, query, data) @@ -1717,6 +1721,27 @@ func (alert *SCommonAlert) GetSilentPeriod() (int64, error) { return 0, nil } +func (alert *SCommonAlert) updateNotificationSilentPeriod(silentPeriod string) error { + duration, _ := time.ParseDuration(silentPeriod) + frequency := int64(duration / time.Second) + notis, err := alert.GetNotifications() + if err != nil { + return errors.Wrap(err, "GetNotifications") + } + for _, n := range notis { + noti, _ := n.GetNotification() + if noti != nil { + if _, err := db.Update(noti, func() error { + noti.Frequency = frequency + return nil + }); err != nil { + return errors.Wrapf(err, "update notification %s frequency", noti.GetId()) + } + } + } + return nil +} + func (alert *SCommonAlert) GetAlertRules(silentPeriod int64) ([]*monitor.AlertRecordRule, error) { rules := make([]*monitor.AlertRecordRule, 0) settings, err := alert.GetSettings()