修改获取dashboard统计信息,添加缓存每10s获取一次,加快响应速度

This commit is contained in:
junleea 2025-04-23 18:16:17 +08:00
parent d3d3e6f594
commit d47e4f7276
3 changed files with 53 additions and 9 deletions

View File

@ -414,15 +414,29 @@ func SendMailTool(c *gin.Context) {
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
key := "dashboard_statistics_info"
if worker.IsContainKey(key) {
//从redis获取
dashboardStatisticsInfo := worker.GetRedis(key)
if dashboardStatisticsInfo != "" {
err := json.Unmarshal([]byte(dashboardStatisticsInfo), &resp)
if err != nil {
log.Println("get redis dashboard statistics info error:", err)
}
}
} else {
service.SetDashboardInfoToRedis() //统计dashboard信息保存在redis中
dashboardStatisticsInfo := worker.GetRedis(key)
if dashboardStatisticsInfo != "" {
err := json.Unmarshal([]byte(dashboardStatisticsInfo), &resp)
if err != nil {
log.Println("get redis dashboard statistics info error:", err)
}
}
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": resp})
return
}

View File

@ -212,6 +212,7 @@ func myTask() {
//其它定时任务-通用
RunGeneralCron()
service.AddKnowledgeBaseServer() //将配置文件中支持的知识库处理服务器添加到集合
service.SetDashboardInfoToRedis() //统计dashboard信息保存在redis中
}
func ReadConfigToSetSystem() {

View File

@ -4,6 +4,7 @@ import (
"StuAcaWorksAI/dao"
"StuAcaWorksAI/proto"
"StuAcaWorksAI/worker"
"encoding/json"
"fmt"
"log"
"regexp"
@ -172,3 +173,31 @@ func GetModelUsageStatistics() ([]proto.MessageModelIDCountSt, error) {
}
return res, nil
}
// 定时任务统计dashboard信息保存在redis中
func SetDashboardInfoToRedis() {
//用户统计信息
rbase := GetBaseDashboardInfo()
var resp proto.DashBoardStatisticsResp
//获取最近7天的统计信息
sWeek, _ := GetRecent7DaysMessageSessionCount()
//模型统计信息
modelst, _ := GetModelUsageStatistics()
resp.DashBoardStatisticsSt = rbase
resp.DashBoardStatisticsWeekSt = sWeek
resp.DashBoardStatisticsModelSt = modelst
respStr, err := json.Marshal(resp)
if err != nil {
log.Println("set dashboard info to redis error:", err)
return
}
key := "dashboard_statistics_info"
//将数据存入redis
isSet := worker.SetRedisWithExpire(key, string(respStr), time.Duration(1)*time.Hour)
if !isSet {
log.Println("set dashboard info to redis error")
return
}
}