增加告警间隔配置

This commit is contained in:
2025-11-08 00:13:15 +08:00
parent f46c8aed0c
commit 3a59a2755c
4 changed files with 59 additions and 17 deletions

View File

@@ -14,15 +14,17 @@ import (
// AlarmNotificationTaskParams 定义了 AlarmNotificationTask 的参数结构
// 如果用户没有指定某个等级的配置, 则默认为该等级消息只发送一次
type AlarmNotificationTaskParams struct {
// NotificationIntervals 告警通知的发送间隔时间,键为告警等级,值为时间间隔
NotificationIntervals map[models.SeverityLevel]time.Duration `json:"notification_intervals"`
// NotificationIntervals 告警通知的发送间隔时间,键为告警等级,值为时间间隔(分钟)
NotificationIntervals map[models.SeverityLevel]uint `json:"notification_intervals"`
}
// AlarmNotificationTask 告警通知发送任务
type AlarmNotificationTask struct {
ctx context.Context
taskLog *models.TaskExecutionLog
params *AlarmNotificationTaskParams
// notificationIntervals 告警通知的发送间隔时间,键为告警等级,值为 time.Duration
notificationIntervals map[models.SeverityLevel]time.Duration
onceParse sync.Once // 保证解析参数只执行一次
@@ -48,7 +50,7 @@ func (t *AlarmNotificationTask) Execute(ctx context.Context) error {
return err
}
// TODO: 实现告警通知发送逻辑,可以使用 t.params.NotificationIntervals 来获取不同等级的发送间隔
// TODO: 实现告警通知发送逻辑,可以使用 t.notificationIntervals 来获取不同等级的发送间隔
logger.Infof("告警通知发送任务执行完成, 任务ID: %d", t.taskLog.TaskID)
return nil
@@ -87,12 +89,14 @@ func (t *AlarmNotificationTask) parseParameters(ctx context.Context) error {
return
}
// 如果 NotificationIntervals 为 nil则初始化它
if params.NotificationIntervals == nil {
params.NotificationIntervals = make(map[models.SeverityLevel]time.Duration)
// 初始化 notificationIntervals
t.notificationIntervals = make(map[models.SeverityLevel]time.Duration)
// 将 uint 类型的秒数转换为 time.Duration
for level, seconds := range params.NotificationIntervals {
t.notificationIntervals[level] = time.Duration(seconds) * time.Minute
}
t.params = &params
})
return err
}