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.
This commit is contained in:
Zexi Li
2026-03-31 20:03:45 +08:00
committed by GitHub
parent 31a1aa7f26
commit 7e58612829

View File

@@ -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()