package evaluation import ( "context" "errors" "github.com/google/uuid" "gorm.io/gorm" ) var ErrEventNotFound = errors.New("事件不存在") type EventResults struct { Evaluations []Evaluation `json:"evaluations"` Alerts []AlertLink `json:"alerts"` } type AlertLink struct { ID string `json:"id"` Summary string `json:"summary"` Status string `json:"status"` Severity string `json:"severity"` Location string `json:"location"` } type Service struct{ DB *gorm.DB } func NewService(db *gorm.DB) Service { return Service{DB: db} } func (s Service) ForEvent(ctx context.Context, eventID string) (EventResults, error) { if _, err := uuid.Parse(eventID); err != nil { return EventResults{}, ErrEventNotFound } var count int64 if err := s.DB.WithContext(ctx).Table("bell_events").Where("id = ?", eventID).Count(&count).Error; err != nil { return EventResults{}, err } if count == 0 { return EventResults{}, ErrEventNotFound } result := EventResults{Evaluations: make([]Evaluation, 0), Alerts: make([]AlertLink, 0)} if err := s.DB.WithContext(ctx).Where("event_id = ?", eventID).Order("evaluated_at, rule_id").Find(&result.Evaluations).Error; err != nil { return EventResults{}, err } err := s.DB.WithContext(ctx).Table("bell_alerts a"). Select("a.id, a.summary, a.status, a.severity, a.location"). Joins("JOIN bell_alert_events ae ON ae.alert_id = a.id"). Where("ae.event_id = ?", eventID).Order("a.created_at, a.id").Scan(&result.Alerts).Error return result, err }