181 lines
8.8 KiB
Go
181 lines
8.8 KiB
Go
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"
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||
|
||
"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)
|
||
}
|
||
|
||
// 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)
|
||
}
|