实现忽略告警和取消忽略告警接口及功能
This commit is contained in:
109
internal/app/controller/alarm/threshold_alarm_controller.go
Normal file
109
internal/app/controller/alarm/threshold_alarm_controller.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user