-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from PolkadotEducation/feature/course-progress…
…-and-ranking course progress for course navigation and ranking feature
- Loading branch information
Showing
10 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Request, Response } from "express"; | ||
import { RankingModel } from "@/models/Ranking"; | ||
import { calculateRanking } from "@/services/ranking"; | ||
|
||
export const getRanking = async (req: Request, res: Response) => { | ||
const type = req.query.type as "weekly" | "general"; | ||
|
||
if (!type || !["weekly", "general"].includes(type)) { | ||
return res.status(400).send({ error: { message: "Invalid ranking type. Valid types are 'weekly' or 'general'." } }); | ||
} | ||
|
||
try { | ||
let ranking = await RankingModel.findOne({ type }).select("ranking lastUpdated").lean(); | ||
|
||
if (!ranking) { | ||
await calculateRanking(type); | ||
ranking = await RankingModel.findOne({ type }).select("ranking lastUpdated").lean(); | ||
|
||
if (!ranking) { | ||
return res.status(500).send({ error: { message: "Failed to calculate ranking. Please try again." } }); | ||
} | ||
} | ||
|
||
return res.status(200).send({ | ||
type, | ||
lastUpdated: ranking.lastUpdated, | ||
ranking: ranking.ranking, | ||
}); | ||
} catch (error) { | ||
console.error(`[ERROR][getRanking] Failed to fetch ranking for type '${type}':`, error); | ||
return res.status(500).send({ error: { message: "Internal server error. Please try again later." } }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getModelForClass, prop } from "@typegoose/typegoose"; | ||
import BaseModel from "./BaseModel"; | ||
|
||
class Ranking extends BaseModel { | ||
@prop({ required: true, enum: ["weekly", "general"], type: String }) | ||
public type: string; | ||
|
||
@prop({ required: true, type: Date }) | ||
public lastUpdated: Date; | ||
|
||
@prop({ required: true, type: () => [Object] }) | ||
public ranking!: Array<{ | ||
userId: string; | ||
name: string; | ||
picture: string; | ||
xp: number; | ||
}>; | ||
} | ||
|
||
const RankingModel = getModelForClass(Ranking); | ||
export { Ranking, RankingModel }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { calculateRanking } from "@/services/ranking"; | ||
|
||
(async () => { | ||
try { | ||
await calculateRanking("weekly"); | ||
await calculateRanking("general"); | ||
} catch (error) { | ||
console.error("Error updating ranking:", error); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ProgressModel } from "@/models/Progress"; | ||
import { UserModel } from "@/models/User"; | ||
import { RankingModel } from "@/models/Ranking"; | ||
|
||
export const calculateRanking = async (type: "weekly" | "general") => { | ||
const today = new Date(); | ||
today.setHours(0, 0, 0, 0); | ||
|
||
const startOfWeek = new Date(today); | ||
startOfWeek.setDate(today.getDate() - today.getDay()); | ||
|
||
let matchCondition = {}; | ||
if (type === "weekly") { | ||
matchCondition = { createdAt: { $gte: startOfWeek } }; | ||
} | ||
|
||
const xpByDifficulty = { | ||
easy: 50, | ||
medium: 100, | ||
hard: 200, | ||
}; | ||
|
||
const aggregationPipeline = [ | ||
{ $match: { ...matchCondition, isCorrect: true } }, | ||
{ | ||
$group: { | ||
_id: "$userId", | ||
totalXP: { | ||
$sum: { | ||
$switch: { | ||
branches: [ | ||
{ case: { $eq: ["$difficulty", "easy"] }, then: xpByDifficulty.easy }, | ||
{ case: { $eq: ["$difficulty", "medium"] }, then: xpByDifficulty.medium }, | ||
{ case: { $eq: ["$difficulty", "hard"] }, then: xpByDifficulty.hard }, | ||
], | ||
default: 0, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const results = await ProgressModel.aggregate(aggregationPipeline); | ||
|
||
const ranking = await Promise.all( | ||
results.map(async (result) => { | ||
const user = await UserModel.findById(result._id).select("name picture"); | ||
return { | ||
userId: result._id.toString(), | ||
name: user?.name || "Unknown", | ||
picture: user?.picture || "", | ||
xp: result.totalXP, | ||
}; | ||
}), | ||
); | ||
|
||
ranking.sort((a, b) => b.xp - a.xp); | ||
|
||
await RankingModel.updateOne({ type }, { type, lastUpdated: new Date(), ranking }, { upsert: true }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
const cron = require("node-cron"); | ||
|
||
import { calculateRanking } from "@/services/ranking"; | ||
|
||
export const scheduleRankingUpdate = () => { | ||
cron.schedule("0 0 * * *", async () => { | ||
try { | ||
await calculateRanking("weekly"); | ||
await calculateRanking("general"); | ||
} catch (error) { | ||
console.error("[UpdateRankError]:", error); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters