实现查询满足发送告警消息条件的活跃告警列表的仓库层方法
This commit is contained in:
@@ -2,6 +2,8 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
@@ -26,6 +28,15 @@ type AlarmRepository interface {
|
||||
// ListActiveAlarms 支持分页和过滤的活跃告警列表查询。
|
||||
// 返回活跃告警列表、总记录数和错误。
|
||||
ListActiveAlarms(ctx context.Context, opts ActiveAlarmListOptions, page, pageSize int) ([]models.ActiveAlarm, int64, error)
|
||||
|
||||
// <-- 下列两个方法是为了性能做出的架构妥协, 业务逻辑入侵仓库层带来的收益远大于通过业务层进行数据筛选 -->
|
||||
|
||||
// ListAlarmsForNotification 查询满足发送告警消息条件的活跃告警列表。
|
||||
// 返回活跃告警列表和错误。
|
||||
// intervalByLevel: key=SeverityLevel, value=interval_in_minutes
|
||||
ListAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) ([]models.ActiveAlarm, error)
|
||||
// 查询满足发送告警消息条件的记录总数
|
||||
CountAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) (int64, error)
|
||||
}
|
||||
|
||||
// gormAlarmRepository 是 AlarmRepository 的 GORM 实现。
|
||||
@@ -93,3 +104,143 @@ func (r *gormAlarmRepository) ListActiveAlarms(ctx context.Context, opts ActiveA
|
||||
|
||||
return results, total, err
|
||||
}
|
||||
|
||||
// CountAlarmsForNotification 查询满足发送告警消息条件的记录总数
|
||||
func (r *gormAlarmRepository) CountAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) (int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CountAlarmsForNotification")
|
||||
var total int64
|
||||
|
||||
// 1. 构造基础查询对象 (包含 Context 和 Model)
|
||||
baseTx := r.db.WithContext(repoCtx).Model(&models.ActiveAlarm{})
|
||||
|
||||
// 2. 传递给辅助函数应用所有 WHERE 逻辑
|
||||
query := r.buildNotificationBaseQuery(baseTx, intervalByLevel)
|
||||
|
||||
// 3. 只执行 Count
|
||||
err := query.Count(&total).Error
|
||||
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// ListAlarmsForNotification 查询满足发送告警消息条件的活跃告警列表
|
||||
func (r *gormAlarmRepository) ListAlarmsForNotification(ctx context.Context, intervalByLevel map[models.SeverityLevel]int) ([]models.ActiveAlarm, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAlarmsForNotification")
|
||||
var results []models.ActiveAlarm
|
||||
|
||||
// 1. 构造基础查询对象 (包含 Context 和 Model)
|
||||
baseTx := r.db.WithContext(repoCtx).Model(&models.ActiveAlarm{})
|
||||
|
||||
// 2. 传递给辅助函数应用所有 WHERE 逻辑
|
||||
query := r.buildNotificationBaseQuery(baseTx, intervalByLevel)
|
||||
|
||||
// 3. 执行 Find (不排序,高性能)
|
||||
err := query.Find(&results).Error
|
||||
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// buildNotificationBaseQuery 负责组合 Group A 和 Group B 的逻辑
|
||||
func (r *gormAlarmRepository) buildNotificationBaseQuery(tx *gorm.DB, intervalByLevel map[models.SeverityLevel]int) *gorm.DB {
|
||||
|
||||
// 1. 获取所有配置的 Level 列表
|
||||
configuredLevels := make([]models.SeverityLevel, 0, len(intervalByLevel))
|
||||
for level := range intervalByLevel {
|
||||
configuredLevels = append(configuredLevels, level)
|
||||
}
|
||||
|
||||
// 2. 构造 Group A (只发送一次)
|
||||
// Group A 是一个独立的 GORM SubQuery,用于构建 OR 关系
|
||||
groupAQuery := r.buildGroupAClause(tx.Session(&gorm.Session{}), configuredLevels)
|
||||
|
||||
// 3. 构造 Group B (间隔发送)
|
||||
// Group B 也是一个独立的 GORM SubQuery
|
||||
groupBQuery := r.buildGroupBClause(tx.Session(&gorm.Session{}), intervalByLevel, configuredLevels)
|
||||
|
||||
// 4. 最终组合:Group A OR Group B
|
||||
|
||||
// 核心逻辑:利用 GORM 的 Where(SubQuery) OR Where(SubQuery) 特性。
|
||||
// GORM 允许将 WHERE 或 OR 的参数写成 func(db *gorm.DB) *gorm.DB
|
||||
// 这样可以确保子查询的括号被正确处理,实现 (A) OR (B) 结构。
|
||||
|
||||
// 注意:我们必须检查配置,因为如果 Group B 配置为空,我们不应该将其添加到 OR 关系中。
|
||||
if len(configuredLevels) == 0 {
|
||||
// 只有 Group A 存在(即 Level NOT IN 的条件是 1=1)
|
||||
return tx.Where(groupAQuery)
|
||||
}
|
||||
|
||||
// 存在 Group A 和 Group B,用 OR 连接
|
||||
return tx.Where(groupAQuery).Or(groupBQuery)
|
||||
}
|
||||
|
||||
// buildGroupAClause 构造 Group A 的 WHERE 语句和参数列表。
|
||||
// 针对 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)
|
||||
} else {
|
||||
// 如果配置列表为空,则所有 Level 都符合,使用 1=1
|
||||
tx = tx.Where("1 = 1")
|
||||
}
|
||||
|
||||
// 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_NotIgnored: 未被忽略 且 仅发送一次
|
||||
notIgnoredQuery := tx.Where("is_ignored = ? AND last_notified_at IS NULL", false)
|
||||
|
||||
// A.3. 组合 Group A 核心逻辑: (C_A_Ignored OR C_A_NotIgnored)
|
||||
return tx.Where(ignoredQuery).Or(notIgnoredQuery)
|
||||
}
|
||||
|
||||
// buildGroupBClause 构造 Group B 的 WHERE 语句和参数列表。
|
||||
// 针对 Level 存在配置的告警,使用“间隔发送”逻辑。
|
||||
func (r *gormAlarmRepository) buildGroupBClause(tx *gorm.DB, intervalByLevel map[models.SeverityLevel]int, configuredLevels []models.SeverityLevel) *gorm.DB {
|
||||
now := time.Now()
|
||||
|
||||
// B.1. 构造 Level IN 子句
|
||||
tx = tx.Where("level IN (?)", configuredLevels)
|
||||
|
||||
// B.2. 构造 Level-Based 间隔检查 (OR 部分)
|
||||
// 核心思想:利用 GORM 的 Or 链式调用构建 Level 间隔检查子句
|
||||
|
||||
// 初始化 Level 间隔检查查询 (ICC)
|
||||
iccQuery := tx.Session(&gorm.Session{}) // 创建一个干净的子查询对象来构建 ICC
|
||||
|
||||
// 动态添加 Level 间隔检查 OR 条件
|
||||
for level, minutes := range intervalByLevel {
|
||||
// PostgreSQL 语法: last_notified_at + (5 * interval '1 minute') <= ?
|
||||
sql := fmt.Sprintf("level = ? AND last_notified_at + (%d * interval '1 minute') <= ?", minutes)
|
||||
|
||||
// 每次使用 Or 叠加新的 Level 检查
|
||||
iccQuery = iccQuery.Or(sql, level, now)
|
||||
}
|
||||
|
||||
// B.3. 组合 Group B 核心逻辑: (last_notified_at IS NULL OR [ICC])
|
||||
|
||||
// C_B_NotIgnored: 未被忽略
|
||||
notIgnoredQuery := tx.Where("is_ignored = ?", false).Where(
|
||||
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(
|
||||
tx.Where("last_notified_at IS NULL").Or(iccQuery), // LastNotifiedAt IS NULL OR ICC
|
||||
)
|
||||
|
||||
// B.4. 组合 Group B 核心逻辑: (C_B_NotIgnored OR C_B_Ignored)
|
||||
return tx.Where(notIgnoredQuery).Or(ignoredQuery)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user