实现任务11应的八个web接口
This commit is contained in:
@@ -197,6 +197,19 @@ func (a *API) setupRoutes() {
|
||||
thresholdGroup.POST("/:id/cancel-snooze", a.alarmController.CancelSnoozeThresholdAlarm) // 取消忽略阈值告警
|
||||
thresholdGroup.GET("/active-alarms", a.alarmController.ListActiveAlarms) // 获取活跃告警
|
||||
thresholdGroup.GET("/historical-alarms", a.alarmController.ListHistoricalAlarms) // 获取历史告警
|
||||
|
||||
// 设备阈值告警配置
|
||||
thresholdGroup.POST("/device", a.alarmController.CreateDeviceThresholdAlarm)
|
||||
thresholdGroup.GET("/device/:task_id", a.alarmController.GetDeviceThresholdAlarm)
|
||||
thresholdGroup.PUT("/device/:task_id", a.alarmController.UpdateDeviceThresholdAlarm)
|
||||
thresholdGroup.DELETE("/device/:task_id", a.alarmController.DeleteDeviceThresholdAlarm)
|
||||
|
||||
// 区域阈值告警配置
|
||||
thresholdGroup.POST("/area", a.alarmController.CreateAreaThresholdAlarm)
|
||||
thresholdGroup.GET("/area/:task_id", a.alarmController.GetAreaThresholdAlarm)
|
||||
thresholdGroup.PUT("/area/:task_id", a.alarmController.UpdateAreaThresholdAlarm)
|
||||
thresholdGroup.DELETE("/area/:task_id", a.alarmController.DeleteAreaThresholdAlarm)
|
||||
|
||||
}
|
||||
}
|
||||
logger.Debug("告警相关接口注册成功 (需要认证和审计)")
|
||||
|
||||
@@ -178,3 +178,279 @@ func (t *ThresholdAlarmController) ListHistoricalAlarms(ctx echo.Context) error
|
||||
logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "成功获取历史告警列表", resp, actionType, "成功获取历史告警列表", req)
|
||||
}
|
||||
|
||||
// CreateDeviceThresholdAlarm godoc
|
||||
// @Summary 创建设备阈值告警
|
||||
// @Description 为单个设备创建一条新的阈值告警规则
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body dto.CreateDeviceThresholdAlarmDTO true "创建设备阈值告警请求体"
|
||||
// @Success 200 {object} controller.Response "成功创建设备阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/device [post]
|
||||
func (t *ThresholdAlarmController) CreateDeviceThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "CreateDeviceThresholdAlarm")
|
||||
const actionType = "创建设备阈值告警"
|
||||
|
||||
var req dto.CreateDeviceThresholdAlarmDTO
|
||||
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.CreateDeviceThresholdAlarm(reqCtx, &req); err != nil {
|
||||
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, DeviceID: %d, SensorType: %s", actionType, req.DeviceID, req.SensorType)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "创建成功", nil, actionType, "创建成功", req)
|
||||
}
|
||||
|
||||
// GetDeviceThresholdAlarm godoc
|
||||
// @Summary 获取设备阈值告警
|
||||
// @Description 根据任务ID获取单个设备阈值告警规则的详细信息
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Produce json
|
||||
// @Param task_id path int true "任务ID"
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceThresholdAlarmDTO} "成功获取设备阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/device/{task_id} [get]
|
||||
func (t *ThresholdAlarmController) GetDeviceThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "GetDeviceThresholdAlarm")
|
||||
const actionType = "获取设备阈值告警"
|
||||
|
||||
taskID, err := strconv.Atoi(ctx.Param("task_id"))
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id"))
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id"))
|
||||
}
|
||||
|
||||
resp, err := t.thresholdAlarmService.GetDeviceThresholdAlarm(reqCtx, taskID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层获取失败: %v, ID: %d", actionType, err, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取失败: "+err.Error(), actionType, "服务层获取失败", taskID)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, ID: %d", actionType, taskID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, actionType, "获取成功", taskID)
|
||||
}
|
||||
|
||||
// UpdateDeviceThresholdAlarm godoc
|
||||
// @Summary 更新设备阈值告警
|
||||
// @Description 根据任务ID更新已存在的设备阈值告警规则
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param task_id path int true "任务ID"
|
||||
// @Param request body dto.UpdateDeviceThresholdAlarmDTO true "更新设备阈值告警请求体"
|
||||
// @Success 200 {object} controller.Response "成功更新设备阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/device/{task_id} [put]
|
||||
func (t *ThresholdAlarmController) UpdateDeviceThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "UpdateDeviceThresholdAlarm")
|
||||
const actionType = "更新设备阈值告警"
|
||||
|
||||
taskID, err := strconv.Atoi(ctx.Param("task_id"))
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id"))
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id"))
|
||||
}
|
||||
|
||||
var req dto.UpdateDeviceThresholdAlarmDTO
|
||||
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.UpdateDeviceThresholdAlarm(reqCtx, taskID, &req); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层更新失败: %v, ID: %d", actionType, err, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败: "+err.Error(), actionType, "服务层更新失败", taskID)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, ID: %d", actionType, taskID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", nil, actionType, "更新成功", taskID)
|
||||
}
|
||||
|
||||
// DeleteDeviceThresholdAlarm godoc
|
||||
// @Summary 删除设备阈值告警
|
||||
// @Description 根据任务ID删除设备阈值告警规则
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param task_id path int true "任务ID"
|
||||
// @Param request body dto.DeleteDeviceThresholdAlarmDTO true "删除设备阈值告警请求体"
|
||||
// @Success 200 {object} controller.Response "成功删除设备阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/device/{task_id} [delete]
|
||||
func (t *ThresholdAlarmController) DeleteDeviceThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "DeleteDeviceThresholdAlarm")
|
||||
const actionType = "删除设备阈值告警"
|
||||
|
||||
taskID, err := strconv.Atoi(ctx.Param("task_id"))
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id"))
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id"))
|
||||
}
|
||||
|
||||
var req dto.DeleteDeviceThresholdAlarmDTO
|
||||
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.DeleteDeviceThresholdAlarm(reqCtx, taskID, &req); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层删除失败: %v, ID: %d", actionType, err, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败: "+err.Error(), actionType, "服务层删除失败", taskID)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, ID: %d", actionType, taskID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, actionType, "删除成功", taskID)
|
||||
}
|
||||
|
||||
// CreateAreaThresholdAlarm godoc
|
||||
// @Summary 创建区域阈值告警
|
||||
// @Description 为指定的区域主控创建一个新的阈值告警规则
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body dto.CreateAreaThresholdAlarmDTO true "创建区域阈值告警请求体"
|
||||
// @Success 200 {object} controller.Response "成功创建区域阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/area [post]
|
||||
func (t *ThresholdAlarmController) CreateAreaThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "CreateAreaThresholdAlarm")
|
||||
const actionType = "创建区域阈值告警"
|
||||
|
||||
var req dto.CreateAreaThresholdAlarmDTO
|
||||
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.CreateAreaThresholdAlarm(reqCtx, &req); err != nil {
|
||||
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, AreaControllerID: %d, SensorType: %s", actionType, req.AreaControllerID, req.SensorType)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "创建成功", nil, actionType, "创建成功", req)
|
||||
}
|
||||
|
||||
// GetAreaThresholdAlarm godoc
|
||||
// @Summary 获取区域阈值告警
|
||||
// @Description 根据任务ID获取单个区域阈值告警规则的详细信息
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Produce json
|
||||
// @Param task_id path int true "任务ID"
|
||||
// @Success 200 {object} controller.Response{data=dto.AreaThresholdAlarmDTO} "成功获取区域阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/area/{task_id} [get]
|
||||
func (t *ThresholdAlarmController) GetAreaThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "GetAreaThresholdAlarm")
|
||||
const actionType = "获取区域阈值告警"
|
||||
|
||||
taskID, err := strconv.Atoi(ctx.Param("task_id"))
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id"))
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id"))
|
||||
}
|
||||
|
||||
resp, err := t.thresholdAlarmService.GetAreaThresholdAlarm(reqCtx, taskID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层获取失败: %v, ID: %d", actionType, err, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取失败: "+err.Error(), actionType, "服务层获取失败", taskID)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, ID: %d", actionType, taskID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, actionType, "获取成功", taskID)
|
||||
}
|
||||
|
||||
// UpdateAreaThresholdAlarm godoc
|
||||
// @Summary 更新区域阈值告警
|
||||
// @Description 根据任务ID更新已存在的区域阈值告警规则
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param task_id path int true "任务ID"
|
||||
// @Param request body dto.UpdateAreaThresholdAlarmDTO true "更新区域阈值告警请求体"
|
||||
// @Success 200 {object} controller.Response "成功更新区域阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/area/{task_id} [put]
|
||||
func (t *ThresholdAlarmController) UpdateAreaThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "UpdateAreaThresholdAlarm")
|
||||
const actionType = "更新区域阈值告警"
|
||||
|
||||
taskID, err := strconv.Atoi(ctx.Param("task_id"))
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id"))
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id"))
|
||||
}
|
||||
|
||||
var req dto.UpdateAreaThresholdAlarmDTO
|
||||
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.UpdateAreaThresholdAlarm(reqCtx, taskID, &req); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层更新失败: %v, ID: %d", actionType, err, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败: "+err.Error(), actionType, "服务层更新失败", taskID)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, ID: %d", actionType, taskID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", nil, actionType, "更新成功", taskID)
|
||||
}
|
||||
|
||||
// DeleteAreaThresholdAlarm godoc
|
||||
// @Summary 删除区域阈值告警
|
||||
// @Description 根据任务ID删除区域阈值告警规则
|
||||
// @Tags 告警管理
|
||||
// @Security BearerAuth
|
||||
// @Produce json
|
||||
// @Param task_id path int true "任务ID"
|
||||
// @Success 200 {object} controller.Response "成功删除区域阈值告警"
|
||||
// @Router /api/v1/alarm/threshold/area/{task_id} [delete]
|
||||
func (t *ThresholdAlarmController) DeleteAreaThresholdAlarm(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "DeleteAreaThresholdAlarm")
|
||||
const actionType = "删除区域阈值告警"
|
||||
|
||||
taskID, err := strconv.Atoi(ctx.Param("task_id"))
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id"))
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id"))
|
||||
}
|
||||
|
||||
if err := t.thresholdAlarmService.DeleteAreaThresholdAlarm(reqCtx, taskID); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层删除失败: %v, ID: %d", actionType, err, taskID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败: "+err.Error(), actionType, "服务层删除失败", taskID)
|
||||
}
|
||||
|
||||
logger.Infof("%s: 成功, ID: %d", actionType, taskID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, actionType, "删除成功", taskID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user