Skip to content

Commit

Permalink
fix: use faster single query instead of batch
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Jul 18, 2024
1 parent 85619a8 commit e2db5e0
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions src/helpers/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,46 +127,39 @@ function mapSpaces() {
}

async function loadSpaces() {
const limit = 25e3;
let newSpaces = {};
let start = 0;
const newSpaces = {};

Check failure on line 130 in src/helpers/spaces.ts

View workflow job for this annotation

GitHub Actions / lint / Lint

'newSpaces' is assigned a value but never used
const startTime = +Date.now();

while (true) {
const query = `
const query = `
SELECT id, settings, flagged, verified, turbo, hibernated, follower_count, proposal_count, vote_count
FROM spaces
WHERE deleted = 0
ORDER BY id ASC
LIMIT ${start}, ${limit}
`;
const results = await db.queryAsync(query);

if (!results.length) break;

const formattedSpaces = Object.fromEntries(
results.map(space => [
space.id,
{
...JSON.parse(space.settings),
flagged: space.flagged === 1,
verified: space.verified === 1,
turbo: space.turbo === 1,
hibernated: space.hibernated === 1,
follower_count: space.follower_count,
vote_count: space.vote_count,
proposal_count: space.proposal_count
}
])
);

newSpaces = { ...newSpaces, ...formattedSpaces };

start += limit;
}

spaces = newSpaces;
const results = await db.queryAsync(query);

spaces = Object.fromEntries(
results.map(space => [
space.id,
{
...JSON.parse(space.settings),
flagged: space.flagged === 1,
verified: space.verified === 1,
turbo: space.turbo === 1,
hibernated: space.hibernated === 1,
follower_count: space.follower_count,
vote_count: space.vote_count,
proposal_count: space.proposal_count
}
])
);

log.info(`[spaces] total spaces ${Object.keys(spaces).length}`);
log.info(
`[spaces] total spaces ${Object.keys(spaces).length}, in (${(
(+Date.now() - startTime) /
1000
).toFixed()}s)`
);
mapSpaces();
}

Expand Down

0 comments on commit e2db5e0

Please sign in to comment.