dashboard部分添加最近一周、模型使用统计数据

This commit is contained in:
junleea 2025-04-06 14:59:44 +08:00
parent 3a9f6c881c
commit 210daec992
6 changed files with 124 additions and 4 deletions

View File

@ -179,3 +179,33 @@ func FindUserSessionCount(userID, sessionType int) int64 {
log.Println("user session count:", sessionCount)
return sessionCount
}
// 获取最近一周每天的会话数
func FindSessionCountByDate() ([]int64, error) {
var sessionCounts []int64
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
if err := db2.Model(&Session{}).Select("DATE(created_at) as date, COUNT(*) as count").Where("created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)").Group("date").Order("date DESC").Scan(&sessionCounts).Error; err != nil {
return nil, err
}
return sessionCounts, nil
}
// 获取最近一周每天的消息数
func FindMessageCountByDate() ([]int64, error) {
var messageCounts []int64
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
if err := db2.Model(&Message{}).Select("DATE(created_at) as date, COUNT(*) as count").Where("created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)").Group("date").Order("date DESC").Scan(&messageCounts).Error; err != nil {
return nil, err
}
return messageCounts, nil
}

View File

@ -21,7 +21,7 @@ type FunctionModel struct {
ModelID uint `gorm:"column:model_id"` //模型id
ModelIDS string `gorm:"column:model_ids"` //允许模型id,可多个
Info string `gorm:"column:info"` //功能信息,系统功能,对应模型系统参数
//System string `gorm:"column:system"` //系统功能,对应模型系统参数
System string `gorm:"column:system"` //系统功能,对应模型系统参数
}
type ModelToken struct {

View File

@ -1,6 +1,9 @@
package dao
import "gorm.io/gorm"
import (
"StuAcaWorksAI/proto"
"gorm.io/gorm"
)
type AIStreamMsg struct {
gorm.Model
@ -37,3 +40,30 @@ func FindAIStreamMsgByID(id uint) (error, AIStreamMsg) {
err := DB.Where("id = ?", id).First(&aiStreamMsg).Error
return err, aiStreamMsg
}
type AIStreamMsgModelIDCount struct {
ModelID uint `gorm:"column:model_id" json:"model_id"`
ModelName string `gorm:"column:model_name" json:"name"`
Count int `gorm:"column:count" json:"value"`
}
// 获取模型消息数量,统计数据
func GetAIStreamModelMsgCount() ([]AIStreamMsgModelIDCount, error) {
var aiStreamMsgModelIDCounts []AIStreamMsgModelIDCount
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
db2.Model(&AIStreamMsg{}).Select("model_id, count(*) as count").Group("model_id").Find(&aiStreamMsgModelIDCounts)
if db2.Error != nil {
return nil, db2.Error
}
//获取模型名称
for i := 0; i < len(aiStreamMsgModelIDCounts); i++ {
modelName := FindModelByIDV2(int(aiStreamMsgModelIDCounts[i].ModelID))
aiStreamMsgModelIDCounts[i].ModelName = modelName[0].Description
}
return aiStreamMsgModelIDCounts, nil
}

View File

@ -416,7 +416,13 @@ func DashBoardStatistics(c *gin.Context) {
//用户统计信息
rbase := service.GetBaseDashboardInfo()
var resp proto.DashBoardStatisticsResp
//获取最近7天的统计信息
sWeek, _ := service.GetRecent7DaysMessageSessionCount()
//模型统计信息
modelst, _ := service.GetModelUsageStatistics()
resp.DashBoardStatisticsSt = rbase
resp.DashBoardStatisticsWeekSt = sWeek
resp.DashBoardStatisticsModelSt = modelst
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": resp})
return
}

View File

@ -8,6 +8,21 @@ type DashBoardStatisticsSt struct {
TodayMessageCount int64 `json:"today_message_count"`
}
type DashBoardStatisticsResp struct {
DashBoardStatisticsSt DashBoardStatisticsSt `json:"dashboard_statistics_st"`
// 统计过去一周每天的会话数、消息数
type DashBoardStatisticsWeekSt struct {
SessionCount []int64 `json:"session_count"`
MessageCount []int64 `json:"message_count"`
}
type DashBoardStatisticsResp struct {
DashBoardStatisticsSt DashBoardStatisticsSt `json:"dashboard_statistics_st"`
DashBoardStatisticsWeekSt DashBoardStatisticsWeekSt `json:"dashboard_statistics_week"`
// 模型的使用统计
DashBoardStatisticsModelSt []MessageModelIDCountSt `json:"dashboard_statistics_model_st"`
}
type MessageModelIDCountSt struct {
ModelID uint `json:"model_id"`
ModelName string `json:"name"`
Count int `json:"value"`
}

View File

@ -128,3 +128,42 @@ func GetBaseDashboardInfo() proto.DashBoardStatisticsSt {
res.UserCount = dao.FindUserNum()
return res
}
// 获取最近7天的消息、会话数量
func GetRecent7DaysMessageSessionCount() (proto.DashBoardStatisticsWeekSt, error) {
var res proto.DashBoardStatisticsWeekSt
var err error
res.SessionCount, err = dao.FindSessionCountByDate()
if err != nil {
log.Println("get recent 7 days session count error:", err)
return res, err
}
res.MessageCount, err = dao.FindMessageCountByDate()
if err != nil {
log.Println("get recent 7 days message count error:", err)
return res, err
}
return res, nil
}
// 获取模型使用统计
func GetModelUsageStatistics() ([]proto.MessageModelIDCountSt, error) {
m, err := dao.GetAIStreamModelMsgCount()
if err != nil {
log.Println("get model usage statistics error:", err)
return nil, err
}
var res []proto.MessageModelIDCountSt
for _, v := range m {
model := dao.FindModelByModelID(v.ModelID)
if len(model) == 0 {
continue
}
res = append(res, proto.MessageModelIDCountSt{
ModelID: v.ModelID,
ModelName: v.ModelName,
Count: v.Count,
})
}
return res, nil
}