feat: cron task style fix
This commit is contained in:
@@ -1,18 +1,16 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"baihu/internal/constant"
|
"baihu/internal/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SendStats 任务执行统计
|
// SendStats 任务执行统计
|
||||||
type SendStats struct {
|
type SendStats struct {
|
||||||
ID uint `json:"id" gorm:"primaryKey"`
|
ID uint `json:"id" gorm:"primaryKey"`
|
||||||
TaskID uint `json:"task_id" gorm:"index"`
|
TaskID uint `json:"task_id" gorm:"uniqueIndex:idx_task_day_status"`
|
||||||
Status string `json:"status" gorm:"size:20;not null"` // success, failed
|
Day string `json:"day" gorm:"size:10;uniqueIndex:idx_task_day_status"` // 格式: 2006-01-02
|
||||||
|
Status string `json:"status" gorm:"size:20;uniqueIndex:idx_task_day_status"`
|
||||||
Num int `json:"num" gorm:"default:0"`
|
Num int `json:"num" gorm:"default:0"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (SendStats) TableName() string {
|
func (SendStats) TableName() string {
|
||||||
|
|||||||
@@ -15,19 +15,18 @@ func NewSendStatsService() *SendStatsService {
|
|||||||
|
|
||||||
// IncrementStats 增加任务执行统计
|
// IncrementStats 增加任务执行统计
|
||||||
func (s *SendStatsService) IncrementStats(taskID uint, status string) error {
|
func (s *SendStatsService) IncrementStats(taskID uint, status string) error {
|
||||||
today := time.Now().Format("2006-01-02")
|
day := time.Now().Format("2006-01-02")
|
||||||
startOfDay, _ := time.ParseInLocation("2006-01-02", today, time.Local)
|
|
||||||
|
|
||||||
var stats models.SendStats
|
var stats models.SendStats
|
||||||
result := database.DB.Where("task_id = ? AND status = ? AND created_at >= ?", taskID, status, startOfDay).First(&stats)
|
result := database.DB.Where("task_id = ? AND day = ? AND status = ?", taskID, day, status).First(&stats)
|
||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
// 不存在则创建
|
// 不存在则创建
|
||||||
stats = models.SendStats{
|
stats = models.SendStats{
|
||||||
TaskID: taskID,
|
TaskID: taskID,
|
||||||
|
Day: day,
|
||||||
Status: status,
|
Status: status,
|
||||||
Num: 1,
|
Num: 1,
|
||||||
CreatedAt: time.Now(),
|
|
||||||
}
|
}
|
||||||
return database.DB.Create(&stats).Error
|
return database.DB.Create(&stats).Error
|
||||||
}
|
}
|
||||||
@@ -39,25 +38,22 @@ func (s *SendStatsService) IncrementStats(taskID uint, status string) error {
|
|||||||
// GetStatsByTaskID 获取任务的统计数据
|
// GetStatsByTaskID 获取任务的统计数据
|
||||||
func (s *SendStatsService) GetStatsByTaskID(taskID uint) []models.SendStats {
|
func (s *SendStatsService) GetStatsByTaskID(taskID uint) []models.SendStats {
|
||||||
var stats []models.SendStats
|
var stats []models.SendStats
|
||||||
database.DB.Where("task_id = ?", taskID).Order("created_at DESC").Find(&stats)
|
database.DB.Where("task_id = ?", taskID).Order("day DESC").Find(&stats)
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTodayStats 获取今日统计
|
// GetTodayStats 获取今日统计
|
||||||
func (s *SendStatsService) GetTodayStats() []models.SendStats {
|
func (s *SendStatsService) GetTodayStats() []models.SendStats {
|
||||||
today := time.Now().Format("2006-01-02")
|
day := time.Now().Format("2006-01-02")
|
||||||
startOfDay, _ := time.ParseInLocation("2006-01-02", today, time.Local)
|
|
||||||
|
|
||||||
var stats []models.SendStats
|
var stats []models.SendStats
|
||||||
database.DB.Where("created_at >= ?", startOfDay).Find(&stats)
|
database.DB.Where("day = ?", day).Find(&stats)
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecentStats 获取最近N天的统计
|
// GetRecentStats 获取最近N天的统计
|
||||||
func (s *SendStatsService) GetRecentStats(days int) []models.SendStats {
|
func (s *SendStatsService) GetRecentStats(days int) []models.SendStats {
|
||||||
startDate := time.Now().AddDate(0, 0, -days)
|
startDay := time.Now().AddDate(0, 0, -days).Format("2006-01-02")
|
||||||
|
|
||||||
var stats []models.SendStats
|
var stats []models.SendStats
|
||||||
database.DB.Where("created_at >= ?", startDate).Order("created_at DESC").Find(&stats)
|
database.DB.Where("day >= ?", startDay).Order("day DESC").Find(&stats)
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user