Skip to content

Commit

Permalink
Update counts for verified and unverified spaces seperately
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaituVR committed Jul 16, 2024
1 parent aa3366b commit f94c0fd
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions scripts/refresh_spaces_counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ 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) {
await db.queryAsync(
`UPDATE spaces SET vote_count = (
SELECT COALESCE(SUM(vote_count),0) FROM leaderboard WHERE space = ?
) WHERE id = ?`,
[spaces[i].id, 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(
Expand Down

0 comments on commit f94c0fd

Please sign in to comment.