From 3a59a2755cf8cc69cccfe38240365c009e1fba7f Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 8 Nov 2025 00:13:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=8A=E8=AD=A6=E9=97=B4?= =?UTF-8?q?=E9=9A=94=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yml | 11 +++++++ config.yml | 13 +++++++- .../domain/task/alarm_notification_task.go | 20 +++++++----- internal/infra/config/config.go | 32 ++++++++++++++----- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/config.example.yml b/config.example.yml index 6641bac..ed4f81a 100644 --- a/config.example.yml +++ b/config.example.yml @@ -113,3 +113,14 @@ notify: # 定时采集配置 collection: interval: 1 # 采集间隔 (分钟) + +# 告警通知配置 +alarm_notification: + notification_intervals: # 告警通知间隔(分钟) + debug: 1 + info: 1 + warn: 1 + error: 1 + dpanic: 1 + panic: 1 + fatal: 1 diff --git a/config.yml b/config.yml index 56cbeeb..728e14a 100644 --- a/config.yml +++ b/config.yml @@ -90,4 +90,15 @@ lora_mesh: # 定时采集配置 collection: - interval: 1 # 采集间隔 (分钟) \ No newline at end of file + interval: 1 # 采集间隔 (分钟) + +# 告警通知配置 +alarm_notification: + notification_intervals: # 告警通知间隔 (分钟) + debug: 1 + info: 1 + warn: 1 + error: 1 + dpanic: 1 + panic: 1 + fatal: 1 diff --git a/internal/domain/task/alarm_notification_task.go b/internal/domain/task/alarm_notification_task.go index 8481d17..14ec7d7 100644 --- a/internal/domain/task/alarm_notification_task.go +++ b/internal/domain/task/alarm_notification_task.go @@ -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 = ¶ms }) return err } diff --git a/internal/infra/config/config.go b/internal/infra/config/config.go index 81256b5..2579f3f 100644 --- a/internal/infra/config/config.go +++ b/internal/infra/config/config.go @@ -47,6 +47,9 @@ type Config struct { // Collection 定时采集配置 Collection CollectionConfig `yaml:"collection"` + + // AlarmNotification 告警通知配置 + AlarmNotification AlarmNotificationConfig `yaml:"alarm_notification"` } // AppConfig 代表应用基础配置 @@ -204,14 +207,27 @@ type CollectionConfig struct { Interval int `yaml:"interval"` } -// NewConfig 创建并返回一个新的配置实例 -func NewConfig() *Config { - // 默认值可以在这里设置,但我们优先使用配置文件中的值 - return &Config{ - Collection: CollectionConfig{ - Interval: 1, // 默认为1分钟 - }, - } +type NotificationIntervalsConfig struct { + // DebugIntervalMinutes Debug级别告警的通知间隔(分钟) + DebugIntervalMinutes uint `yaml:"debug"` + // InfoIntervalMinutes Info级别告警的通知间隔(分钟) + InfoIntervalMinutes uint `yaml:"info"` + // WarnIntervalMinutes Warn级别告警的通知间隔(分钟) + WarnIntervalMinutes uint `yaml:"warn"` + // ErrorIntervalMinutes Error级别告警的通知间隔(分钟) + ErrorIntervalMinutes uint `yaml:"error"` + // DPanicIntervalMinutes DPanic级别告警的通知间隔(分钟) + DPanicIntervalMinutes uint `yaml:"dpanic"` + // PanicIntervalMinutes Panic级别告警的通知间隔(分钟) + PanicIntervalMinutes uint `yaml:"panic"` + // FatalIntervalMinutes Fatal级别告警的通知间隔(分钟) + FatalIntervalMinutes uint `yaml:"fatal"` +} + +// AlarmNotificationConfig 告警通知配置 +type AlarmNotificationConfig struct { + // NotificationIntervals 告警通知的发送间隔时间,键为告警等级,值为时间间隔(秒) + NotificationIntervals NotificationIntervalsConfig `yaml:"notification_intervals"` } // Load 从指定路径加载配置文件