2023-12-30 17:40:20 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"message-nest/pkg/util"
|
2024-04-29 17:31:13 +08:00
|
|
|
//"time"
|
2023-12-30 17:40:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SendTasksLogs struct {
|
2024-04-29 17:31:13 +08:00
|
|
|
ID int `gorm:"primaryKey" json:"id" `
|
|
|
|
|
TaskID string `json:"task_id" gorm:"type:varchar(12) ;default:'';index:task_id"`
|
|
|
|
|
Log string `json:"log" gorm:"type:text ;"`
|
|
|
|
|
Status *int `json:"status" gorm:"type:int ;default:0;"`
|
|
|
|
|
CallerIp string `json:"caller_ip" gorm:"type:varchar(256) ;default:'';"`
|
2023-12-30 17:40:20 +08:00
|
|
|
|
2024-04-29 17:31:13 +08:00
|
|
|
CreatedAt util.Time `json:"created_on" gorm:"column:created_on;autoCreateTime "`
|
|
|
|
|
UpdatedAt util.Time `json:"modified_on" gorm:"column:modified_on;autoUpdateTime ;"`
|
2023-12-30 17:40:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add 添加日志记录
|
|
|
|
|
func (log *SendTasksLogs) Add() error {
|
|
|
|
|
if err := db.Create(&log).Error; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 日志列表的结果
|
|
|
|
|
type LogsResult struct {
|
|
|
|
|
ID int `json:"id"`
|
|
|
|
|
TaskID string `json:"task_id"`
|
|
|
|
|
Log string `json:"log"`
|
|
|
|
|
CreatedOn util.Time `json:"created_on"`
|
|
|
|
|
ModifiedOn util.Time `json:"modified_on"`
|
|
|
|
|
TaskName string `json:"task_name"`
|
|
|
|
|
Status int `json:"status"`
|
2024-01-22 14:17:33 +08:00
|
|
|
CallerIp string `json:"caller_ip"`
|
2023-12-30 17:40:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSendLogs 获取所有日志记录
|
2024-01-25 15:08:00 +08:00
|
|
|
func GetSendLogs(pageNum int, pageSize int, name string, taskId string, maps map[string]interface{}) ([]LogsResult, error) {
|
2023-12-30 17:40:20 +08:00
|
|
|
var logs []LogsResult
|
2024-04-29 17:31:13 +08:00
|
|
|
logt := GetSchema(SendTasksLogs{})
|
|
|
|
|
taskt := GetSchema(SendTasks{})
|
2023-12-30 17:40:20 +08:00
|
|
|
|
|
|
|
|
query := db.
|
|
|
|
|
Table(logt).
|
|
|
|
|
Select(fmt.Sprintf("%s.*, %s.name as task_name", logt, taskt)).
|
2023-12-31 21:31:56 +08:00
|
|
|
Joins(fmt.Sprintf("LEFT JOIN %s ON %s.task_id = %s.id", taskt, logt, taskt))
|
2023-12-30 17:40:20 +08:00
|
|
|
|
2024-01-25 15:08:00 +08:00
|
|
|
dayVal, ok := maps["day_created_on"]
|
|
|
|
|
if ok {
|
|
|
|
|
delete(maps, "day_created_on")
|
|
|
|
|
query = query.Where(fmt.Sprintf("DATE(%s.created_on) = ?", logt), dayVal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query = query.Where(maps)
|
2023-12-30 17:40:20 +08:00
|
|
|
if name != "" {
|
|
|
|
|
query = query.Where(fmt.Sprintf("%s.name like ?", taskt), fmt.Sprintf("%%%s%%", name))
|
|
|
|
|
}
|
|
|
|
|
if taskId != "" {
|
2023-12-31 21:31:56 +08:00
|
|
|
query = query.Where(fmt.Sprintf("%s.task_id = ?", logt), taskId)
|
2023-12-30 17:40:20 +08:00
|
|
|
}
|
|
|
|
|
query = query.Order("created_on DESC")
|
|
|
|
|
if pageSize > 0 || pageNum > 0 {
|
|
|
|
|
query = query.Offset(pageNum).Limit(pageSize)
|
|
|
|
|
}
|
|
|
|
|
query.Scan(&logs)
|
|
|
|
|
|
|
|
|
|
return logs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSendLogsTotal 获取所有日志总数
|
2024-04-29 17:31:13 +08:00
|
|
|
func GetSendLogsTotal(name string, taskId string, maps map[string]interface{}) (int64, error) {
|
|
|
|
|
var total int64
|
|
|
|
|
logt := GetSchema(SendTasksLogs{})
|
|
|
|
|
taskt := GetSchema(SendTasks{})
|
2023-12-30 17:40:20 +08:00
|
|
|
query := db.
|
|
|
|
|
Table(logt).
|
2023-12-31 21:31:56 +08:00
|
|
|
Joins(fmt.Sprintf("LEFT JOIN %s ON %s.task_id = %s.id", taskt, logt, taskt))
|
2024-01-25 15:08:00 +08:00
|
|
|
|
|
|
|
|
dayVal, ok := maps["day_created_on"]
|
|
|
|
|
if ok {
|
|
|
|
|
delete(maps, "day_created_on")
|
|
|
|
|
query = query.Where(fmt.Sprintf("DATE(%s.created_on) = ?", logt), dayVal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query = query.Where(maps)
|
2023-12-30 17:40:20 +08:00
|
|
|
if name != "" {
|
|
|
|
|
query = query.Where(fmt.Sprintf("%s.name like ?", taskt), fmt.Sprintf("%%%s%%", name))
|
|
|
|
|
}
|
|
|
|
|
if taskId != "" {
|
2023-12-31 21:31:56 +08:00
|
|
|
query = query.Where(fmt.Sprintf("%s.task_id = ?", logt), taskId)
|
2023-12-30 17:40:20 +08:00
|
|
|
}
|
|
|
|
|
query.Count(&total)
|
|
|
|
|
return total, nil
|
|
|
|
|
}
|
2023-12-31 21:31:56 +08:00
|
|
|
|
|
|
|
|
// GetSendLogsTotal 获取所有日志总数
|
2024-01-03 20:41:43 +08:00
|
|
|
func DeleteOutDateLogs(keepNum int) (int, error) {
|
|
|
|
|
var affectedRows int
|
2024-04-29 17:31:13 +08:00
|
|
|
logt := GetSchema(SendTasksLogs{})
|
2023-12-31 21:31:56 +08:00
|
|
|
sql := fmt.Sprintf(`DELETE FROM %s
|
2024-01-03 20:41:43 +08:00
|
|
|
WHERE id NOT IN (
|
|
|
|
|
SELECT id FROM (
|
|
|
|
|
SELECT id
|
|
|
|
|
FROM %s
|
|
|
|
|
ORDER BY created_on DESC
|
|
|
|
|
LIMIT %d
|
|
|
|
|
) tmp
|
|
|
|
|
);`, logt, logt, keepNum)
|
2023-12-31 21:31:56 +08:00
|
|
|
|
|
|
|
|
result := db.Exec(sql)
|
|
|
|
|
if result.Error != nil {
|
2024-01-03 20:41:43 +08:00
|
|
|
return affectedRows, result.Error
|
2023-12-31 21:31:56 +08:00
|
|
|
}
|
2024-01-03 20:41:43 +08:00
|
|
|
affectedRows = int(result.RowsAffected)
|
|
|
|
|
return affectedRows, nil
|
2023-12-31 21:31:56 +08:00
|
|
|
}
|
2024-01-24 21:33:51 +08:00
|
|
|
|
|
|
|
|
type StatisticData struct {
|
|
|
|
|
TodaySuccNum int `json:"today_succ_num"`
|
|
|
|
|
TodayFailedNum int `json:"today_failed_num"`
|
|
|
|
|
TodayTotalNum int `json:"today_total_num"`
|
|
|
|
|
|
2024-04-29 17:31:13 +08:00
|
|
|
LatestSendData []LatestSendData `json:"latest_send_data" gorm:"many2many:latest_send_data;"`
|
|
|
|
|
WayCateData []WayCateData `json:"way_cate_data" gorm:"many2many:way_cate_data;"`
|
2024-01-24 21:33:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LatestSendData struct {
|
2024-01-25 16:52:33 +08:00
|
|
|
Day string `json:"day"`
|
|
|
|
|
Num int `json:"num"`
|
|
|
|
|
SuccNum int `json:"succ_num"`
|
|
|
|
|
DaySuccNum int `json:"day_succ_num"`
|
|
|
|
|
DayFailedNum int `json:"day_failed_num"`
|
2024-01-24 21:33:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WayCateData struct {
|
|
|
|
|
WayName string `json:"way_name"`
|
|
|
|
|
CountNum int `json:"count_num"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetStatisticData 获取统计数据
|
|
|
|
|
func GetStatisticData() (StatisticData, error) {
|
|
|
|
|
var statistic StatisticData
|
|
|
|
|
var latestData []LatestSendData
|
|
|
|
|
var wayCateData []WayCateData
|
2024-04-29 17:31:13 +08:00
|
|
|
logt := GetSchema(SendTasksLogs{})
|
|
|
|
|
inst := GetSchema(SendTasksIns{})
|
|
|
|
|
wayst := GetSchema(SendWays{})
|
2024-01-26 10:14:07 +08:00
|
|
|
currDay := util.GetNowTimeStr()[:10]
|
2024-01-24 21:33:51 +08:00
|
|
|
|
|
|
|
|
// 今日统计数据
|
|
|
|
|
query := db.
|
|
|
|
|
Table(logt).
|
|
|
|
|
Select(`
|
2024-01-26 15:55:13 +08:00
|
|
|
COUNT(*) AS today_total_num,
|
|
|
|
|
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS today_succ_num,
|
|
|
|
|
SUM(CASE WHEN status != 1 or status is null THEN 1 ELSE 0 END) AS today_failed_num`).
|
2024-01-26 10:14:07 +08:00
|
|
|
Where("DATE(created_on) = ?", currDay)
|
2024-01-26 15:55:13 +08:00
|
|
|
|
2024-04-29 17:31:13 +08:00
|
|
|
query.Take(&statistic)
|
2024-01-24 21:33:51 +08:00
|
|
|
|
|
|
|
|
// 最近30天数据
|
|
|
|
|
days := 30
|
2024-04-29 17:31:13 +08:00
|
|
|
now := util.GetNowTime()
|
|
|
|
|
past := now.AddDate(0, 0, -days)
|
|
|
|
|
pastDate := past.Format("2006-01-02")
|
|
|
|
|
next := now.AddDate(0, 0, 1)
|
|
|
|
|
nextDate := next.Format("2006-01-02")
|
2024-01-24 21:33:51 +08:00
|
|
|
queryData := db.
|
|
|
|
|
Table(logt).
|
|
|
|
|
Select(`
|
2024-01-26 15:55:13 +08:00
|
|
|
CAST(DATE(created_on) AS CHAR) AS day,
|
2024-01-25 16:52:33 +08:00
|
|
|
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS day_succ_num,
|
|
|
|
|
SUM(CASE WHEN status != 1 or status is null THEN 1 ELSE 0 END) AS day_failed_num,
|
2024-01-26 15:55:13 +08:00
|
|
|
COUNT(*) AS num`).
|
2024-04-29 17:31:13 +08:00
|
|
|
Where(fmt.Sprintf(" created_on >= '%s' and created_on <= '%s' ", pastDate, nextDate)).
|
2024-01-26 15:55:13 +08:00
|
|
|
Group("day").
|
|
|
|
|
Order("day")
|
|
|
|
|
|
2024-01-24 21:33:51 +08:00
|
|
|
queryData.Scan(&latestData)
|
|
|
|
|
|
|
|
|
|
// 消息实例分类数目
|
|
|
|
|
db.
|
|
|
|
|
Table(inst).
|
|
|
|
|
Select(fmt.Sprintf("%s.name as way_name, count(%s.id) as count_num", wayst, wayst)).
|
|
|
|
|
Joins(fmt.Sprintf("JOIN %s ON %s.way_id = %s.id", wayst, inst, wayst)).
|
|
|
|
|
Group(fmt.Sprintf("%s.id", wayst)).
|
|
|
|
|
Scan(&wayCateData)
|
|
|
|
|
|
|
|
|
|
statistic.LatestSendData = latestData
|
|
|
|
|
statistic.WayCateData = wayCateData
|
|
|
|
|
return statistic, nil
|
|
|
|
|
}
|