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) {
const result = await getLots(request.body, {
limit: request.body.limit,
offset: request.body.offset
offset: request.body.offset,
includeLotOccupancyCount: true
});
response.json({
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> {
const result = await getLots(request.body, {
limit: request.body.limit,
offset: request.body.offset
offset: request.body.offset,
includeLotOccupancyCount: true
})
response.json({

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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