实现 AlarmNotificationTask
This commit is contained in:
@@ -29,14 +29,20 @@ type AlarmRepository interface {
|
||||
// 返回活跃告警列表、总记录数和错误。
|
||||
ListActiveAlarms(ctx context.Context, opts ActiveAlarmListOptions, page, pageSize int) ([]models.ActiveAlarm, int64, error)
|
||||
|
||||
// UpdateAlarmNotificationStatus 显式更新告警的通知相关状态字段。
|
||||
// lastNotifiedAt: 传入具体的发送时间。
|
||||
// isIgnored: 告警新的忽略状态。
|
||||
// ignoredUntil: 告警新的忽略截止时间 (nil 表示没有忽略截止时间/已取消忽略)。
|
||||
UpdateAlarmNotificationStatus(ctx context.Context, alarmID uint, lastNotifiedAt time.Time, isIgnored bool, ignoredUntil *time.Time) error
|
||||
|
||||
// <-- 下列两个方法是为了性能做出的架构妥协, 业务逻辑入侵仓库层带来的收益远大于通过业务层进行数据筛选 -->
|
||||
|
||||
// ListAlarmsForNotification 查询满足发送告警消息条件的活跃告警列表。
|
||||
// 返回活跃告警列表和错误。
|
||||
// intervalByLevel: key=SeverityLevel, value=interval_in_minutes
|
||||
ListAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) ([]models.ActiveAlarm, error)
|
||||
ListAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]uint) ([]models.ActiveAlarm, error)
|
||||
// 查询满足发送告警消息条件的记录总数
|
||||
CountAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) (int64, error)
|
||||
CountAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]uint) (int64, error)
|
||||
}
|
||||
|
||||
// gormAlarmRepository 是 AlarmRepository 的 GORM 实现。
|
||||
@@ -105,8 +111,31 @@ func (r *gormAlarmRepository) ListActiveAlarms(ctx context.Context, opts ActiveA
|
||||
return results, total, err
|
||||
}
|
||||
|
||||
func (r *gormAlarmRepository) UpdateAlarmNotificationStatus(ctx context.Context, alarmID uint, lastNotifiedAt time.Time, isIgnored bool, ignoredUntil *time.Time) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateAlarmNotificationStatus")
|
||||
|
||||
// 1. 内部安全地构造 map,将强类型参数转换为 GORM 需要的格式
|
||||
// GORM 的 Updates 方法会正确处理 *time.Time (nil -> DB NULL)
|
||||
updates := map[string]interface{}{
|
||||
"last_notified_at": lastNotifiedAt, // time.Time 会被 GORM 视为非空时间
|
||||
"is_ignored": isIgnored,
|
||||
"ignored_until": ignoredUntil, // *time.Time (nil) 会被 GORM 写入 NULL
|
||||
}
|
||||
|
||||
// 2. 执行更新
|
||||
result := r.db.WithContext(repoCtx).
|
||||
Model(&models.ActiveAlarm{}).
|
||||
Where("id = ?", alarmID).
|
||||
Updates(updates) // 仅更新 updates map 中指定的三个字段
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountAlarmsForNotification 查询满足发送告警消息条件的记录总数
|
||||
func (r *gormAlarmRepository) CountAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) (int64, error) {
|
||||
func (r *gormAlarmRepository) CountAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]uint) (int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CountAlarmsForNotification")
|
||||
var total int64
|
||||
|
||||
@@ -127,7 +156,7 @@ func (r *gormAlarmRepository) CountAlarmsForNotification(ctx context.Context, in
|
||||
}
|
||||
|
||||
// ListAlarmsForNotification 查询满足发送告警消息条件的活跃告警列表
|
||||
func (r *gormAlarmRepository) ListAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) ([]models.ActiveAlarm, error) {
|
||||
func (r *gormAlarmRepository) ListAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]uint) ([]models.ActiveAlarm, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAlarmsForNotification")
|
||||
var results []models.ActiveAlarm
|
||||
|
||||
@@ -148,7 +177,7 @@ func (r *gormAlarmRepository) ListAlarmsForNotification(ctx context.Context, int
|
||||
}
|
||||
|
||||
// buildNotificationBaseQuery 负责组合 Group A 和 Group B 的逻辑
|
||||
func (r *gormAlarmRepository) buildNotificationBaseQuery(tx *gorm.DB, intervalByLevel map[models.SeverityLevel]int) *gorm.DB {
|
||||
func (r *gormAlarmRepository) buildNotificationBaseQuery(tx *gorm.DB, intervalByLevel map[models.SeverityLevel]uint) *gorm.DB {
|
||||
|
||||
// 1. 获取所有配置的 Level 列表
|
||||
configuredLevels := make([]models.SeverityLevel, 0, len(intervalByLevel))
|
||||
@@ -208,7 +237,7 @@ func (r *gormAlarmRepository) buildGroupAClause(tx *gorm.DB, configuredLevels []
|
||||
|
||||
// buildGroupBClause 构造 Group B 的 WHERE 语句和参数列表。
|
||||
// 针对 Level 存在配置的告警,使用“间隔发送”逻辑。
|
||||
func (r *gormAlarmRepository) buildGroupBClause(tx *gorm.DB, intervalByLevel map[models.SeverityLevel]int, configuredLevels []models.SeverityLevel) *gorm.DB {
|
||||
func (r *gormAlarmRepository) buildGroupBClause(tx *gorm.DB, intervalByLevel map[models.SeverityLevel]uint, configuredLevels []models.SeverityLevel) *gorm.DB {
|
||||
now := time.Now()
|
||||
|
||||
// B.1. 构造 Level IN 子句
|
||||
|
||||
Reference in New Issue
Block a user