speed up getLots()

deepsource-autofix-76c6eb20
Dan Gowans 2023-03-28 11:11:44 -04:00
parent f33ddb604f
commit e955e5b43e
17 changed files with 95 additions and 60 deletions

View File

@ -2,7 +2,8 @@ import { getLots } from '../../helpers/lotOccupancyDB/getLots.js';
export async function handler(request, response) { export async function handler(request, response) {
const result = await getLots(request.body, { const result = await getLots(request.body, {
limit: request.body.limit, limit: request.body.limit,
offset: request.body.offset offset: request.body.offset,
includeLotOccupancyCount: true
}); });
response.json({ response.json({
count: result.count, count: result.count,

View File

@ -5,7 +5,8 @@ import { getLots } from '../../helpers/lotOccupancyDB/getLots.js'
export async function handler(request: Request, response: Response): Promise<void> { export async function handler(request: Request, response: Response): Promise<void> {
const result = await getLots(request.body, { const result = await getLots(request.body, {
limit: request.body.limit, limit: request.body.limit,
offset: request.body.offset offset: request.body.offset,
includeLotOccupancyCount: true
}) })
response.json({ response.json({

View File

@ -9,7 +9,8 @@ export async function handler(request, response) {
workOrderId: request.body.workOrderId workOrderId: request.body.workOrderId
}, { }, {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}); });
response.json({ response.json({
success, success,

View File

@ -21,7 +21,8 @@ export async function handler(
}, },
{ {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
} }
) )

View File

@ -6,7 +6,8 @@ export async function handler(request, response) {
workOrderId: request.body.workOrderId workOrderId: request.body.workOrderId
}, { }, {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}); });
response.json({ response.json({
success, success,

View File

@ -19,7 +19,8 @@ export async function handler(
}, },
{ {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
} }
) )

View File

@ -6,7 +6,8 @@ export async function handler(request, response) {
workOrderId: request.body.workOrderId workOrderId: request.body.workOrderId
}, { }, {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: true
}); });
response.json({ response.json({
success, success,

View File

@ -19,7 +19,8 @@ export async function handler(
}, },
{ {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: true
} }
) )

View File

@ -12,6 +12,7 @@ interface GetLotsFilters {
interface GetLotsOptions { interface GetLotsOptions {
limit: -1 | number; limit: -1 | number;
offset: number; offset: number;
includeLotOccupancyCount?: boolean;
} }
export declare function getLots(filters: GetLotsFilters, options: GetLotsOptions, connectedDatabase?: PoolConnection): Promise<{ export declare function getLots(filters: GetLotsFilters, options: GetLotsOptions, connectedDatabase?: PoolConnection): Promise<{
count: number; count: number;

View File

@ -60,31 +60,37 @@ export async function getLots(filters, options, connectedDatabase) {
} }
let lots = []; let lots = [];
if (options.limit === -1 || count > 0) { if (options.limit === -1 || count > 0) {
const includeLotOccupancyCount = options.includeLotOccupancyCount ?? true;
database.function('userFn_lotNameSortName', configFunctions.getProperty('settings.lot.lotNameSortNameFunction')); database.function('userFn_lotNameSortName', configFunctions.getProperty('settings.lot.lotNameSortNameFunction'));
if (includeLotOccupancyCount) {
sqlParameters.unshift(currentDate, currentDate); sqlParameters.unshift(currentDate, currentDate);
}
lots = database lots = database
.prepare('select l.lotId, l.lotName,' + .prepare(`select l.lotId, l.lotName,
' t.lotType,' + t.lotType,
' l.mapId, m.mapName, l.mapKey,' + l.mapId, m.mapName, l.mapKey,
' l.lotStatusId, s.lotStatus,' + l.lotStatusId, s.lotStatus
' ifnull(o.lotOccupancyCount, 0) as lotOccupancyCount' + ${includeLotOccupancyCount
' from Lots l' + ? ', ifnull(o.lotOccupancyCount, 0) as lotOccupancyCount'
' left join LotTypes t on l.lotTypeId = t.lotTypeId' + : ''}
' left join LotStatuses s on l.lotStatusId = s.lotStatusId' + from Lots l
' left join Maps m on l.mapId = m.mapId' + left join LotTypes t on l.lotTypeId = t.lotTypeId
(' left join (' + left join LotStatuses s on l.lotStatusId = s.lotStatusId
'select lotId, count(lotOccupancyId) as lotOccupancyCount' + left join Maps m on l.mapId = m.mapId
' from LotOccupancies' + ${includeLotOccupancyCount
' where recordDelete_timeMillis is null' + ? `left join (
' and occupancyStartDate <= ?' + select lotId, count(lotOccupancyId) as lotOccupancyCount
' and (occupancyEndDate is null or occupancyEndDate >= ?)' + from LotOccupancies
' group by lotId' + where recordDelete_timeMillis is null
') o on l.lotId = o.lotId') + and occupancyStartDate <= ?
sqlWhereClause + and (occupancyEndDate is null or occupancyEndDate >= ?)
' order by userFn_lotNameSortName(l.lotName), l.lotId' + group by lotId) o on l.lotId = o.lotId`
(options.limit === -1 : ''}
${sqlWhereClause}
order by userFn_lotNameSortName(l.lotName), l.lotId
${options.limit === -1
? '' ? ''
: ` limit ${options.limit.toString()} offset ${options.offset.toString()}`)) : ` limit ${options.limit.toString()} offset ${options.offset.toString()}`}`)
.all(sqlParameters); .all(sqlParameters);
if (options.limit === -1) { if (options.limit === -1) {
count = lots.length; count = lots.length;

View File

@ -23,6 +23,7 @@ interface GetLotsFilters {
interface GetLotsOptions { interface GetLotsOptions {
limit: -1 | number limit: -1 | number
offset: number offset: number
includeLotOccupancyCount?: boolean
} }
function buildWhereClause(filters: GetLotsFilters): { function buildWhereClause(filters: GetLotsFilters): {
@ -109,37 +110,50 @@ export async function getLots(
let lots: recordTypes.Lot[] = [] let lots: recordTypes.Lot[] = []
if (options.limit === -1 || count > 0) { if (options.limit === -1 || count > 0) {
const includeLotOccupancyCount = options.includeLotOccupancyCount ?? true
database.function( database.function(
'userFn_lotNameSortName', 'userFn_lotNameSortName',
configFunctions.getProperty('settings.lot.lotNameSortNameFunction') configFunctions.getProperty('settings.lot.lotNameSortNameFunction')
) )
if (includeLotOccupancyCount) {
sqlParameters.unshift(currentDate, currentDate) sqlParameters.unshift(currentDate, currentDate)
}
lots = database lots = database
.prepare( .prepare(
'select l.lotId, l.lotName,' + `select l.lotId, l.lotName,
' t.lotType,' + t.lotType,
' l.mapId, m.mapName, l.mapKey,' + l.mapId, m.mapName, l.mapKey,
' l.lotStatusId, s.lotStatus,' + l.lotStatusId, s.lotStatus
' ifnull(o.lotOccupancyCount, 0) as lotOccupancyCount' + ${
' from Lots l' + includeLotOccupancyCount
' left join LotTypes t on l.lotTypeId = t.lotTypeId' + ? ', ifnull(o.lotOccupancyCount, 0) as lotOccupancyCount'
' left join LotStatuses s on l.lotStatusId = s.lotStatusId' + : ''
' left join Maps m on l.mapId = m.mapId' + }
(' left join (' + from Lots l
'select lotId, count(lotOccupancyId) as lotOccupancyCount' + left join LotTypes t on l.lotTypeId = t.lotTypeId
' from LotOccupancies' + left join LotStatuses s on l.lotStatusId = s.lotStatusId
' where recordDelete_timeMillis is null' + left join Maps m on l.mapId = m.mapId
' and occupancyStartDate <= ?' + ${
' and (occupancyEndDate is null or occupancyEndDate >= ?)' + includeLotOccupancyCount
' group by lotId' + ? `left join (
') o on l.lotId = o.lotId') + select lotId, count(lotOccupancyId) as lotOccupancyCount
sqlWhereClause + from LotOccupancies
' order by userFn_lotNameSortName(l.lotName), l.lotId' + where recordDelete_timeMillis is null
(options.limit === -1 and occupancyStartDate <= ?
and (occupancyEndDate is null or occupancyEndDate >= ?)
group by lotId) o on l.lotId = o.lotId`
: ''
}
${sqlWhereClause}
order by userFn_lotNameSortName(l.lotName), l.lotId
${
options.limit === -1
? '' ? ''
: ` limit ${options.limit.toString()} offset ${options.offset.toString()}`) : ` limit ${options.limit.toString()} offset ${options.offset.toString()}`
}`
) )
.all(sqlParameters) .all(sqlParameters)

View File

@ -25,7 +25,8 @@ async function _getWorkOrder(sql, workOrderIdOrWorkOrderNumber, options, connect
workOrderId: workOrder.workOrderId workOrderId: workOrder.workOrderId
}, { }, {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}, database); }, database);
workOrder.workOrderLots = workOrderLotsResults.lots; workOrder.workOrderLots = workOrderLotsResults.lots;
const workOrderLotOccupanciesResults = await getLotOccupancies({ const workOrderLotOccupanciesResults = await getLotOccupancies({

View File

@ -51,7 +51,8 @@ async function _getWorkOrder(
}, },
{ {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}, },
database database
) )

View File

@ -120,7 +120,8 @@ export async function getWorkOrderMilestones(filters, options, connectedDatabase
workOrderId: workOrderMilestone.workOrderId workOrderId: workOrderMilestone.workOrderId
}, { }, {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}, database); }, database);
workOrderMilestone.workOrderLots = workOrderLotsResults.lots; workOrderMilestone.workOrderLots = workOrderLotsResults.lots;
const lotOccupancies = await getLotOccupancies({ const lotOccupancies = await getLotOccupancies({

View File

@ -200,7 +200,8 @@ export async function getWorkOrderMilestones(
}, },
{ {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}, },
database database
) )

View File

@ -111,7 +111,8 @@ export async function getWorkOrders(filters, options, connectedDatabase) {
workOrderId: workOrder.workOrderId workOrderId: workOrder.workOrderId
}, { }, {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}, database); }, database);
workOrder.workOrderLots = workOrderLotsResults.lots; workOrder.workOrderLots = workOrderLotsResults.lots;
} }

View File

@ -178,7 +178,8 @@ export async function getWorkOrders(
}, },
{ {
limit: -1, limit: -1,
offset: 0 offset: 0,
includeLotOccupancyCount: false
}, },
database database
) )