speed up getLots()
parent
f33ddb604f
commit
e955e5b43e
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ export async function handler(
|
|||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
offset: 0,
|
||||
includeLotOccupancyCount: false
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ export async function handler(
|
|||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
offset: 0,
|
||||
includeLotOccupancyCount: false
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ export async function handler(
|
|||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
offset: 0,
|
||||
includeLotOccupancyCount: true
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
sqlParameters.unshift(currentDate, currentDate);
|
||||
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
|
||||
? ''
|
||||
: ` limit ${options.limit.toString()} offset ${options.offset.toString()}`))
|
||||
.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()}`}`)
|
||||
.all(sqlParameters);
|
||||
if (options.limit === -1) {
|
||||
count = lots.length;
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
)
|
||||
|
||||
sqlParameters.unshift(currentDate, currentDate)
|
||||
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
|
||||
? ''
|
||||
: ` limit ${options.limit.toString()} offset ${options.offset.toString()}`)
|
||||
`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()}`
|
||||
}`
|
||||
)
|
||||
.all(sqlParameters)
|
||||
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
|
|
@ -51,7 +51,8 @@ async function _getWorkOrder(
|
|||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
offset: 0,
|
||||
includeLotOccupancyCount: false
|
||||
},
|
||||
database
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
|
|
@ -200,7 +200,8 @@ export async function getWorkOrderMilestones(
|
|||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
offset: 0,
|
||||
includeLotOccupancyCount: false
|
||||
},
|
||||
database
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -178,7 +178,8 @@ export async function getWorkOrders(
|
|||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
offset: 0,
|
||||
includeLotOccupancyCount: false
|
||||
},
|
||||
database
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue