Improve the driver search query to make use of the toDate index

This commit is contained in:
Romain Thouvenin 2024-05-07 16:14:30 +02:00
parent e2beba299b
commit 35e8de4cfa
1 changed files with 7 additions and 20 deletions

View File

@ -179,6 +179,8 @@ export class PassengerOrientedSelector extends Selector {
private _maxFromDate = (role: Role): string => {
if (role == Role.DRIVER) {
//When looking for a passenger, we add the duration of the driver route to the latest toDate
//to compute the maximum sensible passenger fromDate, in case the pickup date could be on the next day
const querySchedule = this.query.schedule;
// When there is no schedule (search whole day), we consider the driver accepts to depart until 23:59
const maxScheduleTime =
@ -201,26 +203,11 @@ export class PassengerOrientedSelector extends Selector {
private _minToDate = (role: Role): string => {
if (role == Role.PASSENGER) {
const querySchedule = this.query.schedule;
// When there is no schedule (search whole day), we consider the passenger accepts to depart from 00:00
const minScheduleTime =
querySchedule === undefined
? '00:00'
: querySchedule.reduce(
(min, s) => (s.time < min ? s.time : min),
'23:59',
);
const [h, m] = minScheduleTime.split(':');
const minToDate = new Date(this.query.fromDate);
minToDate.setHours(parseInt(h));
minToDate.setMinutes(parseInt(m));
return `(make_timestamp(\
${minToDate.getUTCFullYear()},\
${minToDate.getUTCMonth() + 1},\
${minToDate.getUTCDate()},\
${minToDate.getUTCHours()},\
${minToDate.getUTCMinutes()},0)\
- concat("driverDuration", ' second')::interval)::date`;
// When looking for a driver, we look for a toDate that is one day before the fromDate of the query
// so that the driver will be able to pick up the passenger even during a long trip that starts the day before
const oneDayBeforeFromDate = new Date(this.query.fromDate);
oneDayBeforeFromDate.setDate(oneDayBeforeFromDate.getDate() - 1);
return `'${oneDayBeforeFromDate.getUTCFullYear()}-${oneDayBeforeFromDate.getUTCMonth() + 1}-${oneDayBeforeFromDate.getUTCDate()}'`;
} else {
return `'${this.query.fromDate}'`;
}