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

feat: Store user space activty #127

Merged
merged 30 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8557794
feat: update user_space counter
wa0x6e Aug 24, 2023
f85c449
chore: update sql schema for tests
wa0x6e Aug 24, 2023
0fb5b34
fix: decrease proposals_count on proposal deletion
wa0x6e Aug 24, 2023
f74fff8
fix: updates votes_count on proposal deletion
wa0x6e Aug 24, 2023
1efe0fa
fix: add counters generation script
wa0x6e Aug 26, 2023
8225ee9
fix: extract counters update to actions.ts
wa0x6e Aug 27, 2023
9a2cbb8
fix: fix wrong import
wa0x6e Aug 27, 2023
36f85f6
fix: trigger query only when necessary
wa0x6e Aug 27, 2023
e9f4687
fix: rename table
wa0x6e Aug 27, 2023
c5799c0
fix: fix argument to always be an array of strings
wa0x6e Aug 31, 2023
c7991e6
fix: fix invalid SQL queries
wa0x6e Aug 31, 2023
1a0041f
refactor: batch the refreshProposalsCounters
wa0x6e Aug 31, 2023
211797a
fix: use PRIMARY KEY instead of UNIQUE KEY
wa0x6e Sep 8, 2023
217c908
chore: fix column name to follow convention
wa0x6e Sep 8, 2023
adb2a96
fix: update to process one space at a time
wa0x6e Sep 8, 2023
1e4d175
chore: remove useless prettier
wa0x6e Sep 25, 2023
bfec416
Merge branch 'master' into update-user-space-counters
wa0x6e Apr 1, 2024
1353add
Merge branch 'master' into update-user-space-counters
wa0x6e Apr 8, 2024
4a4ee97
refactor: better console log
wa0x6e Apr 8, 2024
ff8a647
refactor: trigger activities update alongside main query
wa0x6e Apr 8, 2024
84bbd6e
Update scripts/generate_user_space_activities_counters.ts
ChaituVR Apr 14, 2024
e2bda13
Merge branch 'master' into update-user-space-counters
ChaituVR Apr 14, 2024
562cd2a
fix: fix invalid argument type
wa0x6e Apr 14, 2024
8d2117b
Merge branch 'update-user-space-counters' of https://github.com/snaps…
wa0x6e Apr 14, 2024
65cdaa4
fix: allow counter refresh script to target down a single space
wa0x6e Apr 14, 2024
d599a98
Merge branch 'master' into update-user-space-counters
wa0x6e Apr 14, 2024
2a1d8ad
refactor: rename table to `leaderboard`
wa0x6e Apr 16, 2024
dd3fdb5
Merge branch 'update-user-space-counters' of https://github.com/snaps…
wa0x6e Apr 16, 2024
7ecb58e
fix: rename votes and proposals count column
wa0x6e Apr 16, 2024
fcff05c
Update src/writer/delete-space.ts
wa0x6e Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions scripts/refresh_leaderboard_counters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dotenv/config';
import { refreshProposalsCount, refreshVotesCount } from '../src/helpers/actions';
import db from '../src/helpers/mysql';

// Usage: yarn ts-node scripts/refresh_leaderboard_counters.ts [OPTIONAL-SPACE-ID]
async function main() {
const query = `SELECT id, name FROM spaces ${
process.argv[2] ? `WHERE id = '${process.argv[2]}'` : ''
}`;

const spaces: { id: string; name: string }[] = await db.queryAsync(query);

for (const index in spaces) {
console.log(
`Processing space #${spaces[index].id} (${spaces[index].name}) - ${+index + 1}/${
spaces.length
}`
);

const votesCountRes = await refreshVotesCount([spaces[index].id]);
console.log(
'Inserting/Updating vote_count - ',
`Affected: ${votesCountRes.affectedRows}`,
`Changed: ${votesCountRes.changedRows}`
);

const proposalsCountRes = await refreshProposalsCount([spaces[index].id]);
console.log(
'Inserting/Updating proposal_count',
`Affected: ${proposalsCountRes.affectedRows}`,
`Changed: ${proposalsCountRes.changedRows}`
);
}
}

(async () => {
try {
await main();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
35 changes: 32 additions & 3 deletions src/helpers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,37 @@ export async function getSpace(id: string, includeDeleted = false) {
};
}

export async function markSpaceAsDeleted(space: string) {
const query = 'UPDATE spaces SET deleted = ? WHERE id = ?';
export function refreshProposalsCount(spaces?: string[]) {
return db.queryAsync(
`
INSERT INTO leaderboard (proposal_count, user, space)
(SELECT * FROM (
SELECT COUNT(proposals.id) AS proposal_count, author, space
FROM proposals
JOIN spaces ON spaces.id = proposals.space
WHERE spaces.deleted = 0
${spaces ? ' AND space IN (?)' : ''}
GROUP BY author, space
) AS t)
ON DUPLICATE KEY UPDATE proposal_count = t.proposal_count
`,
spaces
);
}

await db.queryAsync(query, [1, space]);
export function refreshVotesCount(spaces: string[]) {
return db.queryAsync(
`
INSERT INTO leaderboard (vote_count, user, space)
(SELECT * FROM (
SELECT COUNT(votes.id) AS vote_count, voter, space
FROM votes
JOIN spaces ON spaces.id = votes.space
WHERE spaces.deleted = 0 AND space IN (?)
GROUP BY voter, space
) AS t)
ON DUPLICATE KEY UPDATE vote_count = t.vote_count
`,
spaces
);
}
21 changes: 15 additions & 6 deletions src/writer/delete-proposal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProposal, getSpace } from '../helpers/actions';
import { getProposal, getSpace, refreshVotesCount } from '../helpers/actions';
import { jsonParse } from '../helpers/utils';
import db from '../helpers/mysql';

Expand All @@ -20,11 +20,20 @@ export async function verify(body): Promise<any> {

export async function action(body): Promise<void> {
const msg = jsonParse(body.msg);
const proposal = await getProposal(msg.space, msg.payload.proposal);
const id = msg.payload.proposal;

const query = `
DELETE FROM proposals WHERE id = ? LIMIT 1;
DELETE FROM votes WHERE proposal = ?;
`;
await db.queryAsync(query, [id, id]);
await db.queryAsync(
`
DELETE FROM proposals WHERE id = ? LIMIT 1;
DELETE FROM votes WHERE proposal = ?;
UPDATE leaderboard
SET proposal_count = proposal_count - 1
WHERE user = ? AND space = ?
LIMIT 1;
`,
[id, id, proposal.author, msg.space]
);

await refreshVotesCount([msg.space]);
}
10 changes: 8 additions & 2 deletions src/writer/delete-space.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import snapshot from '@snapshot-labs/snapshot.js';
import { getSpace, markSpaceAsDeleted } from '../helpers/actions';
import db from '../helpers/mysql';
import { getSpace } from '../helpers/actions';
import { jsonParse, DEFAULT_NETWORK } from '../helpers/utils';
import { capture } from '@snapshot-labs/snapshot-sentry';
import log from '../helpers/log';
Expand All @@ -24,7 +25,12 @@ export async function action(body): Promise<void> {
const space = msg.space;

try {
await markSpaceAsDeleted(space);
const query = `
UPDATE spaces SET deleted = ? WHERE id = ? LIMIT 1;
DELETE FROM leaderboard WHERE space = ?;
`;

await db.queryAsync(query, [1, space, space]);
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
capture(e, { space });
log.warn('[writer] Failed to store settings', space, e);
Expand Down
10 changes: 8 additions & 2 deletions src/writer/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
flagged: +containsFlaggedLinks(msg.payload.body)
};

const query = 'INSERT INTO proposals SET ?; ';
await db.queryAsync(query, proposal);
const query = `
INSERT INTO proposals SET ?;
INSERT INTO leaderboard (space, user, proposal_count)
VALUES(?, ?, 1)
ON DUPLICATE KEY UPDATE proposal_count = proposal_count + 1
`;

await db.queryAsync(query, [proposal, space, author]);
}
10 changes: 9 additions & 1 deletion src/writer/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@ export async function action(body, ipfs, receipt, id, context): Promise<void> {
);
} else {
// Store vote in dedicated table
await db.queryAsync('INSERT INTO votes SET ?', params);
await db.queryAsync(
`
INSERT INTO votes SET ?;
INSERT INTO leaderboard (space, user, vote_count)
VALUES(?, ?, 1)
ON DUPLICATE KEY UPDATE vote_count = vote_count + 1
`,
[params, msg.space, voter]
);
}

// Update proposal scores and voters vp
Expand Down
11 changes: 11 additions & 0 deletions test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,14 @@ CREATE TABLE messages (
INDEX type (type),
INDEX receipt (receipt)
);

CREATE TABLE leaderboard (
user VARCHAR(64) NOT NULL,
space VARCHAR(64) NOT NULL,
vote_count SMALLINT UNSIGNED NOT NULL DEFAULT '0',
proposal_count SMALLINT UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY user_space (user,space),
INDEX space (space),
INDEX vote_count (vote_count),
INDEX proposal_count (proposal_count)
);
Loading