Skip to content

Commit bc9aea0

Browse files
committed
Merge branch 'main' of ssh://github.com/SysSn13/leetcode-rating-predictor
2 parents 26bd257 + 1f07f78 commit bc9aea0

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

models/contest.js

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const ContestRankingsSchema = new Schema({
4040
default: false,
4141
},
4242
rankings: [rankingSchema],
43+
refetch_rankings:{
44+
type:Boolean,
45+
default:false,
46+
},
4347
lastUpdated: {
4448
type: Date,
4549
},

services/contests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const fetchContestRankings = async function (contestSlug) {
88
if (!contest) {
99
return [null, Error(`Contest ${contestSlug} not found in the db`)];
1010
}
11-
if (contest.rankings_fetched) {
11+
if (!contest.refetch_rankings && contest.rankings_fetched) {
1212
return [contest, null];
1313
}
1414
contest.rankings = [];
@@ -106,6 +106,7 @@ const fetchContestRankings = async function (contestSlug) {
106106

107107
contest.rankings = all_rankings;
108108
contest.rankings_fetched = true;
109+
contest.refetch_rankings = false;
109110
contest.user_num = all_rankings.length;
110111
console.time(`Saving rankings in db (${contestSlug})`);
111112
await contest.save();

services/job-queues/contestPredictionQueue.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ const predictQueue = new Queue("Predictions", opts);
1111
predictQueue.process("predictRatings", async (job, done) => {
1212
try {
1313
console.log(`Processing ${job.name} job: ${job.id}`);
14+
1415
const contest = await Contest.findById(job.data.contestSlug, {
1516
ratings_predicted: 1,
1617
});
17-
if (contest && contest.ratings_predicted) {
18+
19+
const refetch = job.data.refetch && job.attemptsMade === 0; // applicable for the first attempt only
20+
21+
if (!refetch && contest && contest.ratings_predicted) {
1822
done(null, { message: "skipped (already predicted)" });
1923
return;
2024
}
25+
26+
if (refetch) {
27+
contest.refetch_rankings = true;
28+
await contest.save();
29+
}
30+
2131
const err = await predict(job);
2232
if (err) {
2333
console.error(err);

services/job-queues/jobScheduler.js

+19
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ scheduler.process("contestScheduler", async (job, done) => {
3737
}
3838
);
3939
cnt++;
40+
// refetch for the upcoming contests
41+
if (remainingTime > 0) {
42+
remainingTime += 55 * 60 * 1000; // 1 hour after the contest
43+
contestPredictionQueue.add(
44+
"predictRatings",
45+
{
46+
contestSlug: contest._id,
47+
refetch: true,
48+
},
49+
{
50+
jobId: "refetch_" + contest._id,
51+
attempts: 5,
52+
delay: remainingTime,
53+
backoff: 10000,
54+
priority: 1,
55+
}
56+
);
57+
cnt++;
58+
}
4059
}
4160
});
4261

services/predict.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { fetchContestRankings } = require("./contests");
22
const { getContestParticipantsData } = require("./users");
33
const Contest = require("../models/contest");
44
const predictAddon = require("./predict-addon");
5-
const THREAD_CNT = process.env.THREAD_CNT || 4;
5+
const THREAD_CNT = Number(process.env.THREAD_CNT) || 4;
66

77
const predict = async (job) => {
88
try {

views/index.ejs

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<th scope="col">Duration</th>
4848
<th scope="col">Rankings Fetched</th>
4949
<th scope="col">Predicted</th>
50+
<th scope="col">Last Updated</th>
5051
</tr>
5152
</thead>
5253
<tbody>
@@ -73,6 +74,9 @@
7374
<td>
7475
<%= contest.ratings_predicted?"Yes":"No" %>
7576
</td>
77+
<td class="lastUpdatedTime">
78+
<%= contest.lastUpdated %>
79+
</td>
7680
</tr>
7781
<% } %>
7882
</tbody>
@@ -88,10 +92,15 @@
8892
(function () {
8993
function localizeDate(page) {
9094
const startTimes = document.getElementsByClassName('startTime')
95+
const lastUpdatedTimes = document.getElementsByClassName('lastUpdatedTime')
9196
for (const startTime of startTimes) {
9297
const dateString = startTime.data.trim()
9398
startTime.textContent = new Date(dateString).toLocaleString()
9499
}
100+
for(const lastUpdatedTime of lastUpdatedTimes){
101+
const dateString = lastUpdatedTime.data.trim()
102+
lastUpdatedTime.textContent = new Date(dateString).toLocaleString()
103+
}
95104
}
96105
const dataTable = new simpleDatatables.DataTable("#contest-table", {
97106
searchable: true,

0 commit comments

Comments
 (0)