mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): add alert system with CRUD endpoints and statistics
Introduce a comprehensive alert management system for monitoring system events and notifications. Changes include: - Add Alert model with type, severity, status, and metadata fields - Implement AlertHandler with full CRUD operations (create, list, get, acknowledge, resolve, dismiss) - Add alert statistics endpoint for counts by status and severity - Register Alert model in database auto-migration - Add minute-level aggregation to log stats (limited to 6-hour range)
This commit is contained in:
56
internal/model/alert.go
Normal file
56
internal/model/alert.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AlertType defines the type of alert
|
||||
type AlertType string
|
||||
|
||||
const (
|
||||
AlertTypeRateLimit AlertType = "rate_limit"
|
||||
AlertTypeErrorSpike AlertType = "error_spike"
|
||||
AlertTypeQuotaExceeded AlertType = "quota_exceeded"
|
||||
AlertTypeKeyDisabled AlertType = "key_disabled"
|
||||
AlertTypeKeyExpired AlertType = "key_expired"
|
||||
AlertTypeProviderDown AlertType = "provider_down"
|
||||
)
|
||||
|
||||
// AlertSeverity defines the severity level of an alert
|
||||
type AlertSeverity string
|
||||
|
||||
const (
|
||||
AlertSeverityInfo AlertSeverity = "info"
|
||||
AlertSeverityWarning AlertSeverity = "warning"
|
||||
AlertSeverityCritical AlertSeverity = "critical"
|
||||
)
|
||||
|
||||
// AlertStatus defines the status of an alert
|
||||
type AlertStatus string
|
||||
|
||||
const (
|
||||
AlertStatusActive AlertStatus = "active"
|
||||
AlertStatusAcknowledged AlertStatus = "acknowledged"
|
||||
AlertStatusResolved AlertStatus = "resolved"
|
||||
AlertStatusDismissed AlertStatus = "dismissed"
|
||||
)
|
||||
|
||||
// Alert represents a system alert or notification
|
||||
type Alert struct {
|
||||
gorm.Model
|
||||
Type AlertType `gorm:"size:50;not null;index" json:"type"`
|
||||
Severity AlertSeverity `gorm:"size:20;not null;index" json:"severity"`
|
||||
Status AlertStatus `gorm:"size:20;not null;default:'active';index" json:"status"`
|
||||
Title string `gorm:"size:255;not null" json:"title"`
|
||||
Message string `gorm:"type:text" json:"message"`
|
||||
RelatedID uint `gorm:"index" json:"related_id,omitempty"`
|
||||
RelatedType string `gorm:"size:50" json:"related_type,omitempty"` // master, key, apikey, provider_group
|
||||
RelatedName string `gorm:"size:255" json:"related_name,omitempty"`
|
||||
Metadata string `gorm:"type:text" json:"metadata,omitempty"` // JSON encoded additional data
|
||||
AckedAt *time.Time `json:"acked_at,omitempty"`
|
||||
AckedBy string `gorm:"size:100" json:"acked_by,omitempty"`
|
||||
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
|
||||
ExpiresAt *time.Time `gorm:"index" json:"expires_at,omitempty"` // Auto-dismiss after this time
|
||||
}
|
||||
Reference in New Issue
Block a user