实现列表查询活跃告警和历史告警
This commit is contained in:
@@ -195,6 +195,8 @@ func (a *API) setupRoutes() {
|
||||
{
|
||||
thresholdGroup.POST("/:id/snooze", a.alarmController.SnoozeThresholdAlarm) // 忽略阈值告警
|
||||
thresholdGroup.POST("/:id/cancel-snooze", a.alarmController.CancelSnoozeThresholdAlarm) // 取消忽略阈值告警
|
||||
thresholdGroup.GET("/active-alarms", a.alarmController.ListActiveAlarms) // 获取活跃告警
|
||||
thresholdGroup.GET("/historical-alarms", a.alarmController.ListHistoricalAlarms) // 获取历史告警
|
||||
}
|
||||
}
|
||||
logger.Debug("告警相关接口注册成功 (需要认证和审计)")
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"gorm.io/gorm"
|
||||
@@ -107,3 +108,73 @@ func (t *ThresholdAlarmController) CancelSnoozeThresholdAlarm(ctx echo.Context)
|
||||
logger.Infof("%s: 告警忽略状态已成功取消, ID: %d", actionType, alarmID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "告警忽略状态已成功取消", nil, actionType, "告警忽略状态已成功取消", alarmID)
|
||||
}
|
||||
|
||||
// ListActiveAlarms godoc
|
||||
// @Summary 批量查询活跃告警
|
||||
// @Description 根据过滤条件和分页参数查询活跃告警列表
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param query query dto.ListActiveAlarmRequest true "查询参数"
|
||||
// @Success 200 {object} controller.Response{data=dto.ListActiveAlarmResponse} "成功获取活跃告警列表"
|
||||
// @Router /api/v1/alarm/threshold/active-alarms [get]
|
||||
func (t *ThresholdAlarmController) ListActiveAlarms(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "ListActiveAlarms")
|
||||
|
||||
const actionType = "批量查询活跃告警"
|
||||
var req dto.ListActiveAlarmRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求参数: "+err.Error(), actionType, "请求参数绑定失败", nil)
|
||||
}
|
||||
|
||||
resp, err := t.thresholdAlarmService.ListActiveAlarms(reqCtx, &req)
|
||||
if err != nil {
|
||||
// 捕获 ErrInvalidPagination 错误,并返回 Bad Request
|
||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||
}
|
||||
logger.Errorf("%s: 服务层查询活跃告警失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询活跃告警失败: "+err.Error(), actionType, "服务层查询活跃告警失败", req)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "成功获取活跃告警列表", resp, actionType, "成功获取活跃告警列表", req)
|
||||
}
|
||||
|
||||
// ListHistoricalAlarms godoc
|
||||
// @Summary 批量查询历史告警
|
||||
// @Description 根据过滤条件和分页参数查询历史告警列表
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param query query dto.ListHistoricalAlarmRequest true "查询参数"
|
||||
// @Success 200 {object} controller.Response{data=dto.ListHistoricalAlarmResponse} "成功获取历史告警列表"
|
||||
// @Router /api/v1/alarm/threshold/historical-alarms [get]
|
||||
func (t *ThresholdAlarmController) ListHistoricalAlarms(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "ListHistoricalAlarms")
|
||||
|
||||
const actionType = "批量查询历史告警"
|
||||
var req dto.ListHistoricalAlarmRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求参数: "+err.Error(), actionType, "请求参数绑定失败", nil)
|
||||
}
|
||||
|
||||
resp, err := t.thresholdAlarmService.ListHistoricalAlarms(reqCtx, &req)
|
||||
if err != nil {
|
||||
// 捕获 ErrInvalidPagination 错误,并返回 Bad Request
|
||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||
logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||
}
|
||||
logger.Errorf("%s: 服务层查询历史告警失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询历史告警失败: "+err.Error(), actionType, "服务层查询历史告警失败", req)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "成功获取历史告警列表", resp, actionType, "成功获取历史告警列表", req)
|
||||
}
|
||||
|
||||
65
internal/app/dto/alarm_converter.go
Normal file
65
internal/app/dto/alarm_converter.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// NewListActiveAlarmResponse 从模型数据创建活跃告警列表响应 DTO
|
||||
func NewListActiveAlarmResponse(data []models.ActiveAlarm, total int64, page, pageSize int) *ListActiveAlarmResponse {
|
||||
dtos := make([]ActiveAlarmDTO, len(data))
|
||||
for i, item := range data {
|
||||
dtos[i] = ActiveAlarmDTO{
|
||||
ID: item.ID,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
SourceType: item.SourceType,
|
||||
SourceID: item.SourceID,
|
||||
AlarmCode: item.AlarmCode,
|
||||
AlarmSummary: item.AlarmSummary,
|
||||
Level: item.Level,
|
||||
AlarmDetails: item.AlarmDetails,
|
||||
TriggerTime: item.TriggerTime,
|
||||
IsIgnored: item.IsIgnored,
|
||||
IgnoredUntil: item.IgnoredUntil,
|
||||
LastNotifiedAt: item.LastNotifiedAt,
|
||||
}
|
||||
}
|
||||
|
||||
return &ListActiveAlarmResponse{
|
||||
List: dtos,
|
||||
Pagination: PaginationDTO{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewListHistoricalAlarmResponse 从模型数据创建历史告警列表响应 DTO
|
||||
func NewListHistoricalAlarmResponse(data []models.HistoricalAlarm, total int64, page, pageSize int) *ListHistoricalAlarmResponse {
|
||||
dtos := make([]HistoricalAlarmDTO, len(data))
|
||||
for i, item := range data {
|
||||
dtos[i] = HistoricalAlarmDTO{
|
||||
ID: item.ID,
|
||||
SourceType: item.SourceType,
|
||||
SourceID: item.SourceID,
|
||||
AlarmCode: item.AlarmCode,
|
||||
AlarmSummary: item.AlarmSummary,
|
||||
Level: item.Level,
|
||||
AlarmDetails: item.AlarmDetails,
|
||||
TriggerTime: item.TriggerTime,
|
||||
ResolveTime: item.ResolveTime,
|
||||
ResolveMethod: item.ResolveMethod,
|
||||
ResolvedBy: item.ResolvedBy,
|
||||
}
|
||||
}
|
||||
|
||||
return &ListHistoricalAlarmResponse{
|
||||
List: dtos,
|
||||
Pagination: PaginationDTO{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,83 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// SnoozeAlarmRequest 定义了忽略告警的请求体
|
||||
type SnoozeAlarmRequest struct {
|
||||
DurationMinutes uint `json:"duration_minutes" validate:"required,min=1"` // 忽略时长,单位分钟
|
||||
}
|
||||
|
||||
// ListActiveAlarmRequest 定义了获取活跃告警列表的请求参数
|
||||
type ListActiveAlarmRequest struct {
|
||||
Page int `json:"page" query:"page"`
|
||||
PageSize int `json:"page_size" query:"page_size"`
|
||||
SourceType *models.AlarmSourceType `json:"source_type" query:"source_type"` // 按告警来源类型过滤
|
||||
SourceID *uint `json:"source_id" query:"source_id"` // 按告警来源ID过滤
|
||||
Level *models.SeverityLevel `json:"level" query:"level"` // 按告警严重性等级过滤
|
||||
IsIgnored *bool `json:"is_ignored" query:"is_ignored"` // 按是否被忽略过滤
|
||||
TriggerTime *time.Time `json:"trigger_time" query:"trigger_time"` // 告警触发时间范围 - 开始时间
|
||||
EndTime *time.Time `json:"end_time" query:"end_time"` // 告警触发时间范围 - 结束时间
|
||||
OrderBy string `json:"order_by" query:"order_by"` // 排序字段,例如 "trigger_time DESC"
|
||||
}
|
||||
|
||||
// ActiveAlarmDTO 是用于API响应的活跃告警结构
|
||||
type ActiveAlarmDTO struct {
|
||||
ID uint `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
SourceType models.AlarmSourceType `json:"source_type"`
|
||||
SourceID uint `json:"source_id"`
|
||||
AlarmCode models.AlarmCode `json:"alarm_code"`
|
||||
AlarmSummary string `json:"alarm_summary"`
|
||||
Level models.SeverityLevel `json:"level"`
|
||||
AlarmDetails string `json:"alarm_details"`
|
||||
TriggerTime time.Time `json:"trigger_time"`
|
||||
IsIgnored bool `json:"is_ignored"`
|
||||
IgnoredUntil *time.Time `json:"ignored_until"`
|
||||
LastNotifiedAt *time.Time `json:"last_notified_at"`
|
||||
}
|
||||
|
||||
// ListActiveAlarmResponse 是获取活跃告警列表的响应结构
|
||||
type ListActiveAlarmResponse struct {
|
||||
List []ActiveAlarmDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
// ListHistoricalAlarmRequest 定义了获取历史告警列表的请求参数
|
||||
type ListHistoricalAlarmRequest struct {
|
||||
Page int `json:"page" query:"page"`
|
||||
PageSize int `json:"page_size" query:"page_size"`
|
||||
SourceType *models.AlarmSourceType `json:"source_type" query:"source_type"` // 按告警来源类型过滤
|
||||
SourceID *uint `json:"source_id" query:"source_id"` // 按告警来源ID过滤
|
||||
Level *models.SeverityLevel `json:"level" query:"level"` // 按告警严重性等级过滤
|
||||
TriggerTimeStart *time.Time `json:"trigger_time_start" query:"trigger_time_start"` // 告警触发时间范围 - 开始时间
|
||||
TriggerTimeEnd *time.Time `json:"trigger_time_end" query:"trigger_time_end"` // 告警触发时间范围 - 结束时间
|
||||
ResolveTimeStart *time.Time `json:"resolve_time_start" query:"resolve_time_start"` // 告警解决时间范围 - 开始时间
|
||||
ResolveTimeEnd *time.Time `json:"resolve_time_end" query:"resolve_time_end"` // 告警解决时间范围 - 结束时间
|
||||
OrderBy string `json:"order_by" query:"order_by"` // 排序字段,例如 "trigger_time DESC"
|
||||
}
|
||||
|
||||
// HistoricalAlarmDTO 是用于API响应的历史告警结构
|
||||
type HistoricalAlarmDTO struct {
|
||||
ID uint `json:"id"`
|
||||
SourceType models.AlarmSourceType `json:"source_type"`
|
||||
SourceID uint `json:"source_id"`
|
||||
AlarmCode models.AlarmCode `json:"alarm_code"`
|
||||
AlarmSummary string `json:"alarm_summary"`
|
||||
Level models.SeverityLevel `json:"level"`
|
||||
AlarmDetails string `json:"alarm_details"`
|
||||
TriggerTime time.Time `json:"trigger_time"`
|
||||
ResolveTime time.Time `json:"resolve_time"`
|
||||
ResolveMethod string `json:"resolve_method"`
|
||||
ResolvedBy *uint `json:"resolved_by"`
|
||||
}
|
||||
|
||||
// ListHistoricalAlarmResponse 是获取历史告警列表的响应结构
|
||||
type ListHistoricalAlarmResponse struct {
|
||||
List []HistoricalAlarmDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
10
internal/app/dto/dto.go
Normal file
10
internal/app/dto/dto.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package dto
|
||||
|
||||
// --- General ---
|
||||
|
||||
// PaginationDTO 定义了分页信息的标准结构
|
||||
type PaginationDTO struct {
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
@@ -7,15 +7,6 @@ import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// --- General ---
|
||||
|
||||
// PaginationDTO 定义了分页信息的标准结构
|
||||
type PaginationDTO struct {
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
// --- SensorData ---
|
||||
|
||||
// ListSensorDataRequest 定义了获取传感器数据列表的请求参数
|
||||
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
domainAlarm "git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm" // 引入领域层的 AlarmService
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
domainAlarm "git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
)
|
||||
|
||||
// ThresholdAlarmService 定义了阈值告警配置服务的接口。
|
||||
@@ -15,19 +17,25 @@ type ThresholdAlarmService interface {
|
||||
SnoozeThresholdAlarm(ctx context.Context, alarmID uint, durationMinutes uint) error
|
||||
// CancelSnoozeThresholdAlarm 取消对一个阈值告警的忽略状态。
|
||||
CancelSnoozeThresholdAlarm(ctx context.Context, alarmID uint) error
|
||||
// ListActiveAlarms 批量查询活跃告警。
|
||||
ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error)
|
||||
// ListHistoricalAlarms 批量查询历史告警。
|
||||
ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error)
|
||||
}
|
||||
|
||||
// thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。
|
||||
type thresholdAlarmService struct {
|
||||
ctx context.Context
|
||||
alarmService domainAlarm.AlarmService // 注入领域层的 AlarmService
|
||||
alarmService domainAlarm.AlarmService
|
||||
alarmRepo repository.AlarmRepository
|
||||
}
|
||||
|
||||
// NewThresholdAlarmService 创建一个新的 ThresholdAlarmService 实例。
|
||||
func NewThresholdAlarmService(ctx context.Context, alarmService domainAlarm.AlarmService) ThresholdAlarmService {
|
||||
func NewThresholdAlarmService(ctx context.Context, alarmService domainAlarm.AlarmService, alarmRepo repository.AlarmRepository) ThresholdAlarmService {
|
||||
return &thresholdAlarmService{
|
||||
ctx: ctx,
|
||||
alarmService: alarmService,
|
||||
alarmRepo: alarmRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,3 +50,48 @@ func (s *thresholdAlarmService) CancelSnoozeThresholdAlarm(ctx context.Context,
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CancelSnoozeThresholdAlarm")
|
||||
return s.alarmService.CancelAlarmSnooze(serviceCtx, alarmID)
|
||||
}
|
||||
|
||||
// ListActiveAlarms 实现了批量查询活跃告警的逻辑。
|
||||
func (s *thresholdAlarmService) ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListActiveAlarms")
|
||||
|
||||
opts := repository.ActiveAlarmListOptions{
|
||||
SourceType: req.SourceType,
|
||||
SourceID: req.SourceID,
|
||||
Level: req.Level,
|
||||
IsIgnored: req.IsIgnored,
|
||||
TriggerTime: req.TriggerTime,
|
||||
EndTime: req.EndTime,
|
||||
OrderBy: req.OrderBy,
|
||||
}
|
||||
|
||||
alarms, total, err := s.alarmRepo.ListActiveAlarms(serviceCtx, opts, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dto.NewListActiveAlarmResponse(alarms, total, req.Page, req.PageSize), nil
|
||||
}
|
||||
|
||||
// ListHistoricalAlarms 实现了批量查询历史告警的逻辑。
|
||||
func (s *thresholdAlarmService) ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListHistoricalAlarms")
|
||||
|
||||
opts := repository.HistoricalAlarmListOptions{
|
||||
SourceType: req.SourceType,
|
||||
SourceID: req.SourceID,
|
||||
Level: req.Level,
|
||||
TriggerTimeStart: req.TriggerTimeStart,
|
||||
TriggerTimeEnd: req.TriggerTimeEnd,
|
||||
ResolveTimeStart: req.ResolveTimeStart,
|
||||
ResolveTimeEnd: req.ResolveTimeEnd,
|
||||
OrderBy: req.OrderBy,
|
||||
}
|
||||
|
||||
alarms, total, err := s.alarmRepo.ListHistoricalAlarms(serviceCtx, opts, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dto.NewListHistoricalAlarmResponse(alarms, total, req.Page, req.PageSize), nil
|
||||
}
|
||||
|
||||
@@ -269,6 +269,7 @@ func initAppServices(ctx context.Context, infra *Infrastructure, domainServices
|
||||
thresholdAlarmService := service.NewThresholdAlarmService(
|
||||
logs.AddCompName(baseCtx, "ThresholdAlarmService"),
|
||||
domainServices.alarmService,
|
||||
infra.repos.alarmRepo,
|
||||
)
|
||||
|
||||
return &AppServices{
|
||||
|
||||
@@ -23,6 +23,18 @@ type ActiveAlarmListOptions struct {
|
||||
OrderBy string // 排序字段,例如 "trigger_time DESC"
|
||||
}
|
||||
|
||||
// HistoricalAlarmListOptions 定义了查询历史告警列表时的可选参数
|
||||
type HistoricalAlarmListOptions struct {
|
||||
SourceType *models.AlarmSourceType // 按告警来源类型过滤
|
||||
SourceID *uint // 按告警来源ID过滤
|
||||
Level *models.SeverityLevel // 按告警严重性等级过滤
|
||||
TriggerTimeStart *time.Time // 告警触发时间范围 - 开始时间
|
||||
TriggerTimeEnd *time.Time // 告警触发时间范围 - 结束时间
|
||||
ResolveTimeStart *time.Time // 告警解决时间范围 - 开始时间 (对应 models.HistoricalAlarm.ResolveTime)
|
||||
ResolveTimeEnd *time.Time // 告警解决时间范围 - 结束时间 (对应 models.HistoricalAlarm.ResolveTime)
|
||||
OrderBy string // 排序字段,例如 "trigger_time DESC"
|
||||
}
|
||||
|
||||
// AlarmRepository 定义了对告警模型的数据库操作接口
|
||||
type AlarmRepository interface {
|
||||
// CreateActiveAlarm 创建一条新的活跃告警记录
|
||||
@@ -47,6 +59,10 @@ type AlarmRepository interface {
|
||||
// 返回活跃告警列表、总记录数和错误。
|
||||
ListActiveAlarms(ctx context.Context, opts ActiveAlarmListOptions, page, pageSize int) ([]models.ActiveAlarm, int64, error)
|
||||
|
||||
// ListHistoricalAlarms 支持分页和过滤的历史告警列表查询。
|
||||
// 返回历史告警列表、总记录数和错误。
|
||||
ListHistoricalAlarms(ctx context.Context, opts HistoricalAlarmListOptions, page, pageSize int) ([]models.HistoricalAlarm, int64, error)
|
||||
|
||||
// UpdateAlarmNotificationStatus 显式更新告警的通知相关状态字段。
|
||||
// lastNotifiedAt: 传入具体的发送时间。
|
||||
// isIgnored: 告警新的忽略状态。
|
||||
@@ -148,7 +164,7 @@ func (r *gormAlarmRepository) ListActiveAlarms(ctx context.Context, opts ActiveA
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListActiveAlarms")
|
||||
// --- 校验分页参数 ---
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination // 复用已定义的错误
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.ActiveAlarm
|
||||
@@ -195,6 +211,61 @@ func (r *gormAlarmRepository) ListActiveAlarms(ctx context.Context, opts ActiveA
|
||||
return results, total, err
|
||||
}
|
||||
|
||||
// ListHistoricalAlarms 实现了分页和过滤查询历史告警记录的功能
|
||||
func (r *gormAlarmRepository) ListHistoricalAlarms(ctx context.Context, opts HistoricalAlarmListOptions, page, pageSize int) ([]models.HistoricalAlarm, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListHistoricalAlarms")
|
||||
// --- 校验分页参数 ---
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.HistoricalAlarm
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(repoCtx).Model(&models.HistoricalAlarm{})
|
||||
|
||||
// --- 应用过滤条件 ---
|
||||
if opts.SourceType != nil {
|
||||
query = query.Where("source_type = ?", *opts.SourceType)
|
||||
}
|
||||
if opts.SourceID != nil {
|
||||
query = query.Where("source_id = ?", *opts.SourceID)
|
||||
}
|
||||
if opts.Level != nil {
|
||||
query = query.Where("level = ?", *opts.Level)
|
||||
}
|
||||
if opts.TriggerTimeStart != nil {
|
||||
query = query.Where("trigger_time >= ?", *opts.TriggerTimeStart)
|
||||
}
|
||||
if opts.TriggerTimeEnd != nil {
|
||||
query = query.Where("trigger_time <= ?", *opts.TriggerTimeEnd)
|
||||
}
|
||||
if opts.ResolveTimeStart != nil { // 修改字段名
|
||||
query = query.Where("resolve_time >= ?", *opts.ResolveTimeStart) // 修改查询字段名
|
||||
}
|
||||
if opts.ResolveTimeEnd != nil { // 修改字段名
|
||||
query = query.Where("resolve_time <= ?", *opts.ResolveTimeEnd) // 修改查询字段名
|
||||
}
|
||||
|
||||
// --- 计算总数 ---
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// --- 应用排序条件 ---
|
||||
orderBy := "trigger_time DESC" // 默认按触发时间倒序
|
||||
if opts.OrderBy != "" {
|
||||
orderBy = opts.OrderBy
|
||||
}
|
||||
query = query.Order(orderBy)
|
||||
|
||||
// --- 分页 ---
|
||||
offset := (page - 1) * pageSize
|
||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||
|
||||
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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user