|
@@ -5,6 +5,7 @@ import (
|
|
"copter-train/db/model"
|
|
"copter-train/db/model"
|
|
"copter-train/db/repo"
|
|
"copter-train/db/repo"
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
"strconv"
|
|
"strconv"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -37,6 +38,7 @@ func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, er
|
|
}
|
|
}
|
|
year = year_
|
|
year = year_
|
|
}
|
|
}
|
|
|
|
+ fmt.Println("year:", year)
|
|
|
|
|
|
colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
|
|
colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
|
|
return AggregateMonthlyLearnTime(colls, uid, _type, year)
|
|
return AggregateMonthlyLearnTime(colls, uid, _type, year)
|
|
@@ -45,34 +47,58 @@ func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, er
|
|
|
|
|
|
// 整个模块的学习时长 不需要cid约束
|
|
// 整个模块的学习时长 不需要cid约束
|
|
func AggregateMonthlyLearnTime(colls *mongo.Collection, uid string, learnType string, year int) ([]bson.M, error) {
|
|
func AggregateMonthlyLearnTime(colls *mongo.Collection, uid string, learnType string, year int) ([]bson.M, error) {
|
|
|
|
+ chinaTimeZone := time.FixedZone("CST", 8*60*60) // 中国时区
|
|
pipeline := mongo.Pipeline{
|
|
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: "$match", Value: bson.D{
|
|
- {Key: "uid", Value: uid},
|
|
|
|
{Key: "createTime", Value: bson.D{
|
|
{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: "$group", Value: bson.D{
|
|
{Key: "_id", Value: "$month"},
|
|
{Key: "_id", Value: "$month"},
|
|
@@ -176,7 +202,10 @@ func AggregateTotalLearnTime(colls *mongo.Collection, uid string) (int, error) {
|
|
}
|
|
}
|
|
|
|
|
|
if len(results) > 0 {
|
|
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
|
|
return 0, nil
|
|
}
|
|
}
|
|
@@ -206,6 +235,7 @@ func StatisticsLastExeamScore(c *gin.Context, apictx *ApiSession) (interface{},
|
|
if !found {
|
|
if !found {
|
|
return 0, nil
|
|
return 0, nil
|
|
}
|
|
}
|
|
|
|
+ fmt.Println(exeamLog)
|
|
return exeamLog.Score, nil
|
|
return exeamLog.Score, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -302,36 +332,43 @@ func StatisticsExeamTFRate(c *gin.Context, apictx *ApiSession) (interface{}, err
|
|
year = year_
|
|
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{
|
|
pipeline := mongo.Pipeline{
|
|
|
|
+ // 筛选出特定时间范围的记录
|
|
{
|
|
{
|
|
{Key: "$match", Value: bson.D{
|
|
{Key: "$match", Value: bson.D{
|
|
- {Key: "uid", Value: uid},
|
|
|
|
{Key: "createTime", Value: bson.D{
|
|
{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 {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|