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: separate leaderboard query on users query #891

Merged
merged 24 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c2fac2c
fix: count leaderboard entry as valid user
wa0x6e Jul 14, 2024
c712be1
fix: delegate field alias handling to helper
wa0x6e Jul 14, 2024
b13938f
fix: delete unused field
wa0x6e Jul 14, 2024
0b588e5
fix: fix invalid id assignment
wa0x6e Jul 14, 2024
45c77a9
fix: remove unused params
wa0x6e Jul 14, 2024
6371ce8
fix: `users` query should return user if exists in only leaderboard o…
wa0x6e Jul 14, 2024
82fcba0
Revert "fix: `users` query should return user if exists in only leade…
wa0x6e Jul 15, 2024
56dd7bb
Merge branch 'master' into fix-count-leaderboard-user-as-valid-user
wa0x6e Jul 15, 2024
e87880a
Merge branch 'master' into fix-count-leaderboard-user-as-valid-user
wa0x6e Jul 16, 2024
158a948
fix: only return if user exist
wa0x6e Jul 16, 2024
42c54bf
fix: only return results if user exist
wa0x6e Jul 16, 2024
9f86b38
fix: seperate leaderboard query on users query
ChaituVR Jul 16, 2024
cabd398
Update src/graphql/helpers.ts
ChaituVR Jul 16, 2024
3981a8c
fix user query
ChaituVR Jul 17, 2024
b8fd9d7
Merge branch 'fix-users-query' of https://github.com/snapshot-labs/sn…
ChaituVR Jul 17, 2024
6e854b3
fix: optimize query
wa0x6e Jul 18, 2024
95e7585
Revert "fix: optimize query"
wa0x6e Jul 18, 2024
9039399
Update src/graphql/operations/users.ts
wa0x6e Jul 18, 2024
b7e9313
Merge branch 'master' of https://github.com/snapshot-labs/snapshot-hu…
ChaituVR Jul 20, 2024
e67c2a6
Query leaderboard only to users who don't have a created
ChaituVR Jul 20, 2024
310fc37
remove logs
ChaituVR Jul 20, 2024
924ad1e
Use left join and COALESCE
ChaituVR Jul 20, 2024
64e3c5a
Update src/graphql/operations/users.ts
wa0x6e Jul 21, 2024
9f681a2
chore: lint fix
wa0x6e Jul 21, 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
23 changes: 8 additions & 15 deletions src/graphql/operations/user.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { capture } from '@snapshot-labs/snapshot-sentry';
import db from '../../helpers/mysql';
import log from '../../helpers/log';
import { formatUser, formatAddresses, PublicError } from '../helpers';
import { formatAddresses, PublicError } from '../helpers';
import users from './users';

export default async function (parent, args) {
const addresses = formatAddresses([args.id]);
if (!addresses.length) throw new PublicError('Invalid address');
const usersObject = await users(parent, {
first: 1,
skip: 0,
where: { id: addresses[0] }
});

const query = `SELECT u.* FROM users u WHERE id = ? LIMIT 1`;

try {
const users = await db.queryAsync(query, addresses[0]);
if (users.length === 1) return formatUser(users[0]);
return null;
} catch (e: any) {
log.error(`[graphql] user, ${JSON.stringify(e)}`);
capture(e, { args });
return Promise.reject(new Error('request failed'));
}
return usersObject[0] || null;
}
41 changes: 37 additions & 4 deletions src/graphql/operations/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default async function (parent, args) {
const whereQuery = buildWhereQuery(fields, 'u', where);
const queryStr = whereQuery.query;
const params: any[] = whereQuery.params;
const ids = (where.id_in || [where.id]).filter(Boolean);
if (Object.keys(where).length > 1) ids.length = 0;
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved

let orderBy = args.orderBy || 'created';
let orderDirection = args.orderDirection || 'desc';
Expand All @@ -27,19 +29,50 @@ export default async function (parent, args) {
const query = `
SELECT
u.*,
SUM(l.vote_count) as votesCount,
SUM(l.proposal_count) as proposalsCount,
COALESCE(SUM(l.vote_count), 0) as votesCount,
COALESCE(SUM(l.proposal_count), 0) as proposalsCount,
MAX(l.last_vote) as lastVote
FROM users u
INNER JOIN leaderboard l ON l.user = u.id
LEFT JOIN leaderboard l ON l.user = u.id
WHERE 1=1 ${queryStr}
GROUP BY u.id
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
`;
params.push(skip, first);
try {
const users = await db.queryAsync(query, params);
return users.map(user => formatUser(user));
ids.forEach(element => {
if (!users.find((u: any) => u.id === element)) {
users.push({ id: element });
}
});
if (!users.length) return [];

const usersWithOutCreated = users
.filter((u: any) => !u.created)
.map((u: any) => u.id);
if (usersWithOutCreated.length) {
const counts = await db.queryAsync(
`
SELECT
user,
COALESCE(SUM(vote_count), 0) as votesCount,
COALESCE(SUM(proposal_count) ,0) as proposalsCount,
MAX(last_vote) as lastVote
FROM leaderboard
WHERE user IN (?)
GROUP BY user
`,
[usersWithOutCreated]
);
counts.forEach((count: any) => {
const user = users.find((u: any) => u.id === count.user);
user.votesCount = count.votesCount;
user.proposalsCount = count.proposalsCount;
user.lastVote = count.lastVote;
});
}
return users.map(formatUser);
} catch (e: any) {
log.error(`[graphql] users, ${JSON.stringify(e)}`);
capture(e, { args });
Expand Down
14 changes: 1 addition & 13 deletions src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,6 @@ input UsersWhere {
created_gte: Int
created_lt: Int
created_lte: Int
vote_count: Int
vote_count_in: [Int]
vote_count_gt: Int
vote_count_gte: Int
vote_count_lt: Int
vote_count_lte: Int
proposal_count: Int
proposal_count_in: [Int]
proposal_count_gt: Int
proposal_count_gte: Int
proposal_count_lt: Int
proposal_count_lte: Int
}

input StatementsWhere {
Expand Down Expand Up @@ -575,7 +563,7 @@ type User {
twitter: String
lens: String
farcaster: String
created: Int!
created: Int
votesCount: Int
proposalsCount: Int
lastVote: Int
Expand Down
Loading