Skip to content

Commit

Permalink
fix: update refresh spaces counters script (#406)
Browse files Browse the repository at this point in the history
* fix: update refresh spaces counters script

* Update counts for verified and unverified spaces seperately

---------

Co-authored-by: Wan <[email protected]>
  • Loading branch information
ChaituVR and wa0x6e authored Jul 17, 2024
1 parent 85399eb commit bc3686e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion scripts/refresh-space-leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function processVotes(space: string, start: number, end: number) {
`INSERT INTO leaderboard (space, user, vote_count, last_vote)
(SELECT space, voter AS user, COUNT(*) AS vote_count, MAX(created) AS last_vote
FROM votes
WHERE ${space ? 'WHERE space = ? AND' : ''} created >= ? AND created < ?
WHERE ${space ? 'space = ? AND' : ''} created >= ? AND created < ?
GROUP BY space, voter)
ON DUPLICATE KEY UPDATE vote_count = vote_count + VALUES(vote_count), last_vote = VALUES(last_vote)
`,
Expand Down
40 changes: 26 additions & 14 deletions scripts/refresh_spaces_counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,42 @@ import db from '../src/helpers/mysql';

// Usage: yarn ts-node scripts/refresh_spaces_counters.ts
async function main() {
const spaces = await db.queryAsync(`SELECT id FROM spaces`);
const spaces = await db.queryAsync(`SELECT COUNT(*) as count FROM spaces`);
console.log(`Found ${spaces[0].count} spaces`);

console.log(`Found ${spaces.length} spaces`);
const verifiedSpacesCount = await db.queryAsync(
'SELECT COUNT(*) as count FROM spaces WHERE verified = 1'
);
console.log(`Found ${verifiedSpacesCount[0].count} verified spaces`);

for (const i in spaces) {
const stats = await db.queryAsync(
`SELECT COUNT(voter) as vote_count FROM votes WHERE space = ?`,
[spaces[i].id]
);
const stat = stats[0];
await db.queryAsync(`UPDATE spaces SET vote_count = ? WHERE id = ?`, [
stat.vote_count,
spaces[i].id
]);
console.log(`${i} / ${spaces.length}`);
}
console.log('Updating vote count for verified spaces');
await db.queryAsync(
`UPDATE spaces SET vote_count = (
COALESCE((
SELECT SUM(vote_count) FROM leaderboard WHERE space = spaces.id GROUP BY space
), 0)
) WHERE verified = 1`
);

console.log('Updating vote count for unverified spaces');
await db.queryAsync(
`UPDATE spaces SET vote_count = (
COALESCE((
SELECT SUM(vote_count) FROM leaderboard WHERE space = spaces.id GROUP BY space
), 0)
) WHERE verified = 0`
);

console.log('Updating proposal count');
await db.queryAsync(
'UPDATE spaces SET proposal_count = (SELECT count(id) from proposals WHERE space = spaces.id)'
);

console.log('Updating follower count');
await db.queryAsync(
'UPDATE spaces SET follower_count = (SELECT count(id) from follows WHERE space = spaces.id)'
);
console.log('Done! ✅');
}

(async () => {
Expand Down

0 comments on commit bc3686e

Please sign in to comment.