Skip to content

Commit daa929f

Browse files
authored
Refresh leaderboard for all spaces (#399)
1 parent af2a9b2 commit daa929f

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

scripts/refresh-space-leaderboard.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@ import db from '../src/helpers/mysql';
55
// This script assumes there are no active proposals in the space
66

77
async function getFirstAndLastVote(space: string) {
8-
console.log('Getting first and last vote for space', space);
9-
const firstVoted = await db.queryAsync(
10-
'SELECT created FROM votes WHERE space = ? ORDER BY created ASC LIMIT 1',
11-
[space]
12-
);
13-
14-
if (!firstVoted.length) throw new Error('No votes found in the database');
15-
const firstVote = firstVoted[0].created as number;
16-
17-
const lastVote = (
18-
await db.queryAsync('SELECT created FROM votes WHERE space = ? ORDER BY created DESC LIMIT 1', [
19-
space
20-
])
21-
)[0].created;
8+
const startArg = process.argv.indexOf('--start');
9+
const endArg = process.argv.indexOf('--end');
10+
let start = startArg !== -1 ? parseInt(process.argv[startArg + 1]) : null;
11+
let end = endArg !== -1 ? parseInt(process.argv[endArg + 1]) : null;
2212

23-
console.log(
24-
'Will process votes from',
25-
new Date(firstVote * 1000),
26-
'to',
27-
new Date(lastVote * 1000)
28-
);
13+
console.log('Getting first and last vote for space', space);
14+
if (!start) {
15+
console.log('Start date is missing');
16+
const firstVoted = await db.queryAsync(
17+
`SELECT created FROM votes ${space ? 'WHERE space = ?' : ''} ORDER BY created ASC LIMIT 1`,
18+
space ? [space] : []
19+
);
20+
if (!firstVoted.length) throw new Error('No votes found in the database');
21+
start = firstVoted[0].created as number;
22+
}
23+
if (!end) {
24+
end = (
25+
await db.queryAsync(
26+
`SELECT created FROM votes ${space ? 'WHERE space = ?' : ''} ORDER BY created DESC LIMIT 1`,
27+
space ? [space] : []
28+
)
29+
)[0].created;
30+
}
31+
// @ts-ignore
32+
console.log('Will process votes from', new Date(start * 1000), 'to', new Date(end * 1000));
2933

30-
return { firstVote, lastVote };
34+
return { firstVote: start, lastVote: end };
3135
}
3236

3337
async function deleteLeaderboard(space: string) {
@@ -37,17 +41,17 @@ async function deleteLeaderboard(space: string) {
3741
}
3842

3943
async function processVotes(space: string, start: number, end: number) {
40-
console.log('Get votes from', new Date(start * 1000), 'to', new Date(end * 1000));
44+
console.log('Get votes from', new Date(start * 1000), start, 'to', new Date(end * 1000), end);
4145

4246
await db.queryAsync(
4347
`INSERT INTO leaderboard (space, user, vote_count, last_vote)
4448
(SELECT space, voter AS user, COUNT(*) AS vote_count, MAX(created) AS last_vote
4549
FROM votes
46-
WHERE space = ? AND created >= ? AND created < ?
47-
GROUP BY voter)
50+
WHERE ${space ? 'WHERE space = ? AND' : ''} created >= ? AND created < ?
51+
GROUP BY space, voter)
4852
ON DUPLICATE KEY UPDATE vote_count = vote_count + VALUES(vote_count), last_vote = VALUES(last_vote)
4953
`,
50-
[space, start, end]
54+
space ? [space, start, end] : [start, end]
5155
);
5256
}
5357

@@ -58,17 +62,17 @@ async function processProposalsCount(space: string) {
5862
INSERT INTO leaderboard (space, user, proposal_count)
5963
(SELECT space, author AS user, COUNT(*) AS proposal_count
6064
FROM proposals
61-
WHERE space = ?
62-
GROUP BY author)
65+
${space ? 'WHERE space = ?' : ''}
66+
GROUP BY space, author)
6367
ON DUPLICATE KEY UPDATE proposal_count = proposal_count + VALUES(proposal_count)
6468
`,
65-
[space]
69+
space ? [space] : []
6670
);
6771
console.log('Proposals count for space', space, 'has been processed');
6872
}
6973

7074
async function main(space) {
71-
await deleteLeaderboard(space);
75+
if (space) await deleteLeaderboard(space);
7276
const { firstVote, lastVote } = await getFirstAndLastVote(space);
7377

7478
// Process votes in chunks of 24 hours
@@ -77,6 +81,7 @@ async function main(space) {
7781
const end = start + 86400; // 24 hours
7882
await processVotes(space, start, end);
7983

84+
// @ts-ignore
8085
if (end > lastVote) break;
8186
start = end;
8287
}
@@ -85,6 +90,7 @@ async function main(space) {
8590
process.exit(0);
8691
}
8792

88-
const space = process.argv[process.argv.indexOf('--space') + 1];
89-
if (!space) throw new Error('Space ID is missing');
93+
const spaceArg = process.argv.indexOf('--space');
94+
const space = spaceArg !== -1 ? process.argv[spaceArg + 1] : null;
95+
if (spaceArg !== -1 && !space) throw new Error('Space ID is missing');
9096
main(space);

0 commit comments

Comments
 (0)