Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimize spaces helpers #887

Merged
merged 9 commits into from
Jul 22, 2024
32 changes: 22 additions & 10 deletions src/helpers/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,40 @@ async function loadSpaces() {
async function getProposals() {
const ts = parseInt((Date.now() / 1e3).toFixed());
const query = `
SELECT space, COUNT(id) AS count,
COUNT(IF(start < ? AND end > ? AND flagged = 0, 1, NULL)) AS active,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM proposals GROUP BY space
SELECT
space,
spaces.proposal_count AS count,
COUNT(IF(start < ? AND end > ? AND flagged = 0, 1, NULL)) AS active,
COUNT(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) AS count_7d,
FROM proposals
JOIN spaces ON spaces.id = space
GROUP BY space
`;
return await db.queryAsync(query, [ts, ts]);
}

async function getVotes() {
const query = `
SELECT space, COUNT(id) as count,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM votes GROUP BY space
SELECT
space,
spaces.vote_count as count,
COUNT(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) AS count_7d
FROM votes
JOIN spaces ON spaces.id = space
GROUP BY space
`;
return await db.queryAsync(query);
}

async function getFollowers() {
const query = `
SELECT space, COUNT(id) as count,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM follows GROUP BY space
SELECT
space,
spaces.follower_count AS count,
COUNT(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) AS count_7d
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
FROM follows
JOIN spaces ON spaces.id = space
GROUP BY space
`;
return await db.queryAsync(query);
}
Expand Down
Loading