实现忽略告警和取消忽略告警接口及功能

This commit is contained in:
2025-11-09 22:34:05 +08:00
parent 84fe20396b
commit b94aa6137c
11 changed files with 292 additions and 16 deletions

View File

@@ -19,6 +19,7 @@ import (
"time"
_ "git.huangwc.com/pig/pig-farm-controller/docs" // 引入 swag 生成的 docs
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/alarm"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/device"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/health"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/management"
@@ -53,6 +54,7 @@ type API struct {
pigBatchController *management.PigBatchController // 猪群控制器实例
monitorController *monitor.Controller // 数据监控控制器实例
healthController *health.Controller // 健康检查控制器实例
alarmController *alarm.ThresholdAlarmController // 阈值告警控制器
listenHandler webhook.ListenHandler // 设备上行事件监听器
analysisTaskManager *domain_plan.AnalysisPlanTaskManager // 计划触发器管理器实例
}
@@ -69,6 +71,7 @@ func NewAPI(cfg config.ServerConfig,
planService service.PlanService,
userService service.UserService,
auditService service.AuditService,
alarmService service.ThresholdAlarmService,
tokenGenerator token.Generator,
listenHandler webhook.ListenHandler,
) *API {
@@ -106,6 +109,8 @@ func NewAPI(cfg config.ServerConfig,
monitorController: monitor.NewController(logs.AddCompName(baseCtx, "MonitorController"), monitorService),
// 在 NewAPI 中初始化健康检查控制器
healthController: health.NewController(logs.AddCompName(baseCtx, "HealthController")),
// 在 NewAPI 中初始化阈
alarmController: alarm.NewThresholdAlarmController(logs.AddCompName(baseCtx, "ThresholdAlarmController"), alarmService),
}
api.setupRoutes() // 设置所有路由

View File

@@ -187,6 +187,17 @@ func (a *API) setupRoutes() {
monitorGroup.GET("/notifications", a.monitorController.ListNotifications)
}
logger.Debug("数据监控相关接口注册成功 (需要认证和审计)")
// 告警相关路由组
alarmGroup := authGroup.Group("/alarm")
{
thresholdGroup := alarmGroup.Group("/thresholds")
{
thresholdGroup.POST("/:id/snooze", a.alarmController.SnoozeThresholdAlarm) // 忽略阈值告警
thresholdGroup.POST("/:id/cancel-snooze", a.alarmController.CancelSnoozeThresholdAlarm) // 取消忽略阈值告警
}
}
logger.Debug("告警相关接口注册成功 (需要认证和审计)")
}
logger.Debug("所有接口注册成功")

View File

@@ -0,0 +1,109 @@
package alarm
import (
"context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"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"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
// ThresholdAlarmController 阈值告警控制器,封装了所有与阈值告警配置相关的业务逻辑
type ThresholdAlarmController struct {
ctx context.Context
thresholdAlarmService service.ThresholdAlarmService
}
// NewThresholdAlarmController 创建一个新的阈值告警控制器实例
func NewThresholdAlarmController(
ctx context.Context,
thresholdAlarmService service.ThresholdAlarmService,
) *ThresholdAlarmController {
return &ThresholdAlarmController{
ctx: ctx,
thresholdAlarmService: thresholdAlarmService,
}
}
// SnoozeThresholdAlarm godoc
// @Summary 忽略阈值告警
// @Description 根据告警ID忽略一个活跃的阈值告警或更新其忽略时间
// @Tags 告警管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path string true "告警ID"
// @Param request body dto.SnoozeAlarmRequest true "忽略告警请求体"
// @Success 200 {object} controller.Response "成功忽略告警"
// @Router /api/v1/alarm/threshold/{id}/snooze [post]
func (t *ThresholdAlarmController) SnoozeThresholdAlarm(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "SnoozeThresholdAlarm")
const actionType = "忽略阈值告警"
alarmIDStr := ctx.Param("id")
alarmID, err := strconv.ParseUint(alarmIDStr, 10, 64)
if err != nil {
logger.Errorf("%s: 无效的告警ID: %s", actionType, alarmIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的告警ID: "+alarmIDStr, actionType, "无效的告警ID", alarmIDStr)
}
var req dto.SnoozeAlarmRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
if err := t.thresholdAlarmService.SnoozeThresholdAlarm(reqCtx, uint(alarmID), req.DurationMinutes); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Warnf("%s: 告警不存在, ID: %d", actionType, alarmID)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "告警未找到", actionType, "告警不存在", alarmID)
}
logger.Errorf("%s: 服务层忽略告警失败: %v, ID: %d", actionType, err, alarmID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "忽略告警失败: "+err.Error(), actionType, "服务层忽略告警失败", alarmID)
}
logger.Infof("%s: 告警已成功忽略, ID: %d", actionType, alarmID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "告警已成功忽略", nil, actionType, "告警已成功忽略", alarmID)
}
// CancelSnoozeThresholdAlarm godoc
// @Summary 取消忽略阈值告警
// @Description 根据告警ID取消对一个阈值告警的忽略状态
// @Tags 告警管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path string true "告警ID"
// @Success 200 {object} controller.Response "成功取消忽略告警"
// @Router /api/v1/alarm/threshold/{id}/cancel-snooze [post]
func (t *ThresholdAlarmController) CancelSnoozeThresholdAlarm(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "CancelSnoozeThresholdAlarm")
const actionType = "取消忽略阈值告警"
alarmIDStr := ctx.Param("id")
alarmID, err := strconv.ParseUint(alarmIDStr, 10, 64)
if err != nil {
logger.Errorf("%s: 无效的告警ID: %s", actionType, alarmIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的告警ID: "+alarmIDStr, actionType, "无效的告警ID", alarmIDStr)
}
if err := t.thresholdAlarmService.CancelSnoozeThresholdAlarm(reqCtx, uint(alarmID)); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Warnf("%s: 告警不存在, ID: %d", actionType, alarmID)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "告警未找到", actionType, "告警不存在", alarmID)
}
logger.Errorf("%s: 服务层取消忽略告警失败: %v, ID: %d", actionType, err, alarmID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "取消忽略告警失败: "+err.Error(), actionType, "服务层取消忽略告警失败", alarmID)
}
logger.Infof("%s: 告警忽略状态已成功取消, ID: %d", actionType, alarmID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "告警忽略状态已成功取消", nil, actionType, "告警忽略状态已成功取消", alarmID)
}

View File

@@ -0,0 +1,6 @@
package dto
// SnoozeAlarmRequest 定义了忽略告警的请求体
type SnoozeAlarmRequest struct {
DurationMinutes uint `json:"duration_minutes" validate:"required,min=1"` // 忽略时长,单位分钟
}

View File

@@ -0,0 +1,44 @@
package service
import (
"context"
"time"
domainAlarm "git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm" // 引入领域层的 AlarmService
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
)
// ThresholdAlarmService 定义了阈值告警配置服务的接口。
// 该服务负责管理阈值告警任务的配置,并将其与计划进行联动。
type ThresholdAlarmService interface {
// SnoozeThresholdAlarm 忽略一个阈值告警,或更新其忽略时间。
SnoozeThresholdAlarm(ctx context.Context, alarmID uint, durationMinutes uint) error
// CancelSnoozeThresholdAlarm 取消对一个阈值告警的忽略状态。
CancelSnoozeThresholdAlarm(ctx context.Context, alarmID uint) error
}
// thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。
type thresholdAlarmService struct {
ctx context.Context
alarmService domainAlarm.AlarmService // 注入领域层的 AlarmService
}
// NewThresholdAlarmService 创建一个新的 ThresholdAlarmService 实例。
func NewThresholdAlarmService(ctx context.Context, alarmService domainAlarm.AlarmService) ThresholdAlarmService {
return &thresholdAlarmService{
ctx: ctx,
alarmService: alarmService,
}
}
// SnoozeThresholdAlarm 实现了忽略阈值告警的逻辑。
func (s *thresholdAlarmService) SnoozeThresholdAlarm(ctx context.Context, alarmID uint, durationMinutes uint) error {
serviceCtx := logs.AddFuncName(ctx, s.ctx, "SnoozeThresholdAlarm")
return s.alarmService.SnoozeAlarm(serviceCtx, alarmID, time.Duration(durationMinutes)*time.Minute)
}
// CancelSnoozeThresholdAlarm 实现了取消忽略阈值告警的逻辑。
func (s *thresholdAlarmService) CancelSnoozeThresholdAlarm(ctx context.Context, alarmID uint) error {
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CancelSnoozeThresholdAlarm")
return s.alarmService.CancelAlarmSnooze(serviceCtx, alarmID)
}