feat: enhance email notifications with HTML templates and improved content

This commit is contained in:
JustSong
2025-02-02 16:39:30 +08:00
parent 19998ebb8a
commit 8fa9df5b3c
4 changed files with 113 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ package monitor
import (
"fmt"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/message"
@@ -30,17 +31,32 @@ func notifyRootUser(subject string, content string) {
func DisableChannel(channelId int, channelName string, reason string) {
model.UpdateChannelStatusById(channelId, model.ChannelStatusAutoDisabled)
logger.SysLog(fmt.Sprintf("channel #%d has been disabled: %s", channelId, reason))
subject := fmt.Sprintf("渠道「%s」#%d已被禁用", channelName, channelId)
content := fmt.Sprintf("渠道「%s」#%d已被禁用原因%s", channelName, channelId, reason)
subject := fmt.Sprintf("渠道状态变更提醒")
content := message.EmailTemplate(
subject,
fmt.Sprintf(`
<p>您好!</p>
<p>渠道「<strong>%s</strong>」(#%d已被禁用。</p>
<p>禁用原因:</p>
<p style="background-color: #f8f8f8; padding: 10px; border-radius: 4px;">%s</p>
`, channelName, channelId, reason),
)
notifyRootUser(subject, content)
}
func MetricDisableChannel(channelId int, successRate float64) {
model.UpdateChannelStatusById(channelId, model.ChannelStatusAutoDisabled)
logger.SysLog(fmt.Sprintf("channel #%d has been disabled due to low success rate: %.2f", channelId, successRate*100))
subject := fmt.Sprintf("渠道 #%d 已被禁用", channelId)
content := fmt.Sprintf("该渠道(#%d在最近 %d 次调用中成功率为 %.2f%%,低于阈值 %.2f%%,因此被系统自动禁用。",
channelId, config.MetricQueueSize, successRate*100, config.MetricSuccessRateThreshold*100)
subject := fmt.Sprintf("渠道状态变更提醒")
content := message.EmailTemplate(
subject,
fmt.Sprintf(`
<p>您好!</p>
<p>渠道 #%d 已被系统自动禁用。</p>
<p>禁用原因:</p>
<p style="background-color: #f8f8f8; padding: 10px; border-radius: 4px;">该渠道在最近 %d 次调用中成功率为 <strong>%.2f%%</strong>,低于系统阈值 <strong>%.2f%%</strong>。</p>
`, channelId, config.MetricQueueSize, successRate*100, config.MetricSuccessRateThreshold*100),
)
notifyRootUser(subject, content)
}
@@ -48,7 +64,14 @@ func MetricDisableChannel(channelId int, successRate float64) {
func EnableChannel(channelId int, channelName string) {
model.UpdateChannelStatusById(channelId, model.ChannelStatusEnabled)
logger.SysLog(fmt.Sprintf("channel #%d has been enabled", channelId))
subject := fmt.Sprintf("渠道「%s」#%d已被启用", channelName, channelId)
content := fmt.Sprintf("渠道「%s」#%d已被启用", channelName, channelId)
subject := fmt.Sprintf("渠道状态变更提醒")
content := message.EmailTemplate(
subject,
fmt.Sprintf(`
<p>您好!</p>
<p>渠道「<strong>%s</strong>」(#%d已被重新启用。</p>
<p>您现在可以继续使用该渠道了。</p>
`, channelName, channelId),
)
notifyRootUser(subject, content)
}