2025-11-09 22:34:05 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-11-10 13:41:26 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
|
|
|
|
domainAlarm "git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm"
|
2025-11-09 22:34:05 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
2025-11-10 13:41:26 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
2025-11-09 22:34:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ThresholdAlarmService 定义了阈值告警配置服务的接口。
|
|
|
|
|
// 该服务负责管理阈值告警任务的配置,并将其与计划进行联动。
|
|
|
|
|
type ThresholdAlarmService interface {
|
|
|
|
|
// SnoozeThresholdAlarm 忽略一个阈值告警,或更新其忽略时间。
|
|
|
|
|
SnoozeThresholdAlarm(ctx context.Context, alarmID uint, durationMinutes uint) error
|
|
|
|
|
// CancelSnoozeThresholdAlarm 取消对一个阈值告警的忽略状态。
|
|
|
|
|
CancelSnoozeThresholdAlarm(ctx context.Context, alarmID uint) error
|
2025-11-10 13:41:26 +08:00
|
|
|
// ListActiveAlarms 批量查询活跃告警。
|
|
|
|
|
ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error)
|
|
|
|
|
// ListHistoricalAlarms 批量查询历史告警。
|
|
|
|
|
ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error)
|
2025-11-09 22:34:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。
|
|
|
|
|
type thresholdAlarmService struct {
|
|
|
|
|
ctx context.Context
|
2025-11-10 13:41:26 +08:00
|
|
|
alarmService domainAlarm.AlarmService
|
|
|
|
|
alarmRepo repository.AlarmRepository
|
2025-11-09 22:34:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewThresholdAlarmService 创建一个新的 ThresholdAlarmService 实例。
|
2025-11-10 13:41:26 +08:00
|
|
|
func NewThresholdAlarmService(ctx context.Context, alarmService domainAlarm.AlarmService, alarmRepo repository.AlarmRepository) ThresholdAlarmService {
|
2025-11-09 22:34:05 +08:00
|
|
|
return &thresholdAlarmService{
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
alarmService: alarmService,
|
2025-11-10 13:41:26 +08:00
|
|
|
alarmRepo: alarmRepo,
|
2025-11-09 22:34:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SnoozeThresholdAlarm 实现了忽略阈值告警的逻辑。
|
|
|
|
|
func (s *thresholdAlarmService) SnoozeThresholdAlarm(ctx context.Context, alarmID uint, durationMinutes uint) error {
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "SnoozeThresholdAlarm")
|
|
|
|
|
return s.alarmService.SnoozeAlarm(serviceCtx, alarmID, time.Duration(durationMinutes)*time.Minute)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CancelSnoozeThresholdAlarm 实现了取消忽略阈值告警的逻辑。
|
|
|
|
|
func (s *thresholdAlarmService) CancelSnoozeThresholdAlarm(ctx context.Context, alarmID uint) error {
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CancelSnoozeThresholdAlarm")
|
|
|
|
|
return s.alarmService.CancelAlarmSnooze(serviceCtx, alarmID)
|
|
|
|
|
}
|
2025-11-10 13:41:26 +08:00
|
|
|
|
|
|
|
|
// ListActiveAlarms 实现了批量查询活跃告警的逻辑。
|
|
|
|
|
func (s *thresholdAlarmService) ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error) {
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListActiveAlarms")
|
|
|
|
|
|
|
|
|
|
opts := repository.ActiveAlarmListOptions{
|
|
|
|
|
SourceType: req.SourceType,
|
|
|
|
|
SourceID: req.SourceID,
|
|
|
|
|
Level: req.Level,
|
|
|
|
|
IsIgnored: req.IsIgnored,
|
|
|
|
|
TriggerTime: req.TriggerTime,
|
|
|
|
|
EndTime: req.EndTime,
|
|
|
|
|
OrderBy: req.OrderBy,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alarms, total, err := s.alarmRepo.ListActiveAlarms(serviceCtx, opts, req.Page, req.PageSize)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dto.NewListActiveAlarmResponse(alarms, total, req.Page, req.PageSize), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListHistoricalAlarms 实现了批量查询历史告警的逻辑。
|
|
|
|
|
func (s *thresholdAlarmService) ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error) {
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListHistoricalAlarms")
|
|
|
|
|
|
|
|
|
|
opts := repository.HistoricalAlarmListOptions{
|
|
|
|
|
SourceType: req.SourceType,
|
|
|
|
|
SourceID: req.SourceID,
|
|
|
|
|
Level: req.Level,
|
|
|
|
|
TriggerTimeStart: req.TriggerTimeStart,
|
|
|
|
|
TriggerTimeEnd: req.TriggerTimeEnd,
|
|
|
|
|
ResolveTimeStart: req.ResolveTimeStart,
|
|
|
|
|
ResolveTimeEnd: req.ResolveTimeEnd,
|
|
|
|
|
OrderBy: req.OrderBy,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alarms, total, err := s.alarmRepo.ListHistoricalAlarms(serviceCtx, opts, req.Page, req.PageSize)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dto.NewListHistoricalAlarmResponse(alarms, total, req.Page, req.PageSize), nil
|
|
|
|
|
}
|