-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuperUsers.js
181 lines (162 loc) · 5.03 KB
/
superUsers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const axios = require('axios');
const fs = require('fs');
const getEpochSecond = () => {
return Math.floor(new Date().getTime() / 1000);
}
const getRating = async (handle) => {
const response = await axios({
method: 'get',
url: `https://codeforces.com/api/user.rating?handle=${handle}`,
});
return response.data.result;
};
const getSubmissions = async (handle) => {
const response = await axios({
method: 'get',
url: `https://codeforces.com/api/user.status?handle=${handle}`,
});
return response.data.result;
}
const getEligibleUser = async (user) => {
const { handle } = user;
const now = getEpochSecond();
const observationTime = 12 * 365 * 24 * 60 * 60;
const thresholdTime = now - observationTime;
const cutoffTime = 75000 * 24 * 60 * 60;
const problemsLower = 27;
const ratingLower = 2600;
const ratingUpper = 3000;
let ratings = await getRating(handle);
ratings = ratings.filter(r => r.ratingUpdateTimeSeconds >= thresholdTime);
let startRating = ratings[0].newRating;
let startTime = 0;
let endTime = 0;
if (startRating > ratingLower){
return false;
}
for (const rating of ratings){
if (rating.newRating >= ratingLower && startTime === 0){
startTime = rating.ratingUpdateTimeSeconds;
}
if (rating.newRating >= ratingUpper && endTime === 0 && startTime != 0){
endTime = rating.ratingUpdateTimeSeconds;
break;
}
}
if (startTime === 0 || endTime === 0){
return false;
}
let allSubmissions = await getSubmissions(handle);
let preSubmissions = allSubmissions.filter(s => s.creationTimeSeconds < startTime && s.verdict === 'OK');
allSubmissions = allSubmissions.filter(s => s.creationTimeSeconds >= startTime && s.creationTimeSeconds <= endTime && s.verdict === 'OK' && s.author.participantType === 'PRACTICE');
let tagFreq = {};
let ratingFreq = {};
for (const submission of preSubmissions) {
for (const tag of submission.problem.tags){
if (tag in tagFreq){
tagFreq[tag] += 1;
} else {
tagFreq[tag] = 1;
}
}
const ratingVal = submission.problem.rating;
if (ratingVal in ratingFreq){
ratingFreq[ratingVal] += 1;
} else {
ratingFreq[ratingVal] = 1;
}
}
const problemIds = new Set();
let submissions = [];
for (const submission of allSubmissions) {
const key = getProblemKey(submission.problem);
if (problemIds.has(key)) {
continue;
}
problemIds.add(key);
submissions.push([key, submission.creationTimeSeconds, submission.problem.rating, submission.problem.tags]);
}
if (endTime - startTime <= cutoffTime && submissions.length >= problemsLower){
return [submissions, tagFreq, ratingFreq];
}
else {
return false;
}
}
const getProblemKey = (problem) => {
return `${problem.contestId}:${problem.index}`;
}
const getEligibleUsersInBatch = async (users, eligibleUsers, errCount, start, end, skippedUsers) => {
console.log(`Processing batch ${start} to ${end}`);
const last = Math.min(end, users.length);
for (let i = start; i < last; i++) {
const user = users[i];
try {
const isSuper = await getEligibleUser(user);
if (isSuper){
let problems = [...isSuper[0]];
user.problems = problems;
user.tagFreq = isSuper[1];
user.ratingFreq = isSuper[2];
eligibleUsers.push(user);
}
} catch (err) {
if (!errCount[i]) errCount[i] = 0;
errCount[i]++;
if (errCount[i] <= 6) {
await new Promise(resolve => setTimeout(resolve, 3000));
i--;
} else {
skippedUsers.push(user);
}
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log(`Processed batch ${start} to ${end}`);
}
const getAllEligibleUsers= async (startFrom, doSkipped = false) => {
const userFile = JSON.parse(fs.readFileSync('./cfusers.json'));
let users = [...userFile.result];
const errCount = {};
let eligibleUsers = [];
let skippedUsers = [];
if (startFrom) {
const checkpoint = JSON.parse(fs.readFileSync(`./checkpoints/checkpoint_${startFrom}.json`));
eligibleUsers = checkpoint.eligibleUsers;
skippedUsers = checkpoint.skippedUsers;
if (doSkipped) {
users = [...skippedUsers];
skippedUsers = [];
}
} else {
startFrom = 0;
}
console.log('Processing users:' + users.length);
const checkPointSize = 100;
const batchSize = 20;
let start = startFrom;
if (doSkipped) {
start = 0;
}
for (let i = start; i < users.length; i += checkPointSize) {
const toWrite = {
eligibleUsers: eligibleUsers,
skippedUsers: skippedUsers,
};
let checkpoint = startFrom + i;
fs.writeFileSync(`./checkpoints/checkpoint_${checkpoint}.json`, JSON.stringify(toWrite));
console.log(`Checkpoint upto ${checkpoint} written, skipped users: ${skippedUsers.length}, super users: ${eligibleUsers.length}`);
let tasks = [];
for (let j = 0; j < checkPointSize; j += batchSize) {
tasks.push(getEligibleUsersInBatch(users, eligibleUsers, errCount, i + j, i + j + batchSize, skippedUsers));
}
await Promise.allSettled(tasks);
}
const toWrite = {
eligibleUsers: eligibleUsers,
skippedUsers: skippedUsers,
};
fs.writeFileSync(`./checkpoints/checkpoint_${users.length}.json`, JSON.stringify(toWrite));
console.log('Done');
}
getAllEligibleUsers();