-
Notifications
You must be signed in to change notification settings - Fork 21
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: reduce query complexity for getting total grants #1848
Changes from 4 commits
90a0c2f
c3e5829
4565daa
13a4c60
cfc10d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -507,6 +507,47 @@ function buildFiltersQuery(queryBuilder, filters, agencyId) { | |
); | ||
} | ||
|
||
function grantsQuery(queryBuilder, filters, agencyId, orderingParams, paginationParams) { | ||
as1729 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (filters) { | ||
if (filters.reviewStatuses?.length) { | ||
queryBuilder.join(TABLES.grants_interested, `${TABLES.grants}.grant_id`, `${TABLES.grants_interested}.grant_id`) | ||
.join(TABLES.interested_codes, `${TABLES.interested_codes}.id`, `${TABLES.grants_interested}.interested_code_id`); | ||
} | ||
if (parseInt(filters.assignedToAgencyId, 10) >= 0) { | ||
queryBuilder.join(TABLES.assigned_grants_agency, `${TABLES.grants}.grant_id`, `${TABLES.assigned_grants_agency}.grant_id`); | ||
} | ||
buildKeywordQuery(queryBuilder, filters.includeKeywords, filters.excludeKeywords); | ||
buildFiltersQuery(queryBuilder, filters, agencyId); | ||
} | ||
if (orderingParams.orderBy && orderingParams.orderBy !== 'undefined') { | ||
if (orderingParams.orderBy.includes('interested_agencies')) { | ||
// Only perform the join if it was not already performed above. | ||
if (!filters.reviewStatuses?.length) { | ||
queryBuilder.leftJoin(TABLES.grants_interested, `${TABLES.grants}.grant_id`, `${TABLES.grants_interested}.grant_id`); | ||
} | ||
const orderArgs = orderingParams.orderBy.split('|'); | ||
queryBuilder.orderBy(`${TABLES.grants_interested}.grant_id`, orderArgs[1]); | ||
queryBuilder.orderBy(`${TABLES.grants}.grant_id`, orderArgs[1]); | ||
} else if (orderingParams.orderBy.includes('viewed_by')) { | ||
const orderArgs = orderingParams.orderBy.split('|'); | ||
queryBuilder.leftJoin(TABLES.grants_viewed, `${TABLES.grants}.grant_id`, `${TABLES.grants_viewed}.grant_id`); | ||
queryBuilder.orderBy(`${TABLES.grants_viewed}.grant_id`, orderArgs[1]); | ||
queryBuilder.orderBy(`${TABLES.grants}.grant_id`, orderArgs[1]); | ||
} else { | ||
const orderArgs = orderingParams.orderBy.split('|'); | ||
const orderDirection = ((orderingParams.orderDesc === 'true') ? 'desc' : 'asc'); | ||
if (orderArgs.length > 1) { | ||
console.log(`Too many orderArgs: ${orderArgs}`); | ||
} | ||
queryBuilder.orderBy(orderArgs[0], orderDirection); | ||
} | ||
} | ||
if (paginationParams) { | ||
queryBuilder.limit(paginationParams.perPage); | ||
queryBuilder.offset((paginationParams.currentPage - 1) * paginationParams.perPage); | ||
} | ||
} | ||
|
||
/* | ||
filters: { | ||
reviewStatuses: List[Enum['Interested', 'Result', 'Rejected']], | ||
|
@@ -530,46 +571,20 @@ function buildFiltersQuery(queryBuilder, filters, agencyId) { | |
*/ | ||
async function getGrantsNew(filters, paginationParams, orderingParams, tenantId, agencyId) { | ||
console.log(filters, paginationParams, orderingParams, tenantId, agencyId); | ||
const { data, pagination } = await knex(TABLES.grants) | ||
const data = await knex(TABLES.grants) | ||
.select(`${TABLES.grants}.*`) | ||
.distinct() | ||
.modify((queryBuilder) => { | ||
if (filters) { | ||
if (filters.reviewStatuses?.length) { | ||
queryBuilder.join(TABLES.grants_interested, `${TABLES.grants}.grant_id`, `${TABLES.grants_interested}.grant_id`) | ||
.join(TABLES.interested_codes, `${TABLES.interested_codes}.id`, `${TABLES.grants_interested}.interested_code_id`); | ||
} | ||
if (parseInt(filters.assignedToAgencyId, 10) >= 0) { | ||
queryBuilder.join(TABLES.assigned_grants_agency, `${TABLES.grants}.grant_id`, `${TABLES.assigned_grants_agency}.grant_id`); | ||
} | ||
buildKeywordQuery(queryBuilder, filters.includeKeywords, filters.excludeKeywords); | ||
buildFiltersQuery(queryBuilder, filters, agencyId); | ||
} | ||
if (orderingParams.orderBy && orderingParams.orderBy !== 'undefined') { | ||
if (orderingParams.orderBy.includes('interested_agencies')) { | ||
// Only perform the join if it was not already performed above. | ||
if (!filters.reviewStatuses?.length) { | ||
queryBuilder.leftJoin(TABLES.grants_interested, `${TABLES.grants}.grant_id`, `${TABLES.grants_interested}.grant_id`); | ||
} | ||
const orderArgs = orderingParams.orderBy.split('|'); | ||
queryBuilder.orderBy(`${TABLES.grants_interested}.grant_id`, orderArgs[1]); | ||
queryBuilder.orderBy(`${TABLES.grants}.grant_id`, orderArgs[1]); | ||
} else if (orderingParams.orderBy.includes('viewed_by')) { | ||
const orderArgs = orderingParams.orderBy.split('|'); | ||
queryBuilder.leftJoin(TABLES.grants_viewed, `${TABLES.grants}.grant_id`, `${TABLES.grants_viewed}.grant_id`); | ||
queryBuilder.orderBy(`${TABLES.grants_viewed}.grant_id`, orderArgs[1]); | ||
queryBuilder.orderBy(`${TABLES.grants}.grant_id`, orderArgs[1]); | ||
} else { | ||
const orderArgs = orderingParams.orderBy.split('|'); | ||
const orderDirection = ((orderingParams.orderDesc === 'true') ? 'desc' : 'asc'); | ||
if (orderArgs.length > 1) { | ||
console.log(`Too many orderArgs: ${orderArgs}`); | ||
} | ||
queryBuilder.orderBy(orderArgs[0], orderDirection); | ||
} | ||
} | ||
}) | ||
.paginate(paginationParams); | ||
.modify((qb) => grantsQuery(qb, filters, agencyId, orderingParams, paginationParams)); | ||
|
||
const counts = await knex(TABLES.grants) | ||
.distinct() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is mostly a nit because I don't think it will affect performance too much, but I think you can remove the Removing the Comparison:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: On second glance, it appears the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with the perf improvement here. One concern of removing the Edit: I don't think we should do this given that the original query does not have any group-by clauses and relies on the |
||
.modify((qb) => grantsQuery(qb, filters, agencyId, { orderBy: undefined }, null)) | ||
.count('grant_id as total_grants'); | ||
|
||
const pagination = { | ||
total: counts[0].total_grants, | ||
lastPage: Math.ceil(parseInt(counts[0].total_grants, 10) / parseInt(paginationParams.perPage, 10)), | ||
}; | ||
|
||
const dataWithAgency = await enhanceGrantData(tenantId, data); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
grantsQuery
has 5 arguments (exceeds 4 allowed). Consider refactoring.