-
Notifications
You must be signed in to change notification settings - Fork 121
Added backend for leaderbaord #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,89 @@ | ||||||
const axios = require("axios"); | ||||||
const fs = require("fs"); | ||||||
require("dotenv").config(); | ||||||
|
||||||
const ORG_NAME = "recodehive"; | ||||||
let leaderboard = {}; | ||||||
|
||||||
const timer = ms => new Promise(res => setTimeout(res, ms)); | ||||||
|
||||||
async function generateOrgLeaderboard() { | ||||||
try { | ||||||
let repos = await axios.get( | ||||||
`https://api.github.com/orgs/${ORG_NAME}/repos?per_page=100`, | ||||||
{ | ||||||
headers: { Authorization: "token " + process.env.GIT_TOKEN } | ||||||
} | ||||||
); | ||||||
|
||||||
for (let repo of repos.data) { | ||||||
let repoFullName = repo.full_name; // recodehive/repoName | ||||||
console.log(`Fetching PRs for ${repoFullName}`); | ||||||
|
||||||
try { | ||||||
let response = await axios.get( | ||||||
`https://api.github.com/search/issues?q=repo:${repoFullName}+is:pr+is:merged&per_page=100`, | ||||||
{ headers: { Authorization: "token " + process.env.GIT_TOKEN } } | ||||||
); | ||||||
|
||||||
let prs = response.data.items; | ||||||
|
||||||
for (let pr of prs) { | ||||||
let user = pr.user; | ||||||
|
||||||
if (!leaderboard[user.id]) { | ||||||
leaderboard[user.id] = { | ||||||
avatar_url: user.avatar_url, | ||||||
login: user.login, | ||||||
url: user.html_url, | ||||||
score: 0, | ||||||
// postManTag: false, // keep if needed | ||||||
pr_urls: [] | ||||||
}; | ||||||
} | ||||||
|
||||||
if (!leaderboard[user.id].pr_urls.includes(pr.html_url)) { | ||||||
leaderboard[user.id].pr_urls.push(pr.html_url); | ||||||
leaderboard[user.id].score += 10; // scoring rule (static here) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The score value (10) is a magic number. Consider defining this as a named constant at the top of the file for better maintainability. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
} | ||||||
} catch (err) { | ||||||
console.log(`Error fetching PRs for ${repoFullName}`, err.message); | ||||||
} | ||||||
|
||||||
// small delay to avoid hitting GitHub rate limits | ||||||
await timer(3000); | ||||||
} | ||||||
|
||||||
// Convert to array, add rank + PR count | ||||||
let leaderboardArray = Object.values(leaderboard) | ||||||
.map(u => ({ | ||||||
...u, | ||||||
no_of_prs: u.pr_urls.length | ||||||
})) | ||||||
.sort((a, b) => b.score - a.score) | ||||||
.map((u, i) => ({ | ||||||
rank: i + 1, | ||||||
...u | ||||||
})); | ||||||
|
||||||
let json = { | ||||||
leaderboard: leaderboardArray, | ||||||
success: true, | ||||||
updatedAt: +new Date(), | ||||||
generated: true, | ||||||
updatedTimestring: new Date().toLocaleString() | ||||||
}; | ||||||
|
||||||
fs.writeFileSync("org_leaderboard.json", JSON.stringify(json, null, 2)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using synchronous file operations (writeFileSync) in an async function can block the event loop. Consider using fs.promises.writeFile or fs.writeFile with a callback for better performance.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
console.log("recodehive leaderboard generated!"); | ||||||
} catch (error) { | ||||||
console.error("Failed to generate leaderboard:", error.message); | ||||||
} | ||||||
} | ||||||
|
||||||
module.exports.generateOrgLeaderboard = generateOrgLeaderboard; | ||||||
|
||||||
// if (require.main === module) { | ||||||
// generateOrgLeaderboard(); //Added to test the function directly | ||||||
// } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const schedule = require('node-schedule'); | ||
const { generateOrgLeaderboard } = require("../functions/org_leaderboard"); | ||
|
||
|
||
|
||
function updateOrgLeaderboardJob() { | ||
// Cron format: second, minute, hour, day of month, month, day of week | ||
// This runs every day at 00:00:00 (midnight) | ||
schedule.scheduleJob('0 0 0 * * *', async function () { | ||
console.log("========"); | ||
console.log("Starting leaderboard update job..."); | ||
console.log("========"); | ||
|
||
try { | ||
await generateOrgLeaderboard(); | ||
console.log("Leaderboard updated successfully!"); | ||
} catch (err) { | ||
console.error("Leaderboard update failed:", err.message); | ||
} | ||
}); | ||
} | ||
|
||
module.exports.updateOrgLeaderboardJob = updateOrgLeaderboardJob; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The leaderboard object is declared at module level, which means it will accumulate data across multiple function calls without being reset. This will cause duplicate entries and incorrect scores when the function runs multiple times.
Copilot uses AI. Check for mistakes.