diff --git a/handler/tool.go b/handler/tool.go index fde2741..60581fe 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -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 } diff --git a/main.go b/main.go index 791cb71..538398d 100644 --- a/main.go +++ b/main.go @@ -211,7 +211,8 @@ func myTask() { } //其它定时任务-通用 RunGeneralCron() - service.AddKnowledgeBaseServer() //将配置文件中支持的知识库处理服务器添加到集合 + service.AddKnowledgeBaseServer() //将配置文件中支持的知识库处理服务器添加到集合 + service.SetDashboardInfoToRedis() //统计dashboard信息保存在redis中 } func ReadConfigToSetSystem() { diff --git a/service/toolService.go b/service/toolService.go index 407b7df..c449682 100644 --- a/service/toolService.go +++ b/service/toolService.go @@ -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 + } + +}