sunsheng 1 year ago
parent
commit
70e262973c
2 changed files with 46 additions and 44 deletions
  1. 2 2
      src/api/router.go
  2. 44 42
      src/api/statistics.go

+ 2 - 2
src/api/router.go

@@ -71,8 +71,8 @@ func RegRouters(svc *Service) {
 	root.GETJWT("/statistics/last/exeamScore/:scope", StatisticsLastExeamScore)
 	// 个人: 获取历史最好成绩 uid
 	root.GETJWT("/statistics/best/exeamScore/:scope", StatisticsBestExeamScore)
-	// 个人: 已学习模块数 uid
-	root.GETJWT("/statistics/learned/modules/:scope", StatisticsLearnedModules)
+	// // !这个功能已经迁移LearnLogStatistics【待验证】到个人: 已学习模块数 uid
+	// root.GETJWT("/statistics/learned/modules/:scope", StatisticsLearnedModules)
 	// 个人: 统计用户本年每月考核正确和错误数 year
 	root.GETJWT("/statistics/exeamTF/rate/:scope", StatisticsExeamTFRate)
 

+ 44 - 42
src/api/statistics.go

@@ -142,7 +142,9 @@ func StatisticsTotalLearnTime(c *gin.Context, apictx *ApiSession) (interface{},
 		uid = apictx.User.ID
 	}
 
-	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
+	// colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
+	// ! 优化查询性能,使用统计表
+	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLogStatistics)
 	return AggregateTotalLearnTime(colls, uid)
 
 }
@@ -241,47 +243,47 @@ func StatisticsBestExeamScore(c *gin.Context, apictx *ApiSession) (interface{},
 	return exeamLog.Score, nil
 }
 
-// 个人: 已学习模块数
-// 统计各个模块的学习总时长,根据总时长时间判断是否已学习
-// 只需要返回统计各个模块的时长汇总,cid相同的为同一个模块
-func StatisticsLearnedModules(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	db := c.Param("scope")
-	if len(db) == 0 {
-		return nil, errors.New("scope不能为空")
-	}
-	// 如果没有uid参数,则查询当前用户的
-	uid := c.Query("uid")
-	if len(uid) == 0 {
-		uid = apictx.User.ID
-	}
-
-	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
-	pipeline := mongo.Pipeline{
-		{
-			{Key: "$match", Value: bson.D{
-				{Key: "uid", Value: uid},
-			}},
-		},
-		{
-			{Key: "$group", Value: bson.D{
-				{Key: "_id", Value: "$cid"},
-				{Key: "totalLearnTime", Value: bson.D{{Key: "$sum", Value: "$learnTime"}}},
-			}},
-		},
-	}
-
-	cursor, err := colls.Aggregate(apictx.CreateRepoCtx().Ctx, pipeline)
-	if err != nil {
-		return nil, err
-	}
-
-	var results []bson.M
-	if err = cursor.All(apictx.CreateRepoCtx().Ctx, &results); err != nil {
-		return nil, err
-	}
-
-	return results, nil
-}
+// // 个人: 已学习模块数
+// // 统计各个模块的学习总时长,根据总时长时间判断是否已学习
+// // 只需要返回统计各个模块的时长汇总,cid相同的为同一个模块
+// func StatisticsLearnedModules(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+// 	db := c.Param("scope")
+// 	if len(db) == 0 {
+// 		return nil, errors.New("scope不能为空")
+// 	}
+// 	// 如果没有uid参数,则查询当前用户的
+// 	uid := c.Query("uid")
+// 	if len(uid) == 0 {
+// 		uid = apictx.User.ID
+// 	}
+
+// 	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
+// 	pipeline := mongo.Pipeline{
+// 		{
+// 			{Key: "$match", Value: bson.D{
+// 				{Key: "uid", Value: uid},
+// 			}},
+// 		},
+// 		{
+// 			{Key: "$group", Value: bson.D{
+// 				{Key: "_id", Value: "$cid"},
+// 				{Key: "totalLearnTime", Value: bson.D{{Key: "$sum", Value: "$learnTime"}}},
+// 			}},
+// 		},
+// 	}
+
+// 	cursor, err := colls.Aggregate(apictx.CreateRepoCtx().Ctx, pipeline)
+// 	if err != nil {
+// 		return nil, err
+// 	}
+
+// 	var results []bson.M
+// 	if err = cursor.All(apictx.CreateRepoCtx().Ctx, &results); err != nil {
+// 		return nil, err
+// 	}
+
+// 	return results, nil
+// }
 
 // 个人: 统计用户本年每月考核正确和错误数
 func StatisticsExeamTFRate(c *gin.Context, apictx *ApiSession) (interface{}, error) {