feat: add send_stats table
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/constant"
|
||||
"message-nest/pkg/util"
|
||||
"message-nest/service/send_message_service/unified"
|
||||
"message-nest/service/send_task_service"
|
||||
"message-nest/service/send_way_service"
|
||||
@@ -294,6 +295,8 @@ func (sm *SendMessageService) Send(task models.TaskIns) (string, error) {
|
||||
sm.AppendSendContent()
|
||||
// 日志写到数据库
|
||||
sm.RecordSendLog()
|
||||
// 更新统计数据(任务级别:一次任务算一次)
|
||||
sm.UpdateSendStats()
|
||||
|
||||
totalOutputLog := strings.Join(sm.LogOutput, "\n")
|
||||
if sm.Status == SendSuccess {
|
||||
@@ -339,6 +342,49 @@ func (sm *SendMessageService) RecordSendLog() {
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateSendStats 更新发送统计数据(任务级别)
|
||||
func (sm *SendMessageService) UpdateSendStats() {
|
||||
// 获取当前日期
|
||||
currentDay := sm.getCurrentDay()
|
||||
|
||||
// 解析任务ID(如果是数字ID)
|
||||
var taskID *uint
|
||||
if sm.TaskID != "" {
|
||||
// 尝试将字符串ID转换为uint(如果是数字ID)
|
||||
var id uint64
|
||||
_, err := fmt.Sscanf(sm.TaskID, "%d", &id)
|
||||
if err == nil {
|
||||
taskIDValue := uint(id)
|
||||
taskID = &taskIDValue
|
||||
}
|
||||
}
|
||||
|
||||
// 确定任务类型
|
||||
taskType := "task"
|
||||
if sm.SendMode == SendModeTemplate {
|
||||
taskType = "template"
|
||||
}
|
||||
|
||||
// 根据任务的最终状态更新统计(一次任务只记录一次)
|
||||
var status string
|
||||
if sm.Status == SendSuccess {
|
||||
status = "success"
|
||||
} else {
|
||||
status = "failed"
|
||||
}
|
||||
|
||||
// 更新统计:每次任务执行记录为1次
|
||||
err := models.IncrementSendStats(taskID, taskType, currentDay, status, 1)
|
||||
if err != nil {
|
||||
logrus.Errorf("更新发送统计失败:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// getCurrentDay 获取当前日期(YYYY-MM-DD格式)
|
||||
func (sm *SendMessageService) getCurrentDay() string {
|
||||
return util.GetNowTimeStr()[:10]
|
||||
}
|
||||
|
||||
// TransError 转化错误
|
||||
func (sm *SendMessageService) TransError(err string) string {
|
||||
if err == "" {
|
||||
|
||||
@@ -2,9 +2,12 @@ package statistic_service
|
||||
|
||||
import (
|
||||
"message-nest/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type StatisticService struct {
|
||||
TaskID string
|
||||
Days int
|
||||
}
|
||||
|
||||
func (sw *StatisticService) GetStatisticData() (models.StatisticData, error) {
|
||||
@@ -25,3 +28,27 @@ func (sw *StatisticService) GetTrendStatisticData() (models.TrendStatisticData,
|
||||
func (sw *StatisticService) GetChannelStatisticData() (models.ChannelStatisticData, error) {
|
||||
return models.GetChannelStatisticData()
|
||||
}
|
||||
|
||||
// GetSendStatsData 获取发送统计数据(基于 send_stats 表)
|
||||
func (sw *StatisticService) GetSendStatsData() (models.SendStatsData, error) {
|
||||
days := sw.Days
|
||||
if days <= 0 {
|
||||
days = 30 // 默认30天
|
||||
}
|
||||
return models.GetSendStatsData(days)
|
||||
}
|
||||
|
||||
// GetSendStatsByTask 获取指定任务的发送统计数据
|
||||
func (sw *StatisticService) GetSendStatsByTask() (models.SendStatsData, error) {
|
||||
days := sw.Days
|
||||
if days <= 0 {
|
||||
days = 30 // 默认30天
|
||||
}
|
||||
|
||||
taskID, err := strconv.ParseUint(sw.TaskID, 10, 64)
|
||||
if err != nil {
|
||||
return models.SendStatsData{}, err
|
||||
}
|
||||
|
||||
return models.GetSendStatsByTask(uint(taskID), days)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user