Skip to content

Commit a2154f6

Browse files
Yashaswini-K-Pgithub-actions[bot]jagdish-15
authored
Optimize sync engine for stale users (#220)
* perf: ingest inactive users list and recycle cached leaderboard stats * deleted log * style: auto-format code with Prettier (/format) * Simplify inactive users handling in sync-leaderboard --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Jagdish Prajapati <jagadishdrp@gmail.com>
1 parent 4d13e80 commit a2154f6

1 file changed

Lines changed: 45 additions & 9 deletions

File tree

scripts/sync-leaderboard.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,57 @@ async function computeRankChanges(currentSorted, filename) {
206206
}
207207

208208
const baseUrl = "https://leetcode-api-dun.vercel.app/";
209+
210+
const inactiveFilePath = path.join(DATA_DIR, "inactive-users.json");
211+
const inactiveUsersSet = new Set();
212+
try {
213+
if (fs.existsSync(inactiveFilePath)) {
214+
const rawInactive = fs.readFileSync(inactiveFilePath, "utf8");
215+
const inactiveData = JSON.parse(rawInactive);
216+
inactiveData.inactiveUsers.forEach((id) => inactiveUsersSet.add(id));
217+
console.log(
218+
`Loaded ${inactiveUsersSet.size} stale users into skip-filter lookup Set.`,
219+
);
220+
}
221+
} catch (err) {
222+
console.warn(
223+
"Warning: Could not parse inactive-users.json, proceeding without skips:",
224+
err.message,
225+
);
226+
}
227+
228+
const overallFilepath = path.join(DATA_DIR, "overall.json");
229+
let previousOverall = [];
230+
try {
231+
if (fs.existsSync(overallFilepath)) {
232+
previousOverall = JSON.parse(fs.readFileSync(overallFilepath, "utf8"));
233+
}
234+
} catch (err) {
235+
console.warn(
236+
"No previous overall.json found, cannot recycle stale records.",
237+
);
238+
}
239+
240+
const historyMap = new Map();
241+
previousOverall.forEach((oldUser) => {
242+
historyMap.set(oldUser.id, oldUser);
243+
});
244+
209245
const interval = 0;
210246
let overallData = [];
211247

212248
console.log(" ");
213249
console.log("Starting daily fetch...");
214250
for (const user of users) {
251+
if (inactiveUsersSet.has(user.id)) {
252+
const cache = historyMap.get(user.id);
253+
if (cache) {
254+
console.log(`${user.name}: recycled (inactive)`);
255+
overallData.push(cache);
256+
continue;
257+
}
258+
}
259+
215260
const data = await fetchData(baseUrl + user.id);
216261
if (!data) {
217262
console.log(`${user.name}: skipped (API error)`);
@@ -267,15 +312,6 @@ async function computeRankChanges(currentSorted, filename) {
267312
stableSortByScore(overallData);
268313
assignCompetitionRanks(overallData);
269314
console.log("Writing sorted daily data to overall file...");
270-
const overallFilepath = path.join(DATA_DIR, "overall.json");
271-
272-
let previousOverall = [];
273-
try {
274-
const rawPrevious = fs.readFileSync(overallFilepath, "utf8");
275-
previousOverall = JSON.parse(rawPrevious);
276-
} catch (err) {
277-
console.warn("No previous overall.json found, skipping diff.");
278-
}
279315

280316
await computeRankChanges(overallData, "overall.json");
281317
try {

0 commit comments

Comments
 (0)