GetDeviceThresholdAlarm
GetAreaThresholdAlarm
This commit is contained in:
@@ -113,3 +113,23 @@ type UpdateAreaThresholdAlarmDTO struct {
|
|||||||
Operator models.Operator `json:"operator" binding:"required"` // 新的操作符
|
Operator models.Operator `json:"operator" binding:"required"` // 新的操作符
|
||||||
Level models.SeverityLevel `json:"level,omitempty"` // 新的告警等级,可选
|
Level models.SeverityLevel `json:"level,omitempty"` // 新的告警等级,可选
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AreaThresholdAlarmDTO 用于表示一个区域阈值告警任务的详细信息
|
||||||
|
type AreaThresholdAlarmDTO struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
AreaControllerID uint `json:"area_controller_id"`
|
||||||
|
SensorType models.SensorType `json:"sensor_type"`
|
||||||
|
Thresholds float64 `json:"thresholds"`
|
||||||
|
Operator models.Operator `json:"operator"`
|
||||||
|
Level models.SeverityLevel `json:"level"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeviceThresholdAlarmDTO 用于表示一个设备阈值告警任务的详细信息
|
||||||
|
type DeviceThresholdAlarmDTO struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
DeviceID uint `json:"device_id"`
|
||||||
|
SensorType models.SensorType `json:"sensor_type"`
|
||||||
|
Thresholds float64 `json:"thresholds"`
|
||||||
|
Operator models.Operator `json:"operator"`
|
||||||
|
Level models.SeverityLevel `json:"level"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,14 +25,19 @@ type ThresholdAlarmService interface {
|
|||||||
ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error)
|
ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error)
|
||||||
// ListHistoricalAlarms 批量查询历史告警。
|
// ListHistoricalAlarms 批量查询历史告警。
|
||||||
ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error)
|
ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error)
|
||||||
|
|
||||||
// CreateDeviceThresholdAlarm 创建一个设备阈值告警。
|
// CreateDeviceThresholdAlarm 创建一个设备阈值告警。
|
||||||
CreateDeviceThresholdAlarm(ctx context.Context, req *dto.CreateDeviceThresholdAlarmDTO) error
|
CreateDeviceThresholdAlarm(ctx context.Context, req *dto.CreateDeviceThresholdAlarmDTO) error
|
||||||
// UpdateDeviceThresholdAlarm 更新一个设备阈值告警。
|
// UpdateDeviceThresholdAlarm 更新一个设备阈值告警。
|
||||||
UpdateDeviceThresholdAlarm(ctx context.Context, taskID int, req *dto.UpdateDeviceThresholdAlarmDTO) error
|
UpdateDeviceThresholdAlarm(ctx context.Context, taskID int, req *dto.UpdateDeviceThresholdAlarmDTO) error
|
||||||
|
// GetDeviceThresholdAlarm 根据ID获取一个设备阈值告警任务。
|
||||||
|
GetDeviceThresholdAlarm(ctx context.Context, taskID int) (*dto.DeviceThresholdAlarmDTO, error)
|
||||||
// CreateAreaThresholdAlarm 创建一个区域阈值告警。
|
// CreateAreaThresholdAlarm 创建一个区域阈值告警。
|
||||||
CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error
|
CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error
|
||||||
// UpdateAreaThresholdAlarm 更新一个区域阈值告警。
|
// UpdateAreaThresholdAlarm 更新一个区域阈值告警。
|
||||||
UpdateAreaThresholdAlarm(ctx context.Context, taskID int, req *dto.UpdateAreaThresholdAlarmDTO) error
|
UpdateAreaThresholdAlarm(ctx context.Context, taskID int, req *dto.UpdateAreaThresholdAlarmDTO) error
|
||||||
|
// GetAreaThresholdAlarm 根据ID获取一个区域阈值告警任务。
|
||||||
|
GetAreaThresholdAlarm(ctx context.Context, taskID int) (*dto.AreaThresholdAlarmDTO, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。
|
// thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。
|
||||||
@@ -254,6 +259,38 @@ func (s *thresholdAlarmService) UpdateDeviceThresholdAlarm(ctx context.Context,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDeviceThresholdAlarm 实现了根据ID获取一个设备阈值告警任务的逻辑。
|
||||||
|
func (s *thresholdAlarmService) GetDeviceThresholdAlarm(ctx context.Context, taskID int) (*dto.DeviceThresholdAlarmDTO, error) {
|
||||||
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetDeviceThresholdAlarm")
|
||||||
|
|
||||||
|
// 1. 使用 planRepo 查询任务
|
||||||
|
t, err := s.planRepo.FindTaskByID(serviceCtx, taskID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err // 如果未找到或发生其他错误,直接返回
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 验证任务类型是否正确
|
||||||
|
if t.Type != models.TaskTypeDeviceThresholdCheck {
|
||||||
|
return nil, fmt.Errorf("任务 %d 不是一个设备阈值检查任务", taskID)
|
||||||
|
}
|
||||||
|
var params task.DeviceThresholdCheckParams
|
||||||
|
err = t.ParseParameters(¶ms)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("任务 %d: 解析参数失败: %w", taskID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := &dto.DeviceThresholdAlarmDTO{
|
||||||
|
ID: t.ID,
|
||||||
|
DeviceID: params.DeviceID,
|
||||||
|
SensorType: params.SensorType,
|
||||||
|
Thresholds: params.Thresholds,
|
||||||
|
Operator: params.Operator,
|
||||||
|
Level: params.Level,
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CreateAreaThresholdAlarm 实现了创建一个区域阈值告警的逻辑。
|
// CreateAreaThresholdAlarm 实现了创建一个区域阈值告警的逻辑。
|
||||||
func (s *thresholdAlarmService) CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error {
|
func (s *thresholdAlarmService) CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error {
|
||||||
serviceCtx, logger := logs.Trace(ctx, s.ctx, "CreateAreaThresholdAlarm")
|
serviceCtx, logger := logs.Trace(ctx, s.ctx, "CreateAreaThresholdAlarm")
|
||||||
@@ -374,3 +411,34 @@ func (s *thresholdAlarmService) UpdateAreaThresholdAlarm(ctx context.Context, ta
|
|||||||
_, err = s.planService.UpdatePlan(serviceCtx, plan, models.PlanTypeSystem)
|
_, err = s.planService.UpdatePlan(serviceCtx, plan, models.PlanTypeSystem)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAreaThresholdAlarm 实现了根据ID获取一个区域阈值告警任务的逻辑。
|
||||||
|
func (s *thresholdAlarmService) GetAreaThresholdAlarm(ctx context.Context, taskID int) (*dto.AreaThresholdAlarmDTO, error) {
|
||||||
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetAreaThresholdAlarm")
|
||||||
|
|
||||||
|
// 1. 使用 planRepo 查询任务
|
||||||
|
t, err := s.planRepo.FindTaskByID(serviceCtx, taskID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err // 如果未找到或发生其他错误,直接返回
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 验证任务类型是否正确
|
||||||
|
if t.Type != models.TaskTypeAreaCollectorThresholdCheck {
|
||||||
|
return nil, fmt.Errorf("任务 %d 不是一个区域阈值检查任务", taskID)
|
||||||
|
}
|
||||||
|
var params task.AreaThresholdCheckParams
|
||||||
|
err = t.ParseParameters(¶ms)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("任务 %d: 解析参数失败: %w", taskID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := &dto.AreaThresholdAlarmDTO{
|
||||||
|
ID: t.ID,
|
||||||
|
AreaControllerID: params.AreaControllerID,
|
||||||
|
SensorType: params.SensorType,
|
||||||
|
Thresholds: params.Thresholds,
|
||||||
|
Operator: params.Operator,
|
||||||
|
Level: params.Level,
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -69,25 +69,22 @@ type PlanRepository interface {
|
|||||||
FlattenPlanTasks(ctx context.Context, planID uint) ([]models.Task, error)
|
FlattenPlanTasks(ctx context.Context, planID uint) ([]models.Task, error)
|
||||||
// DeleteTask 根据ID删除任务
|
// DeleteTask 根据ID删除任务
|
||||||
DeleteTask(ctx context.Context, id int) error
|
DeleteTask(ctx context.Context, id int) error
|
||||||
|
// FindTaskByID 根据ID获取任务的基本信息
|
||||||
|
FindTaskByID(ctx context.Context, id int) (*models.Task, error)
|
||||||
// FindPlanAnalysisTaskByParamsPlanID 根据Parameters中的ParamsPlanID字段值查找TaskPlanAnalysis类型的Task
|
// FindPlanAnalysisTaskByParamsPlanID 根据Parameters中的ParamsPlanID字段值查找TaskPlanAnalysis类型的Task
|
||||||
FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint) (*models.Task, error)
|
FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint) (*models.Task, error)
|
||||||
// FindRunnablePlans 获取所有应执行的计划
|
// FindRunnablePlans 获取所有应执行的计划
|
||||||
FindRunnablePlans(ctx context.Context) ([]*models.Plan, error)
|
FindRunnablePlans(ctx context.Context) ([]*models.Plan, error)
|
||||||
// FindInactivePlans 获取所有已禁用或已停止的计划
|
// FindInactivePlans 获取所有已禁用或已停止的计划
|
||||||
FindInactivePlans(ctx context.Context) ([]*models.Plan, error)
|
FindInactivePlans(ctx context.Context) ([]*models.Plan, error)
|
||||||
|
|
||||||
// FindPlanAnalysisTaskByPlanID 根据 PlanID 找到其关联的 'plan_analysis' 任务
|
// FindPlanAnalysisTaskByPlanID 根据 PlanID 找到其关联的 'plan_analysis' 任务
|
||||||
FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint) (*models.Task, error)
|
FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint) (*models.Task, error)
|
||||||
|
|
||||||
// CreatePlanAnalysisTask 创建一个 plan_analysis 类型的任务并返回它
|
// CreatePlanAnalysisTask 创建一个 plan_analysis 类型的任务并返回它
|
||||||
CreatePlanAnalysisTask(ctx context.Context, plan *models.Plan) (*models.Task, error)
|
CreatePlanAnalysisTask(ctx context.Context, plan *models.Plan) (*models.Task, error)
|
||||||
|
|
||||||
// FindPlansWithPendingTasks 查找所有正在执行的计划
|
// FindPlansWithPendingTasks 查找所有正在执行的计划
|
||||||
FindPlansWithPendingTasks(ctx context.Context) ([]*models.Plan, error)
|
FindPlansWithPendingTasks(ctx context.Context) ([]*models.Plan, error)
|
||||||
|
|
||||||
// StopPlanTransactionally 停止一个计划的执行,包括更新状态、移除待执行任务和更新执行日志
|
// StopPlanTransactionally 停止一个计划的执行,包括更新状态、移除待执行任务和更新执行日志
|
||||||
StopPlanTransactionally(ctx context.Context, planID uint) error
|
StopPlanTransactionally(ctx context.Context, planID uint) error
|
||||||
|
|
||||||
// UpdatePlanStateAfterExecution 更新计划执行后的状态(计数和状态)
|
// UpdatePlanStateAfterExecution 更新计划执行后的状态(计数和状态)
|
||||||
UpdatePlanStateAfterExecution(ctx context.Context, planID uint, newCount uint, newStatus models.PlanStatus) error
|
UpdatePlanStateAfterExecution(ctx context.Context, planID uint, newCount uint, newStatus models.PlanStatus) error
|
||||||
}
|
}
|
||||||
@@ -870,3 +867,14 @@ func (r *gormPlanRepository) UpdateExecuteCount(ctx context.Context, id uint, co
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindTaskByID 根据ID获取任务的基本信息
|
||||||
|
func (r *gormPlanRepository) FindTaskByID(ctx context.Context, id int) (*models.Task, error) {
|
||||||
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTaskByID")
|
||||||
|
var task models.Task
|
||||||
|
result := r.db.WithContext(repoCtx).First(&task, id)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return &task, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user