增加通知状态刷新任务
This commit is contained in:
@@ -69,6 +69,11 @@ type AlarmRepository interface {
|
||||
// ignoredUntil: 告警新的忽略截止时间 (nil 表示没有忽略截止时间/已取消忽略)。
|
||||
UpdateAlarmNotificationStatus(ctx context.Context, alarmID uint32, lastNotifiedAt time.Time, isIgnored bool, ignoredUntil *time.Time) error
|
||||
|
||||
// ClearExpiredIgnoredAlarms 清除所有已过期的告警忽略状态。
|
||||
// 将 is_ignored 设置为 false,ignored_until 设置为 NULL。
|
||||
// 返回受影响的行数和错误。
|
||||
ClearExpiredIgnoredAlarms(ctx context.Context) (int64, error)
|
||||
|
||||
// <-- 下列两个方法是为了性能做出的架构妥协, 业务逻辑入侵仓库层带来的收益远大于通过业务层进行数据筛选 -->
|
||||
|
||||
// ListAlarmsForNotification 查询满足发送告警消息条件的活跃告警列表。
|
||||
@@ -159,6 +164,30 @@ func (r *gormAlarmRepository) UpdateIgnoreStatus(ctx context.Context, id uint32,
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClearExpiredIgnoredAlarms 清除所有已过期的告警忽略状态。
|
||||
// 将 is_ignored 设置为 false,ignored_until 设置为 NULL。
|
||||
// 返回受影响的行数和错误。
|
||||
func (r *gormAlarmRepository) ClearExpiredIgnoredAlarms(ctx context.Context) (int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ClearExpiredIgnoredAlarms")
|
||||
now := time.Now()
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"is_ignored": false,
|
||||
"ignored_until": nil, // 将 ignored_until 设置为 NULL
|
||||
}
|
||||
|
||||
result := r.db.WithContext(repoCtx).
|
||||
Model(&models.ActiveAlarm{}).
|
||||
Where("is_ignored = ? AND ignored_until <= ?", true, now).
|
||||
Updates(updates)
|
||||
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
|
||||
return result.RowsAffected, nil
|
||||
}
|
||||
|
||||
// ListActiveAlarms 实现了分页和过滤查询活跃告警记录的功能
|
||||
func (r *gormAlarmRepository) ListActiveAlarms(ctx context.Context, opts ActiveAlarmListOptions, page, pageSize int) ([]models.ActiveAlarm, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListActiveAlarms")
|
||||
@@ -368,8 +397,6 @@ func (r *gormAlarmRepository) buildNotificationBaseQuery(tx *gorm.DB, intervalBy
|
||||
// 针对 Level 缺失配置(或所有 Level)的告警,使用“只发送一次”逻辑:LastNotifiedAt IS NULL
|
||||
// 参数 configuredLevels: 用于构建 Level NOT IN (?) 子句。
|
||||
func (r *gormAlarmRepository) buildGroupAClause(tx *gorm.DB, configuredLevels []models.SeverityLevel) *gorm.DB {
|
||||
now := time.Now()
|
||||
|
||||
// A.1. 构造 Level 范围检查子句 (Level NOT IN 或 1=1)
|
||||
if len(configuredLevels) > 0 {
|
||||
tx = tx.Where("level NOT IN (?)", configuredLevels)
|
||||
@@ -380,8 +407,8 @@ func (r *gormAlarmRepository) buildGroupAClause(tx *gorm.DB, configuredLevels []
|
||||
|
||||
// A.2. 构造 Group A 核心逻辑 (LastNotifiedAt IS NULL 且满足忽略条件)
|
||||
|
||||
// C_A_Ignored: 被忽略但忽略期结束 且 仅发送一次
|
||||
ignoredQuery := tx.Where("is_ignored = ? AND ignored_until <= ? AND last_notified_at IS NULL", true, now)
|
||||
// C_A_Ignored: 被忽略 且 仅发送一次 (忽略期已在前置任务中解除)
|
||||
ignoredQuery := tx.Where("is_ignored = ? AND last_notified_at IS NULL", true)
|
||||
|
||||
// C_A_NotIgnored: 未被忽略 且 仅发送一次
|
||||
notIgnoredQuery := tx.Where("is_ignored = ? AND last_notified_at IS NULL", false)
|
||||
@@ -420,8 +447,8 @@ func (r *gormAlarmRepository) buildGroupBClause(tx *gorm.DB, intervalByLevel map
|
||||
tx.Where("last_notified_at IS NULL").Or(iccQuery), // LastNotifiedAt IS NULL OR ICC
|
||||
)
|
||||
|
||||
// C_B_Ignored: 被忽略但忽略期结束
|
||||
ignoredQuery := tx.Where("is_ignored = ? AND ignored_until <= ?", true, now).Where(
|
||||
// C_B_Ignored: 被忽略 (忽略期已在前置任务中解除)
|
||||
ignoredQuery := tx.Where("is_ignored = ?", true).Where(
|
||||
tx.Where("last_notified_at IS NULL").Or(iccQuery), // LastNotifiedAt IS NULL OR ICC
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user