Skip to content

Commit 9fed5e7

Browse files
committed
caching last 200 response data & using when codeforces server is down/slow
1 parent 4f32ced commit 9fed5e7

File tree

1 file changed

+101
-62
lines changed

1 file changed

+101
-62
lines changed

src/fetcher.js

Lines changed: 101 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
import { capitalize } from "@/common.js";
22
import { api } from "@/axios.js";
33

4+
const last_rating_cache = new Map();
5+
const last_stats_cache = new Map();
6+
7+
function fetch_error_handler(fetch, username, last_cache) {
8+
return new Promise((resolve, reject) => {
9+
const timeoutID = setTimeout(function () {
10+
const res = last_cache.get(username);
11+
if (res != null) resolve(res);
12+
else reject({ status: 500, error: "Codeforces Server Error" });
13+
}, 2000);
14+
fetch()
15+
.then((result) => {
16+
clearTimeout(timeoutID);
17+
resolve(result);
18+
})
19+
.catch((error) => {
20+
if (error.status === 400){
21+
clearTimeout(timeoutID);
22+
reject(error);
23+
}
24+
});
25+
});
26+
}
27+
428
function count_submissions(submissions) {
529
let alreadySolved = {};
630
let problemID;
@@ -16,22 +40,29 @@ function count_submissions(submissions) {
1640
}
1741

1842
export function get_rating(username, cache_seconds) {
19-
return new Promise((resolve, reject) => {
20-
api
21-
.get(`/user.info?handles=${username}`, {
22-
cache: {
23-
ttl: cache_seconds * 1000,
24-
},
25-
})
26-
.then((response) => {
27-
resolve(response.data.result[0].rating || 0);
28-
})
29-
.catch((error) => {
30-
if (error.response.status === 400)
31-
reject({ status: 400, error: "Codeforces Handle Not Found" });
32-
else reject({ status: 500, error: "Codeforces Server Error" });
33-
});
34-
});
43+
return fetch_error_handler(
44+
() =>
45+
new Promise((resolve, reject) => {
46+
api
47+
.get(`/user.info?handles=${username}`, {
48+
cache: {
49+
ttl: cache_seconds * 1000,
50+
},
51+
})
52+
.then((response) => {
53+
const res = response.data.result[0].rating || 0;
54+
last_rating_cache.set(username, res);
55+
resolve(res);
56+
})
57+
.catch((error) => {
58+
if (error.response.status === 400)
59+
reject({ status: 400, error: "Codeforces Handle Not Found" });
60+
else reject({ status: 500, error: "Codeforces Server Error" });
61+
});
62+
}),
63+
username,
64+
last_rating_cache
65+
);
3566
}
3667

3768
export function get_stats(username, cache_seconds) {
@@ -40,53 +71,61 @@ export function get_stats(username, cache_seconds) {
4071
ttl: cache_seconds * 1000,
4172
},
4273
};
43-
return new Promise((resolve, reject) => {
44-
Promise.all([
45-
api.get(`/user.info?handles=${username}`, apiConfig),
46-
api.get(`/user.rating?handle=${username}`, apiConfig),
47-
api.get(`/user.status?handle=${username}`, apiConfig),
48-
])
49-
.then((responses) => {
50-
let {
51-
firstName,
52-
lastName,
53-
rating,
54-
rank,
55-
maxRank,
56-
maxRating,
57-
friendOfCount,
58-
contribution,
59-
} = responses[0].data.result[0];
74+
return fetch_error_handler(
75+
() =>
76+
new Promise((resolve, reject) => {
77+
Promise.all([
78+
api.get(`/user.info?handles=${username}`, apiConfig),
79+
api.get(`/user.rating?handle=${username}`, apiConfig),
80+
api.get(`/user.status?handle=${username}`, apiConfig),
81+
])
82+
.then((responses) => {
83+
let {
84+
firstName,
85+
lastName,
86+
rating,
87+
rank,
88+
maxRank,
89+
maxRating,
90+
friendOfCount,
91+
contribution,
92+
} = responses[0].data.result[0];
6093

61-
rating = rating ? rating : 0;
62-
maxRating = maxRating ? maxRating : 0;
63-
rank = rank ? capitalize(rank) : "Unrated";
64-
maxRank = maxRank ? capitalize(maxRank) : "Unrated";
94+
rating = rating ? rating : 0;
95+
maxRating = maxRating ? maxRating : 0;
96+
rank = rank ? capitalize(rank) : "Unrated";
97+
maxRank = maxRank ? capitalize(maxRank) : "Unrated";
6598

66-
const fullName = `${firstName} ${lastName}`
67-
.replace("undefined", "")
68-
.replace("undefined", "")
69-
.trim();
70-
const contestsCount = responses[1].data.result.length;
71-
const problemsSolved = count_submissions(responses[2].data.result);
99+
const fullName = `${firstName} ${lastName}`
100+
.replace("undefined", "")
101+
.replace("undefined", "")
102+
.trim();
103+
const contestsCount = responses[1].data.result.length;
104+
const problemsSolved = count_submissions(responses[2].data.result);
72105

73-
resolve({
74-
username,
75-
fullName,
76-
rating,
77-
maxRating,
78-
rank,
79-
maxRank,
80-
contestsCount,
81-
problemsSolved,
82-
friendOfCount,
83-
contribution,
84-
});
85-
})
86-
.catch((error) => {
87-
if (error.response.status === 400)
88-
reject({ status: 400, error: "Codeforces Handle Not Found" });
89-
else reject({ status: 500, error: "Codeforces Server Error" });
90-
});
91-
});
106+
const res = {
107+
username,
108+
fullName,
109+
rating,
110+
maxRating,
111+
rank,
112+
maxRank,
113+
contestsCount,
114+
problemsSolved,
115+
friendOfCount,
116+
contribution,
117+
};
118+
119+
last_stats_cache.set(username, res);
120+
resolve(res);
121+
})
122+
.catch((error) => {
123+
if (error.response.status === 400)
124+
reject({ status: 400, error: "Codeforces Handle Not Found" });
125+
else reject({ status: 500, error: "Codeforces Server Error" });
126+
});
127+
}),
128+
username,
129+
last_stats_cache
130+
);
92131
}

0 commit comments

Comments
 (0)