实现列表查询活跃告警和历史告警
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user