-
Notifications
You must be signed in to change notification settings - Fork 1
/
recalculate_leaderboard.js
177 lines (164 loc) · 6.14 KB
/
recalculate_leaderboard.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
const serviceAccount = require('./service.json');
const admin = require('firebase-admin');
const fs = require('fs');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const db = admin.firestore();
(async function () {
const groupID = 'UyefpawGOgGFh14ptihn';
const group = (await db.collection('groups').doc(groupID).get()).data();
const leaderboardData = {};
const detailedLeaderboardData = {};
const postsSnapshot = db
.collection('groups')
.doc(groupID)
.collection('posts');
postsSnapshot
.get()
.then(async resp => {
const posts = [];
resp.forEach(x => posts.push(x));
for await (let postDoc of posts) {
if (postDoc.data().type === 'assignment') {
const problemsSnapshot = db
.collection('groups')
.doc(groupID)
.collection('posts')
.doc(postDoc.id)
.collection('problems');
await problemsSnapshot.get().then(async resp => {
const problems = [];
resp.forEach(x => problems.push(x));
for await (let problemDoc of problems) {
const submissionsSnapshot = db
.collection('groups')
.doc(groupID)
.collection('posts')
.doc(postDoc.id)
.collection('problems')
.doc(problemDoc.id)
.collection('submissions')
.orderBy('timestamp', 'asc');
await submissionsSnapshot.get().then(resp => {
const submissions = [];
resp.forEach(x => submissions.push(x));
for (let submissionDoc of submissions) {
const data = submissionDoc.data();
if (group.memberIds.includes(data.userId)) {
if (!leaderboardData.hasOwnProperty(data.userId)) {
leaderboardData[data.userId] = { totalPoints: 0 };
detailedLeaderboardData[data.userId] = {};
}
if (
!leaderboardData[data.userId].hasOwnProperty(postDoc.id)
) {
leaderboardData[data.userId][postDoc.id] = {
totalPoints: 0,
};
detailedLeaderboardData[data.userId][postDoc.id] = {};
}
if (
detailedLeaderboardData[data.userId][postDoc.id][
data.problemId
]?.bestScore ??
-1000 <= data.result * problemDoc.data().points
) {
detailedLeaderboardData[data.userId][postDoc.id][
data.problemId
] = {
bestScore: data.result * problemDoc.data().points,
bestScoreStatus: data.status,
bestScoreTimestamp: data.timestamp,
bestScoreSubmissionId: submissionDoc.id,
};
}
leaderboardData[data.userId][postDoc.id][data.problemId] =
Math.max(
data.result * problemDoc.data().points,
leaderboardData[data.userId][postDoc.id][
data.problemId
] ?? 0
);
leaderboardData[data.userId][postDoc.id].totalPoints =
Object.keys(leaderboardData[data.userId][postDoc.id])
.filter(x => x !== 'totalPoints')
.reduce(
(acc, cur) =>
acc + leaderboardData[data.userId][postDoc.id][cur],
0
);
leaderboardData[data.userId].totalPoints = Object.keys(
leaderboardData[data.userId]
)
.filter(x => x !== 'totalPoints')
.reduce(
(acc, cur) =>
acc + leaderboardData[data.userId][cur].totalPoints,
0
);
}
}
});
}
});
}
}
})
.then(async () => {
async function deleteCollection(db, collectionPath, batchSize) {
const collectionRef = db.collection(collectionPath);
const query = collectionRef.orderBy('__name__').limit(batchSize);
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}
async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}
// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach(doc => {
batch.delete(doc.ref);
});
await batch.commit();
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}
await deleteCollection(db, `groups/${groupID}/leaderboard`, 500);
const batch = db.batch();
await Promise.all(
Object.keys(leaderboardData).map(async userId => {
const user = await admin.auth().getUser(userId);
batch.set(
db
.collection('groups')
.doc(groupID)
.collection('leaderboard')
.doc(userId),
{
...leaderboardData[userId],
details: detailedLeaderboardData[userId],
userInfo: {
uid: user.uid,
displayName: user.displayName,
photoURL: user.photoURL,
},
}
);
})
);
await batch.commit();
console.log(
'Done! Updated ' + Object.keys(leaderboardData).length + ' users'
);
});
})();