3.7 KiB
3.7 KiB
阈值告警功能 - 任务清单
本清单按照从底层到上层的依赖关系排序,建议按此顺序进行开发。
1. 基础设施层 (internal/infra)
-
数据库模型 (
models):- 在
internal/infra/models/下创建alarm.go文件,定义ActiveAlarm和HistoricalAlarm两个结构体,包含status,ignored_until,last_notified_at等字段。 - 在
internal/infra/models/schedule.go中,为TaskType新增三个常量:TaskTypeAreaControllerThresholdAlarm,TaskTypeDeviceThresholdAlarm,TaskTypeAlarmNotificationSender。
- 在
-
数据库仓库 (
repository):- 在
internal/infra/repository/下创建alarm_repository.go,实现对active_alarms和historical_alarms表的增删改查操作,并提供MoveToHistorical等方法。 - 在
internal/infra/repository/device_repository.go中,为DeviceRepository接口及其实现增加FindByAreaControllerID方法。
- 在
-
配置 (
config):- 在
config.yml文件中,新增告警相关的配置项,如re_notification_interval(重复通知间隔)。 - 在
internal/infra/config/config.go中,添加对应的字段来加载该配置。
- 在
2. 领域层 (internal/domain)
-
告警领域服务 (
alarm):- 创建
internal/domain/alarm/目录。 - 在该目录下创建
alarm_service.go,定义AlarmService接口及其实现。 - 实现核心业务逻辑:
ReportStatus(处理告警创建/解决) 和GetAndProcessPendingNotifications(获取待发送通知)。该服务将注入AlarmRepository和NotifyService。
- 创建
-
任务实现 (
task):- 创建
area_controller_threshold_alarm_task.go,实现plan.Task接口,其Execute方法调用alarmService.ReportStatus。 - 创建
device_threshold_alarm_task.go,实现逻辑同上。
- 创建
alarm_notification_sender_task.go,实现plan.Task接口,其Execute方法调用alarmService.GetAndProcessPendingNotifications。
- 创建
3. 应用层 (internal/app)
-
告警配置服务 (
service):- 创建
alarm_config_service.go,定义AlarmConfigService,负责告警 配置 的增删改查。 - 实现
ExcludeDeviceIDs的计算和更新逻辑。 - 注入
DeviceRepository和AreaControllerRepository。
- 创建
-
服务集成:
- 在
plan_service.go(或任务工厂) 中,注册上述三种新的任务类型。 - 修改
device_service.go,在处理设备更新(更换区域)和删除的方法中,调用AlarmConfigService的接口来刷新ExcludeDeviceIDs。
- 在
-
告警配置控制器 (
controller):- 创建
internal/app/controller/alarm/目录及alarm_controller.go文件。 - 实现
Create/Update/Delete/Get告警配置的 HTTP 接口,调用AlarmConfigService。
- 创建
-
API 路由 (
api):- 在
internal/app/api/router.go中,注册/api/v1/alarm/region-config和/api/v1/alarm/device-config相关路由,并绑定到AlarmController的方法。
- 在
4. 核心初始化层 (internal/core)
- 系统计划初始化 (
data_initializer.go):- 修改
getPredefinedSystemPlans函数。 - 将 "定时全量数据采集" 计划重命名为 "周期性系统健康检查"。
- 在该计划中,于 "全量采集" 任务之后,添加
TaskTypeAreaControllerThresholdAlarm和TaskTypeDeviceThresholdAlarm任务。 - 新增一个 "告警通知发送" 计划,并为其添加
TaskTypeAlarmNotificationSender任务。
- 修改