handle pagination on query

This commit is contained in:
sbriat 2023-09-27 08:57:34 +02:00
parent 3bc142f4f7
commit 5c802df529
10 changed files with 39 additions and 13 deletions

View File

@ -47,19 +47,16 @@ MAX_DETOUR_DURATION_RATIO=0.3
GEOROUTER_TYPE=graphhopper GEOROUTER_TYPE=graphhopper
# georouter url # georouter url
GEOROUTER_URL=http://localhost:8989 GEOROUTER_URL=http://localhost:8989
# default carpool departure time margin (in seconds)
# DEFAULT CARPOOL DEPARTURE TIME MARGIN (in seconds)
DEPARTURE_TIME_MARGIN=900 DEPARTURE_TIME_MARGIN=900
# default role
# DEFAULT ROLE
ROLE=passenger ROLE=passenger
# seats proposes as driver / requested as passenger
# SEATS PROPOSED AS DRIVER / REQUESTED AS PASSENGER
SEATS_PROPOSED=3 SEATS_PROPOSED=3
SEATS_REQUESTED=1 SEATS_REQUESTED=1
# accept only same frequency requests
# ACCEPT ONLY SAME FREQUENCY REQUESTS
STRICT_FREQUENCY=false STRICT_FREQUENCY=false
# default timezone # default timezone
TIMEZONE=Europe/Paris TIMEZONE=Europe/Paris
# number of matching results per page
PER_PAGE=10

View File

@ -16,4 +16,5 @@ export type DefaultParams = {
AZIMUTH_MARGIN: number; AZIMUTH_MARGIN: number;
MAX_DETOUR_DISTANCE_RATIO: number; MAX_DETOUR_DISTANCE_RATIO: number;
MAX_DETOUR_DURATION_RATIO: number; MAX_DETOUR_DURATION_RATIO: number;
PER_PAGE: number;
}; };

View File

@ -49,6 +49,10 @@ export class MatchQueryHandler implements IQueryHandler {
maxDetourDistanceRatio: this._defaultParams.MAX_DETOUR_DISTANCE_RATIO, maxDetourDistanceRatio: this._defaultParams.MAX_DETOUR_DISTANCE_RATIO,
maxDetourDurationRatio: this._defaultParams.MAX_DETOUR_DURATION_RATIO, maxDetourDurationRatio: this._defaultParams.MAX_DETOUR_DURATION_RATIO,
}) })
.setDefaultPagination({
page: 1,
perPage: this._defaultParams.PER_PAGE,
})
.setDatesAndSchedule(this.datetimeTransformer); .setDatesAndSchedule(this.datetimeTransformer);
await query.setRoutes(); await query.setRoutes();

View File

@ -33,8 +33,8 @@ export class MatchQuery extends QueryBase {
azimuthMargin?: number; azimuthMargin?: number;
maxDetourDistanceRatio?: number; maxDetourDistanceRatio?: number;
maxDetourDurationRatio?: number; maxDetourDurationRatio?: number;
readonly page?: number; page?: number;
readonly perPage?: number; perPage?: number;
driverRoute?: Route; driverRoute?: Route;
passengerRoute?: Route; passengerRoute?: Route;
backAzimuth?: number; backAzimuth?: number;
@ -61,8 +61,8 @@ export class MatchQuery extends QueryBase {
this.azimuthMargin = props.azimuthMargin; this.azimuthMargin = props.azimuthMargin;
this.maxDetourDistanceRatio = props.maxDetourDistanceRatio; this.maxDetourDistanceRatio = props.maxDetourDistanceRatio;
this.maxDetourDurationRatio = props.maxDetourDurationRatio; this.maxDetourDurationRatio = props.maxDetourDurationRatio;
this.page = props.page ?? 1; this.page = props.page;
this.perPage = props.perPage ?? 10; this.perPage = props.perPage;
this.originWaypoint = this.waypoints.filter( this.originWaypoint = this.waypoints.filter(
(waypoint: Waypoint) => waypoint.position == 0, (waypoint: Waypoint) => waypoint.position == 0,
)[0]; )[0];
@ -124,6 +124,12 @@ export class MatchQuery extends QueryBase {
return this; return this;
}; };
setDefaultPagination = (defaultPagination: DefaultPagination): MatchQuery => {
if (!this.page) this.page = defaultPagination.page;
if (!this.perPage) this.perPage = defaultPagination.perPage;
return this;
};
setDatesAndSchedule = ( setDatesAndSchedule = (
datetimeTransformer: DateTimeTransformerPort, datetimeTransformer: DateTimeTransformerPort,
): MatchQuery => { ): MatchQuery => {
@ -243,3 +249,8 @@ interface DefaultAlgorithmParameters {
maxDetourDistanceRatio: number; maxDetourDistanceRatio: number;
maxDetourDurationRatio: number; maxDetourDurationRatio: number;
} }
interface DefaultPagination {
page: number;
perPage: number;
}

View File

@ -18,6 +18,7 @@ const USE_AZIMUTH = true;
const AZIMUTH_MARGIN = 10; const AZIMUTH_MARGIN = 10;
const MAX_DETOUR_DISTANCE_RATIO = 0.3; const MAX_DETOUR_DISTANCE_RATIO = 0.3;
const MAX_DETOUR_DURATION_RATIO = 0.3; const MAX_DETOUR_DURATION_RATIO = 0.3;
const PER_PAGE = 10;
@Injectable() @Injectable()
export class DefaultParamsProvider implements DefaultParamsProviderPort { export class DefaultParamsProvider implements DefaultParamsProviderPort {
@ -81,5 +82,9 @@ export class DefaultParamsProvider implements DefaultParamsProviderPort {
this._configService.get('MAX_DETOUR_DURATION_RATIO') as string, this._configService.get('MAX_DETOUR_DURATION_RATIO') as string,
) )
: MAX_DETOUR_DURATION_RATIO, : MAX_DETOUR_DURATION_RATIO,
PER_PAGE:
this._configService.get('PER_PAGE') !== undefined
? parseInt(this._configService.get('PER_PAGE') as string)
: PER_PAGE,
}); });
} }

View File

@ -72,6 +72,7 @@ const mockDefaultParamsProvider: DefaultParamsProviderPort = {
AZIMUTH_MARGIN: 10, AZIMUTH_MARGIN: 10,
MAX_DETOUR_DISTANCE_RATIO: 0.3, MAX_DETOUR_DISTANCE_RATIO: 0.3,
MAX_DETOUR_DURATION_RATIO: 0.3, MAX_DETOUR_DURATION_RATIO: 0.3,
PER_PAGE: 10,
}; };
}, },
}; };

View File

@ -49,6 +49,7 @@ const defaultParams: DefaultParams = {
AZIMUTH_MARGIN: 10, AZIMUTH_MARGIN: 10,
MAX_DETOUR_DISTANCE_RATIO: 0.3, MAX_DETOUR_DISTANCE_RATIO: 0.3,
MAX_DETOUR_DURATION_RATIO: 0.3, MAX_DETOUR_DURATION_RATIO: 0.3,
PER_PAGE: 10,
}; };
const mockInputDateTimeTransformer: DateTimeTransformerPort = { const mockInputDateTimeTransformer: DateTimeTransformerPort = {

View File

@ -34,6 +34,8 @@ const mockConfigServiceWithDefaults = {
return 0.5; return 0.5;
case 'MAX_DETOUR_DURATION_RATIO': case 'MAX_DETOUR_DURATION_RATIO':
return 0.6; return 0.6;
case 'PER_PAGE':
return 15;
default: default:
return 'some_default_value'; return 'some_default_value';
} }
@ -96,6 +98,7 @@ describe('DefaultParamsProvider', () => {
expect(params.AZIMUTH_MARGIN).toBe(15); expect(params.AZIMUTH_MARGIN).toBe(15);
expect(params.MAX_DETOUR_DISTANCE_RATIO).toBe(0.5); expect(params.MAX_DETOUR_DISTANCE_RATIO).toBe(0.5);
expect(params.MAX_DETOUR_DURATION_RATIO).toBe(0.6); expect(params.MAX_DETOUR_DURATION_RATIO).toBe(0.6);
expect(params.PER_PAGE).toBe(15);
}); });
it('should provide default params if defaults are not set', async () => { it('should provide default params if defaults are not set', async () => {
@ -113,5 +116,6 @@ describe('DefaultParamsProvider', () => {
expect(params.AZIMUTH_MARGIN).toBe(10); expect(params.AZIMUTH_MARGIN).toBe(10);
expect(params.MAX_DETOUR_DISTANCE_RATIO).toBe(0.3); expect(params.MAX_DETOUR_DISTANCE_RATIO).toBe(0.3);
expect(params.MAX_DETOUR_DURATION_RATIO).toBe(0.3); expect(params.MAX_DETOUR_DURATION_RATIO).toBe(0.3);
expect(params.PER_PAGE).toBe(10);
}); });
}); });

View File

@ -29,6 +29,7 @@ const mockDefaultParamsProvider: DefaultParamsProviderPort = {
AZIMUTH_MARGIN: 10, AZIMUTH_MARGIN: 10,
MAX_DETOUR_DISTANCE_RATIO: 0.3, MAX_DETOUR_DISTANCE_RATIO: 0.3,
MAX_DETOUR_DURATION_RATIO: 0.3, MAX_DETOUR_DURATION_RATIO: 0.3,
PER_PAGE: 10,
}; };
}, },
}; };

View File

@ -29,6 +29,7 @@ const mockDefaultParamsProvider: DefaultParamsProviderPort = {
AZIMUTH_MARGIN: 10, AZIMUTH_MARGIN: 10,
MAX_DETOUR_DISTANCE_RATIO: 0.3, MAX_DETOUR_DISTANCE_RATIO: 0.3,
MAX_DETOUR_DURATION_RATIO: 0.3, MAX_DETOUR_DURATION_RATIO: 0.3,
PER_PAGE: 10,
}; };
}, },
}; };