From 35e8de4cfa69e0ebf4034531de5c77d82d0baeae Mon Sep 17 00:00:00 2001 From: Romain Thouvenin Date: Tue, 7 May 2024 16:14:30 +0200 Subject: [PATCH] Improve the driver search query to make use of the toDate index --- .../selector/passenger-oriented.selector.ts | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts b/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts index 611e053..a385727 100644 --- a/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts +++ b/src/modules/ad/core/application/queries/match/selector/passenger-oriented.selector.ts @@ -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}'`; }