Parcourir la source

fix timezone & fileds err

sunsheng il y a 1 an
Parent
commit
aef25b36a3
3 fichiers modifiés avec 87 ajouts et 41 suppressions
  1. 77 40
      src/api/statistics.go
  2. 1 1
      src/db/model/exeamLog.go
  3. 9 0
      src/readme.md

+ 77 - 40
src/api/statistics.go

@@ -5,6 +5,7 @@ import (
 	"copter-train/db/model"
 	"copter-train/db/repo"
 	"errors"
+	"fmt"
 	"strconv"
 	"time"
 
@@ -37,6 +38,7 @@ func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, er
 		}
 		year = year_
 	}
+	fmt.Println("year:", year)
 
 	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
 	return AggregateMonthlyLearnTime(colls, uid, _type, year)
@@ -45,34 +47,58 @@ func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, er
 
 // 整个模块的学习时长 不需要cid约束
 func AggregateMonthlyLearnTime(colls *mongo.Collection, uid string, learnType string, year int) ([]bson.M, error) {
+	chinaTimeZone := time.FixedZone("CST", 8*60*60) // 中国时区
 	pipeline := mongo.Pipeline{
+
+		// {
+		// 	{Key: "$addFields", Value: bson.D{
+		// 		{Key: "dateParts", Value: bson.D{
+		// 			{Key: "$dateToParts", Value: bson.M{
+		// 				"date":     "$createTime",
+		// 				"timezone": "Asia/Shanghai",
+		// 			}},
+		// 		}},
+		// 	}},
+		// },
+		// {
+
+		// 	{Key: "$match", Value: bson.D{
+		// 		{Key: "uid", Value: uid},
+		// 		{Key: "dateParts.year", Value: year},
+		// 		// {Key: "dateParts.month", Value: bson.D{
+		// 		// 	{Key: "$gte", Value: 1},
+		// 		// 	{Key: "$lt", Value: 13},
+		// 		// }},
+		// 	}},
+		// },
+		// {
+		// 	{Key: "$project", Value: bson.D{
+		// 		{Key: "month", Value: bson.D{
+		// 			{Key: "$month", Value: "$createTime"},
+		// 		}},
+		// 		{Key: "learnTime", Value: 1},
+		// 	}},
+		// },
+		// {
+		// 	{Key: "$group", Value: bson.D{
+		// 		{Key: "_id", Value: "$month"},
+
 		{
 			{Key: "$match", Value: bson.D{
-				{Key: "uid", Value: uid},
 				{Key: "createTime", Value: bson.D{
-					{Key: "$gte", Value: bson.M{
-						"$dateFromParts": bson.M{
-							"year": year, "month": 1, "day": 1,
-							"timezone": "Asia/Shanghai",
-						},
-					}},
-					{Key: "$lt", Value: bson.M{
-						"$dateFromParts": bson.M{
-							"year": year + 1, "month": 1, "day": 1,
-							"timezone": "Asia/Shanghai",
-						},
-					}},
+					{Key: "$gte", Value: time.Date(year, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
+					{Key: "$lt", Value: time.Date(year+1, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
 				}},
 			}},
 		},
+		// 将 createTime 字段分解为年和月
 		{
-			{Key: "$project", Value: bson.D{
-				{Key: "month", Value: bson.D{
-					{Key: "$month", Value: "$createTime"},
-				}},
-				{Key: "learnTime", Value: 1},
+			{Key: "$addFields", Value: bson.D{
+				// {"year", bson.D{{"$year", bson.D{{"$add", []interface{}{"$createTime", 8 * 60 * 60 * 1000}}}}}},
+				{Key: "month", Value: bson.D{{Key: "$month", Value: bson.D{{Key: "$add", Value: []interface{}{"$createTime", 8 * 60 * 60 * 1000}}}}}},
 			}},
 		},
+		// 按年和月分组,并计算每个月的正确和错误数量
 		{
 			{Key: "$group", Value: bson.D{
 				{Key: "_id", Value: "$month"},
@@ -176,7 +202,10 @@ func AggregateTotalLearnTime(colls *mongo.Collection, uid string) (int, error) {
 	}
 
 	if len(results) > 0 {
-		return results[0]["totalLearnTime"].(int), nil
+		if v, ok := results[0]["totalLearnTime"].(int32); ok {
+			return int(v), nil
+		}
+		return 0, nil
 	}
 	return 0, nil
 }
@@ -206,6 +235,7 @@ func StatisticsLastExeamScore(c *gin.Context, apictx *ApiSession) (interface{},
 	if !found {
 		return 0, nil
 	}
+	fmt.Println(exeamLog)
 	return exeamLog.Score, nil
 }
 
@@ -302,36 +332,43 @@ func StatisticsExeamTFRate(c *gin.Context, apictx *ApiSession) (interface{}, err
 		year = year_
 	}
 
-	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
+	fmt.Println(year)
+	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionExeamLog)
+	chinaTimeZone := time.FixedZone("CST", 8*60*60) // 中国时区
+	fmt.Println(uid)
+
 	pipeline := mongo.Pipeline{
+		// 筛选出特定时间范围的记录
 		{
 			{Key: "$match", Value: bson.D{
-				{Key: "uid", Value: uid},
 				{Key: "createTime", Value: bson.D{
-					{Key: "$gte", Value: bson.M{
-						"$dateFromParts": bson.M{
-							"year": year, "month": 1, "day": 1,
-							"timezone": "Asia/Shanghai",
-						},
-					}},
-					{Key: "$lt", Value: bson.M{
-						"$dateFromParts": bson.M{
-							"year": year + 1, "month": 1, "day": 1,
-							"timezone": "Asia/Shanghai",
-						},
-					}},
+					{Key: "$gte", Value: time.Date(year, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
+					{Key: "$lt", Value: time.Date(year+1, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
 				}},
 			}},
 		},
-		{{Key: "$group", Value: bson.D{
-			{Key: "_id", Value: bson.D{{Key: "month", Value: bson.D{{Key: "$month", Value: "$createTime"}}}}},
-			{Key: "totalCorrect", Value: bson.D{{Key: "$sum", Value: "$correct"}}},
-			{Key: "totalError", Value: bson.D{{Key: "$sum", Value: "$error"}}},
-		}}},
-		{{Key: "$sort", Value: bson.D{{Key: "_id.month", Value: 1}}}},
+		// 将 createTime 字段分解为年和月
+		{
+			{Key: "$addFields", Value: bson.D{
+				// {"year", bson.D{{"$year", bson.D{{"$add", []interface{}{"$createTime", 8 * 60 * 60 * 1000}}}}}},
+				{Key: "month", Value: bson.D{{Key: "$month", Value: bson.D{{Key: "$add", Value: []interface{}{"$createTime", 8 * 60 * 60 * 1000}}}}}},
+			}},
+		},
+		// 按年和月分组,并计算每个月的正确和错误数量
+		{
+			{Key: "$group", Value: bson.D{
+				{Key: "_id", Value: "$month"},
+				{Key: "correct", Value: bson.D{{Key: "$sum", Value: "$correct"}}},
+				{Key: "error", Value: bson.D{{Key: "$sum", Value: "$error"}}},
+			}},
+		},
+		// 排序结果
+		{
+			{Key: "$sort", Value: bson.D{{Key: "_id", Value: 1}}},
+		},
 	}
 
-	cursor, err := colls.Aggregate(apictx.CreateRepoCtx().Ctx, pipeline)
+	cursor, err := colls.Aggregate(context.TODO(), pipeline)
 	if err != nil {
 		return nil, err
 	}

+ 1 - 1
src/db/model/exeamLog.go

@@ -16,7 +16,7 @@ type ExeamLog struct {
 	TotalScore  *int                     `bson:"totalScore,omitempty" json:"totalScore"`   // 总分
 	ExeamResult map[string][]*TestRecord `bson:"exeamResult,omitempty" json:"exeamResult"` // 考核结果/ choiceQuestion:[{}] judgeQuestion:[{}]
 	// 成绩
-	Score        *int      `bson:"socre,omitempty" json:"socre"`               // 分数
+	Score        *int      `bson:"score,omitempty" json:"score"`               // 分数
 	CompleteRate *int      `bson:"completeRate,omitempty" json:"completeRate"` // 完成进度
 	CreateTime   time.Time `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime   time.Time `bson:"updateTime,omitempty" json:"updateTime"`

+ 9 - 0
src/readme.md

@@ -29,3 +29,12 @@ MONGO_MIGRATION: mongo-migrations==mongodb://root:copter-train-8888@127.0.0.1:27
 
 <!-- 
 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDYxNTUwMzksImlkIjoiNjVhOGEwNGNhODU1NzUyYTU4YWNmYmY3Iiwib3JpZ19pYXQiOjE3MDU1NTAyMzl9.M3-RTgD65y8wg20dTCOyx79XMLfMVDoaNU_bDaHSpmY -->
+
+// 提交
+// 运行环境
+// 编译
+// docker 推送
+// 测试
+// 部署
+Administrator
+eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MDY2MDM4ODgsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjMsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MTJ9fQ.5VPbPLI17XB8-ApULzskKFtqQVxG-Xh8Gdqf1XnhwPA