Skip to content

Commit cc9109d

Browse files
committed
Added job scheduling for updating db
1 parent a56d642 commit cc9109d

File tree

5 files changed

+194
-8
lines changed

5 files changed

+194
-8
lines changed

models/contest.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const Schema = mongoose.Schema
33

44
const ContestRankingsSchema = new Schema({
55
_id: String,
6+
startTime: Date,
7+
endTime: Date,
68
contest_id: Number,
79
lastUpdated:{
810
type: Date,
9-
default: Date.now()
1011
},
1112
rankings : [{
1213
_id: String,

package-lock.json

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"express": "^4.17.1",
1717
"express-ejs-layouts": "^2.5.0",
1818
"mongoose": "^5.12.10",
19-
"node-fetch": "^2.6.1"
19+
"node-fetch": "^2.6.1",
20+
"node-schedule": "^2.0.0"
2021
},
2122
"_id": "leetcode-rating-predictor@1.0.0",
2223
"devDependencies": {

services/contests.js

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const axios = require('axios');
22
const fetch = require('node-fetch')
33

44
const Contest = require('../models/contest')
5-
let halfHour = 1000//*60*30
5+
let halfHour = 1000*60*30
66

77
const getContestRankings = async function(contestSlug) {
88

99
let contest = await Contest.findById(contestSlug)
10-
if(contest === null || Date.now() - contest.lastUpdated >= halfHour){
10+
if(contest===null || contest.rankings.length===0){
1111

1212
try {
1313
rankings = []
@@ -32,12 +32,22 @@ const getContestRankings = async function(contestSlug) {
3232
lastUpdated: Date.now(),
3333
rankings: rankings
3434
})
35-
if(contest===null){
35+
if(contest===null ){
3636
await newContest.save()
37+
console.log(`Created contest ${contestSlug}`)
38+
3739
}
3840
else{
39-
await Contest.findByIdAndUpdate(contestSlug, newContest)
40-
console.log(`Updated contest ${contestSlug}`)
41+
let updatedContest = new Contest({
42+
_id: contestSlug,
43+
contest_id: contest_id,
44+
lastUpdated: Date.now(),
45+
rankings: rankings,
46+
startTime: contest.startTime,
47+
endTime: contest.endTime,
48+
})
49+
await Contest.findByIdAndUpdate(contestSlug, updatedContest)
50+
console.log(`Updated Rankings in contest ${contestSlug}`)
4151
}
4252
}
4353
catch (error) {
@@ -49,5 +59,67 @@ const getContestRankings = async function(contestSlug) {
4959
}
5060

5161
}
52-
module.exports = getContestRankings
62+
63+
64+
const fetchContest = async () => {
65+
66+
try {
67+
let res = await fetch("https://leetcode.com/graphql", {
68+
"headers": {
69+
"accept": "*/*",
70+
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
71+
"content-type": "application/json",
72+
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"",
73+
"sec-ch-ua-mobile": "?0",
74+
"sec-fetch-dest": "empty",
75+
"sec-fetch-mode": "cors",
76+
"sec-fetch-site": "same-origin",
77+
"x-newrelic-id": "UAQDVFVRGwEAXVlbBAg="
78+
79+
},
80+
"referrer": "https://leetcode.com/contest/",
81+
"referrerPolicy": "strict-origin-when-cross-origin",
82+
"body": "{\"operationName\":null,\"variables\":{},\"query\":\"{\\n brightTitle\\n currentTimestamp\\n allContests {\\n containsPremium\\n title\\n cardImg\\n titleSlug\\n description\\n startTime\\n duration\\n originStartTime\\n isVirtual\\n company {\\n watermark\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}",
83+
"method": "POST",
84+
"mode": "cors"
85+
});
86+
res = await res.json()
87+
//console.log(res.data.allContests[0])
88+
let contestSlug = res.data.allContests[0].titleSlug
89+
let startTime = res.data.allContests[0].startTime
90+
let endTime = startTime + res.data.allContests[0].duration
91+
for(let i=0;i<res.data.allContests.length;i++)
92+
{
93+
let contest = res.data.allContests[i];
94+
let isfound = await Contest.findById(contest.titleSlug)
95+
if(isfound){
96+
break;
97+
}
98+
let newContest = new Contest({
99+
_id: contest.titleSlug,
100+
startTime: contest.startTime,
101+
endTime: startTime + contest.duration*1000,
102+
lastUpdated: Date.now(),
103+
})
104+
await newContest.save()
105+
}
106+
return res.data.allContests
107+
}
108+
catch(error){
109+
console.log(error)
110+
}
111+
112+
113+
114+
}
115+
116+
117+
118+
119+
120+
121+
122+
123+
module.exports.getContestRankings = getContestRankings
124+
module.exports.fetchContest = fetchContest
53125

services/scheduling.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const {getContestRankings , fetchContest} = require('../services/contests')
2+
const schedule = require('node-schedule');
3+
4+
//Variables For testing
5+
const jobScheduleTime = {hour: 00,minute: 00}
6+
const contestScheduleCall = { start: Date.now(), end: Date.now() + 1000*60*60, rule: '*/5 * * * *' }
7+
8+
9+
const job = schedule.scheduleJob({hour: 00, minute: 00,},async function(){
10+
let contestList = await fetchContest()
11+
for(let i=0;i<contestList.length;i++){
12+
if(Date.now() + 1000*60*60*24 > contestList[i].startTime && Date.now()<contestList[i].startTime){
13+
const event = schedule.scheduleJob({ start: contestList[todaysContest].startTime, end: contestList[todaysContest].endTime, rule: '*/5 * * * *' },
14+
async function(){
15+
getContestRankings(contestList[i].titleSlug)
16+
});
17+
}
18+
}
19+
});

0 commit comments

Comments
 (0)