重构部分枚举, 让models包不依赖其他项目中的包
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -65,7 +66,7 @@ func (l *larkNotifier) Send(ctx context.Context, content AlarmContent, toAddr st
|
||||
"tag": "lark_md",
|
||||
"content": fmt.Sprintf("## %s\n**级别**: %s\n**时间**: %s\n\n%s",
|
||||
content.Title,
|
||||
content.Level.String(),
|
||||
content.Level,
|
||||
content.Timestamp.Format(DefaultTimeFormat),
|
||||
content.Message,
|
||||
),
|
||||
@@ -171,8 +172,8 @@ func (l *larkNotifier) getAccessToken(ctx context.Context) (string, error) {
|
||||
}
|
||||
|
||||
// Type 返回通知器的类型
|
||||
func (l *larkNotifier) Type() NotifierType {
|
||||
return NotifierTypeLark
|
||||
func (l *larkNotifier) Type() models.NotifierType {
|
||||
return models.NotifierTypeLark
|
||||
}
|
||||
|
||||
// --- API 数据结构 ---
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// logNotifier 实现了 Notifier 接口,用于将告警信息记录到日志中。
|
||||
@@ -24,10 +25,10 @@ func NewLogNotifier(ctx context.Context) Notifier {
|
||||
func (l *logNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
||||
logger := logs.TraceLogger(ctx, l.ctx, "Send")
|
||||
logger.Infow("告警已记录到日志",
|
||||
"notifierType", NotifierTypeLog,
|
||||
"notifierType", models.NotifierTypeLog,
|
||||
"title", content.Title,
|
||||
"message", content.Message,
|
||||
"level", content.Level.String(),
|
||||
"level", content.Level,
|
||||
"timestamp", content.Timestamp.Format(DefaultTimeFormat),
|
||||
"toAddr", toAddr,
|
||||
)
|
||||
@@ -35,6 +36,6 @@ func (l *logNotifier) Send(ctx context.Context, content AlarmContent, toAddr str
|
||||
}
|
||||
|
||||
// Type 返回通知器的类型。
|
||||
func (l *logNotifier) Type() NotifierType {
|
||||
return NotifierTypeLog
|
||||
func (l *logNotifier) Type() models.NotifierType {
|
||||
return models.NotifierTypeLog
|
||||
}
|
||||
|
||||
@@ -4,26 +4,12 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap/zapcore"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// DefaultTimeFormat 定义了所有通知中统一使用的时间格式。
|
||||
const DefaultTimeFormat = "2006-01-02 15:04:05"
|
||||
|
||||
// NotifierType 定义了通知器的类型。
|
||||
type NotifierType string
|
||||
|
||||
const (
|
||||
// NotifierTypeSMTP 表示 SMTP 邮件通知器。
|
||||
NotifierTypeSMTP NotifierType = "邮件"
|
||||
// NotifierTypeWeChat 表示企业微信通知器。
|
||||
NotifierTypeWeChat NotifierType = "企业微信"
|
||||
// NotifierTypeLark 表示飞书通知器。
|
||||
NotifierTypeLark NotifierType = "飞书"
|
||||
// NotifierTypeLog 表示日志通知器,作为最终的告警记录渠道。
|
||||
NotifierTypeLog NotifierType = "日志"
|
||||
)
|
||||
|
||||
// AlarmContent 定义了通知的内容
|
||||
type AlarmContent struct {
|
||||
// 通知标题
|
||||
@@ -31,7 +17,7 @@ type AlarmContent struct {
|
||||
// 通知信息
|
||||
Message string
|
||||
// 通知级别
|
||||
Level zapcore.Level
|
||||
Level models.SeverityLevel
|
||||
// 通知时间
|
||||
Timestamp time.Time
|
||||
}
|
||||
@@ -41,5 +27,5 @@ type Notifier interface {
|
||||
// Send 发送通知
|
||||
Send(ctx context.Context, content AlarmContent, toAddr string) error
|
||||
// Type 返回通知器的类型
|
||||
Type() NotifierType
|
||||
Type() models.NotifierType
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// smtpNotifier 实现了 Notifier 接口,用于通过 SMTP 发送邮件通知。
|
||||
@@ -45,7 +47,7 @@ func (s *smtpNotifier) Send(ctx context.Context, content AlarmContent, toAddr st
|
||||
|
||||
// 邮件正文
|
||||
body := fmt.Sprintf("级别: %s\n时间: %s\n\n%s",
|
||||
content.Level.String(),
|
||||
content.Level,
|
||||
content.Timestamp.Format(DefaultTimeFormat),
|
||||
content.Message,
|
||||
)
|
||||
@@ -71,6 +73,6 @@ func (s *smtpNotifier) Send(ctx context.Context, content AlarmContent, toAddr st
|
||||
}
|
||||
|
||||
// Type 返回通知器的类型
|
||||
func (s *smtpNotifier) Type() NotifierType {
|
||||
return NotifierTypeSMTP
|
||||
func (s *smtpNotifier) Type() models.NotifierType {
|
||||
return models.NotifierTypeSMTP
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -55,7 +57,7 @@ func (w *wechatNotifier) Send(ctx context.Context, content AlarmContent, toAddr
|
||||
// 2. 构建 markdown 内容
|
||||
markdownContent := fmt.Sprintf("## %s\n> 级别: <font color=\"warning\">%s</font>\n> 时间: %s\n\n%s",
|
||||
content.Title,
|
||||
content.Level.String(),
|
||||
content.Level,
|
||||
content.Timestamp.Format(DefaultTimeFormat),
|
||||
content.Message,
|
||||
)
|
||||
@@ -142,8 +144,8 @@ func (w *wechatNotifier) getAccessToken() (string, error) {
|
||||
}
|
||||
|
||||
// Type 返回通知器的类型
|
||||
func (w *wechatNotifier) Type() NotifierType {
|
||||
return NotifierTypeWeChat
|
||||
func (w *wechatNotifier) Type() models.NotifierType {
|
||||
return models.NotifierTypeWeChat
|
||||
}
|
||||
|
||||
// --- API 数据结构 ---
|
||||
|
||||
Reference in New Issue
Block a user