Skip to content

Commit

Permalink
Revert "fix: optimize query"
Browse files Browse the repository at this point in the history
This reverts commit 6e854b3.
  • Loading branch information
wa0x6e committed Jul 18, 2024
1 parent 6e854b3 commit 95e7585
Showing 1 changed file with 25 additions and 104 deletions.
129 changes: 25 additions & 104 deletions src/helpers/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,131 +133,52 @@ async function loadSpaces() {

async function getProposals() {
const ts = parseInt((Date.now() / 1e3).toFixed());

const allActivitiesQuery = `
SELECT
id,
proposal_count AS count
FROM spaces
`;

const results = Object.fromEntries(
(await db.queryAsync(allActivitiesQuery)).map(({ id, count }) => [
id,
{ count, count_7d: 0, active: 0 }
])
);

const recentActivityQuery = `
SELECT
space,
count(id) AS count
FROM proposals
WHERE created > (UNIX_TIMESTAMP() - 604800)
GROUP BY space
`;

(await db.queryAsync(recentActivityQuery)).forEach(({ space, count }) => {
if (results[space]) results[space].count_7d = count;
});

const activeQuery = `
SELECT
space,
count(id) AS count
FROM proposals
WHERE start < ? AND end > ? AND flagged = 0
GROUP BY space
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
`;

(await db.queryAsync(activeQuery, [ts, ts])).forEach(({ space, count }) => {
if (results[space]) results[space].active = count;
});

return results;
return await db.queryAsync(query, [ts, ts]);
}

async function getVotes() {
const allActivitiesQuery = `
SELECT
id,
vote_count AS count
FROM spaces
`;

const results = Object.fromEntries(
(await db.queryAsync(allActivitiesQuery)).map(({ id, count }) => [
id,
{ count, count_7d: 0 }
])
);

const recentActivityQuery = `
SELECT
space,
count(id) AS count
FROM votes
WHERE created > (UNIX_TIMESTAMP() - 604800)
GROUP BY space
const query = `
SELECT space, COUNT(id) as count,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM votes GROUP BY space
`;

(await db.queryAsync(recentActivityQuery)).forEach(({ space, count }) => {
if (results[space]) results[space].count_7d = count;
});

return results;
return await db.queryAsync(query);
}

async function getFollowers() {
const allActivitiesQuery = `
SELECT
id,
follower_count AS count
FROM spaces
`;

const results = Object.fromEntries(
(await db.queryAsync(allActivitiesQuery)).map(({ id, count }) => [
id,
{ count, count_7d: 0 }
])
);

const recentActivityQuery = `
SELECT
space,
count(id) AS count
FROM follows
WHERE created > (UNIX_TIMESTAMP() - 604800)
GROUP BY space
const query = `
SELECT space, COUNT(id) as count,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM follows GROUP BY space
`;

(await db.queryAsync(recentActivityQuery)).forEach(({ space, count }) => {
if (results[space]) results[space].count_7d = count;
});

return results;
return await db.queryAsync(query);
}

async function loadSpacesMetrics() {
const followersMetrics = await getFollowers();
for (const [space, metrics] of Object.entries(followersMetrics)) {
if (spaces[space]) spaceFollowers[space] = metrics;
}
followersMetrics.forEach(followers => {
if (spaces[followers.space]) spaceFollowers[followers.space] = followers;
});
log.info('[spaces] Followers metrics loaded');
mapSpaces();

const proposalsMetrics = await getProposals();
for (const [space, metrics] of Object.entries(proposalsMetrics)) {
if (spaces[space]) spaceProposals[space] = metrics;
}
proposalsMetrics.forEach(proposals => {
if (spaces[proposals.space]) spaceProposals[proposals.space] = proposals;
});
log.info('[spaces] Proposals metrics loaded');
mapSpaces();

const votesMetrics = await getVotes();
for (const [space, metrics] of Object.entries(votesMetrics)) {
if (spaces[space]) spaceVotes[space] = metrics;
}
votesMetrics.forEach(votes => {
if (spaces[votes.space]) spaceVotes[votes.space] = votes;
});
log.info('[spaces] Votes metrics loaded');
mapSpaces();
}
Expand Down

0 comments on commit 95e7585

Please sign in to comment.