Skip to content

Commit

Permalink
fix: separate leaderboard query on users query (#891)
Browse files Browse the repository at this point in the history
* fix: count leaderboard entry as valid user

* fix: delegate field alias handling to helper

* fix: delete unused field

* fix: fix invalid id assignment

* fix: remove unused params

* fix: `users` query should return user if exists in only leaderboard or users table

* Revert "fix: `users` query should return user if exists in only leaderboard or users table"

This reverts commit 6371ce8.

* fix: only return if user exist

* fix: only return results if user exist

* fix: seperate leaderboard query on users query

* Update src/graphql/helpers.ts

* fix user query

* fix: optimize query

* Revert "fix: optimize query"

This reverts commit 6e854b3.

* Update src/graphql/operations/users.ts

* Query leaderboard only to users who don't have a created

* remove logs

* Use left join and COALESCE

* Update src/graphql/operations/users.ts

* chore: lint fix

---------

Co-authored-by: Wan Qi Chen <[email protected]>
  • Loading branch information
ChaituVR and wa0x6e committed Jul 21, 2024
1 parent 0d33534 commit c2cdbc5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
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;

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

0 comments on commit c2cdbc5

Please sign in to comment.