From d47e4f727624104dceff1d9b983d1fbeb2f4704a Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Wed, 23 Apr 2025 18:16:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96dashboard?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BF=A1=E6=81=AF=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=AF=8F10s=E8=8E=B7=E5=8F=96=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=EF=BC=8C=E5=8A=A0=E5=BF=AB=E5=93=8D=E5=BA=94=E9=80=9F?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 30 ++++++++++++++++++++++-------- main.go | 3 ++- service/toolService.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) 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 + } + +}