- 新增 PUT /cloud-accounts/:id 后端端点(UpdateCloudAccount handler + 路由注册) - 前端 CloudAccountManager 添加编辑弹窗:复用创建表单,凭证留空保持不变 - 修复 scripts/mock.go 编译错误:补充缺失的 generateNotifyChannels 函数(5种通知渠道种子) - 包含前序未提交的告警通知渠道 CRUD 后端API + AlertManager 通知配置前端UI
153 lines
5.6 KiB
Go
153 lines
5.6 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AlertRule 告警规则模型
|
|
type AlertRule struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:128;not null" json:"name"`
|
|
Description string `gorm:"size:500" json:"description"`
|
|
Type string `gorm:"size:32;not null" json:"type"` // metric, log, event
|
|
Source string `gorm:"size:64;not null" json:"source"` // prometheus, zabbix, custom
|
|
Expression string `gorm:"type:text;not null" json:"expression"`
|
|
Severity string `gorm:"size:32;default:warning" json:"severity"` // info, warning, critical
|
|
Duration int `json:"duration"` // 持续时间(秒)
|
|
Enabled bool `gorm:"default:true" json:"enabled"`
|
|
Labels string `gorm:"type:text" json:"labels"` // JSON
|
|
Annotations string `gorm:"type:text" json:"annotations"` // JSON
|
|
NotifyChannels string `gorm:"type:text" json:"notify_channels"` // JSON
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (AlertRule) TableName() string {
|
|
return "alert_rules"
|
|
}
|
|
|
|
// AlertEvent 告警事件模型
|
|
type AlertEvent struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
RuleID uint `json:"rule_id"`
|
|
RuleName string `gorm:"size:128" json:"rule_name"`
|
|
Severity string `gorm:"size:32" json:"severity"`
|
|
Status string `gorm:"size:32;default:firing" json:"status"` // firing, resolved
|
|
Target string `gorm:"size:255" json:"target"`
|
|
Message string `gorm:"type:text" json:"message"`
|
|
Value float64 `json:"value"`
|
|
Labels string `gorm:"type:text" json:"labels"`
|
|
StartedAt time.Time `json:"started_at"`
|
|
ResolvedAt *time.Time `json:"resolved_at"`
|
|
Acknowledged bool `json:"acknowledged"`
|
|
AcknowledgedBy *uint `json:"acknowledged_by"`
|
|
AcknowledgedAt *time.Time `json:"acknowledged_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
// 关联
|
|
Rule *AlertRule `json:"rule,omitempty"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (AlertEvent) TableName() string {
|
|
return "alert_events"
|
|
}
|
|
|
|
// NotifyChannel 通知渠道模型
|
|
type NotifyChannel struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:64;not null" json:"name"`
|
|
Type string `gorm:"size:32;not null" json:"type"` // email, sms, webhook, dingtalk, wechat
|
|
Config string `gorm:"type:text" json:"config"` // JSON
|
|
Enabled bool `gorm:"default:true" json:"enabled"`
|
|
TestStatus string `gorm:"size:32" json:"test_status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (NotifyChannel) TableName() string {
|
|
return "notify_channels"
|
|
}
|
|
|
|
// MetricData 监控指标数据
|
|
type MetricData struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
ServerID *uint `json:"server_id"`
|
|
ClusterID *uint `json:"cluster_id"`
|
|
Name string `gorm:"size:64" json:"name"`
|
|
Value float64 `json:"value"`
|
|
Labels string `gorm:"type:text" json:"labels"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (MetricData) TableName() string {
|
|
return "metric_data"
|
|
}
|
|
|
|
// CreateAlertRuleRequest 创建告警规则请求
|
|
type CreateAlertRuleRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
Type string `json:"type" binding:"required,oneof=metric log event"`
|
|
Source string `json:"source" binding:"required"`
|
|
Expression string `json:"expression" binding:"required"`
|
|
Severity string `json:"severity" binding:"omitempty,oneof=info warning critical"`
|
|
Duration int `json:"duration"`
|
|
Labels string `json:"labels"`
|
|
Annotations string `json:"annotations"`
|
|
NotifyChannels []string `json:"notify_channels"`
|
|
}
|
|
|
|
// UpdateAlertRuleRequest 更新告警规则请求
|
|
type UpdateAlertRuleRequest struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Expression string `json:"expression"`
|
|
Severity string `json:"severity" binding:"omitempty,oneof=info warning critical"`
|
|
Duration int `json:"duration"`
|
|
Enabled *bool `json:"enabled"`
|
|
Labels string `json:"labels"`
|
|
Annotations string `json:"annotations"`
|
|
NotifyChannels []string `json:"notify_channels"`
|
|
}
|
|
|
|
// AlertStats 告警统计
|
|
type AlertStats struct {
|
|
Total int64 `json:"total"`
|
|
Firing int64 `json:"firing"`
|
|
Resolved int64 `json:"resolved"`
|
|
Critical int64 `json:"critical"`
|
|
Warning int64 `json:"warning"`
|
|
Info int64 `json:"info"`
|
|
TodayCount int64 `json:"today_count"`
|
|
WeekCount int64 `json:"week_count"`
|
|
}
|
|
|
|
// AcknowledgeAlertRequest 确认告警请求
|
|
type AcknowledgeAlertRequest struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// CreateNotifyChannelRequest 创建通知渠道请求
|
|
type CreateNotifyChannelRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Type string `json:"type" binding:"required,oneof=email sms webhook dingtalk wechat"`
|
|
Config string `json:"config"`
|
|
}
|
|
|
|
// UpdateNotifyChannelRequest 更新通知渠道请求
|
|
type UpdateNotifyChannelRequest struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type" binding:"omitempty,oneof=email sms webhook dingtalk wechat"`
|
|
Config string `json:"config"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|