Files
ez-api/internal/model/alert.go
zenfun bfba16bbd4 feat(api): add internal alerts reporting endpoint with deduplication
Add ReportAlerts endpoint for Data Plane to report alerts to Control Plane
with fingerprint-based deduplication using a 5-minute cooldown period.

Changes:
- Add POST /internal/alerts/report endpoint with validation
- Add Fingerprint field to Alert model for deduplication
- Extend GetAPIKeyStatsSummary with optional time range filtering
  using since/until query parameters to query from log records
2025-12-31 14:18:09 +08:00

58 lines
2.1 KiB
Go

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"`
Fingerprint string `gorm:"size:255;index" json:"fingerprint,omitempty"` // For deduplication: type:related_type:related_id
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
}