Skip to content

Commit

Permalink
transitRouting: Move TransitRoutingService to the backend
Browse files Browse the repository at this point in the history
fixes #770

The `TransitRoutingService` class is moved to the backend and instead of
using the socket/fetch routes, it wraps the calls to the trRouting
backend to return the results.

The current implementation using socket or fetch route is not required
anymore, the direct socket routes for trRouting's route and
accessibility map functions are not required anymore and have been
removed.

Remove the `TransitRoutingServiceManager` class, which is not
necessary anymore.
  • Loading branch information
tahini committed Sep 20, 2024
1 parent 741482a commit 4bbe2ce
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 692 deletions.
3 changes: 0 additions & 3 deletions packages/chaire-lib-backend/jestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@
* License text available at https://opensource.org/licenses/MIT
*/
jest.mock('./src/config/shared/db.config');
import { enableAllMocks } from 'chaire-lib-common/lib/test';

enableAllMocks();
1 change: 1 addition & 0 deletions packages/chaire-lib-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@opentelemetry/resources": "^1.25.1",
"@opentelemetry/sdk-trace-node": "^1.25.1",
"@opentelemetry/sdk-trace-base": "^1.25.1",
"@turf/turf": "^6.3.0",
"@zeit/fetch-retry": "^5.0.1",
"bcryptjs": "^2.4.3",
"bytes": "^3.1.2",
Expand Down
6 changes: 2 additions & 4 deletions packages/chaire-lib-backend/src/services/routing/Routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import TrError from 'chaire-lib-common/lib/utils/TrError';

import { TripRoutingQueryAttributes, RoutingResultsByMode } from 'chaire-lib-common/lib/services/routing/types';
import { getRouteByMode } from 'chaire-lib-common/lib/services/routing/RoutingUtils';
import { routingServiceManager as trRoutingServiceManager } from 'chaire-lib-common/lib/services/transitRouting/TransitRoutingServiceManager';
import transitRoutingService from '../transitRouting/TransitRoutingService';
import { TransitMode, RoutingMode } from 'chaire-lib-common/lib/config/routingModes';
import { RouteResults } from 'chaire-lib-common/lib/services/routing/RoutingService';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/transitRouting/types';
Expand Down Expand Up @@ -94,14 +94,12 @@ const calculateTransit = async (
od: [GeoJSON.Feature<GeoJSON.Point>, GeoJSON.Feature<GeoJSON.Point>],
routingAttributes: TripRoutingQueryAttributes
): Promise<TrRoutingRouteResult> => {
// FIXME This code path will use a fake socket route to do the calculation. Move this code to the backend too
const trRoutingService = trRoutingServiceManager.getService();
const queryParams: TransitRouteQueryOptions = getTransitRouteQueryOptionsOrDefault(routingAttributes, od);

// Build an HostPort
// TODO reflect the comment in TrRoutingOdTrip.ts, we should manage the port in a better way
const hostPort: HostPort = { port: routingAttributes.routingPort };
return await trRoutingService.route(queryParams, hostPort);
return await transitRoutingService.route(queryParams, hostPort);
};

const calculateRoute = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import { default as mockedRoutingUtils } from 'chaire-lib-common/lib/test/services/routing/RoutingUtilsMock';
import { default as mockedTrRouting } from 'chaire-lib-common/lib/test/services/transitRouting/TransitRoutingServiceMock';
import { TestUtils } from 'chaire-lib-common/lib/test';

import { pathNoTransferRouteResult, pathOneTransferRouteResult } from 'chaire-lib-common/lib/test/services/transitRouting/TrRoutingConstantsStubs';
Expand All @@ -19,6 +17,17 @@ import { RoutingOrTransitMode } from 'chaire-lib-common/lib/config/routingModes'
import { UnimodalRoutingResultData } from 'chaire-lib-common/lib/services/routing/RoutingResult';
import TrError from 'chaire-lib-common/lib/utils/TrError';
import { TrRoutingV2 } from 'chaire-lib-common/src/api/TrRouting';
import transitRoutingService from '../../transitRouting/TransitRoutingService';
import { getRouteByMode } from 'chaire-lib-common/lib/services/routing/RoutingUtils';

jest.mock('chaire-lib-common/lib/services/routing/RoutingUtils', () => ({
getRouteByMode: jest.fn()
}));
const mockGetRouteByMode = getRouteByMode as jest.MockedFunction<typeof getRouteByMode>;
jest.mock('../../transitRouting/TransitRoutingService', () => ({
route: jest.fn()
}));
const mockedRouteFunction = transitRoutingService.route as jest.MockedFunction<typeof transitRoutingService.route>;

// A simple path without transfer
export const simplePathResult: TrRoutingRouteResult = {
Expand Down Expand Up @@ -72,14 +81,14 @@ describe('Routing Calculations', () => {
});

test('simple path without transfer no way point', async () => {
mockedTrRouting.mockRouteFunction.mockResolvedValue(simplePathResult);
mockedRoutingUtils.mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest)
mockedRouteFunction.mockResolvedValue(simplePathResult);
mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest)

const result = await Routing.calculate(attributes);

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');

expect(Object.keys(result)).toEqual(['transit', 'walking']);
const transitResult = result['transit'] as TransitRoutingResultData;
Expand All @@ -99,14 +108,14 @@ describe('Routing Calculations', () => {
});

test('simple path without transfer no way point', async () => {
mockedTrRouting.mockRouteFunction.mockResolvedValue(simplePathResult);
mockedRoutingUtils.mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest);
mockedRouteFunction.mockResolvedValue(simplePathResult);
mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest);

const result = await Routing.calculate(attributes);

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');

expect(Object.keys(result)).toEqual(['transit', 'walking']);
const transitResult = result['transit'] as TransitRoutingResultData;
Expand All @@ -127,17 +136,17 @@ describe('Routing Calculations', () => {

test('path with transfer no way point', async () => {

mockedTrRouting.mockRouteFunction.mockResolvedValue(transferPathResult);
mockedRoutingUtils.mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest);
mockedRouteFunction.mockResolvedValue(transferPathResult);
mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest);

const result = await Routing.calculate(attributes);

expect(Object.keys(result)).toEqual(['transit', 'walking']);
const transitResult = result['transit'] as TransitRoutingResultData;

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');

expect(transitResult.paths.length).toEqual(1);

Expand All @@ -156,17 +165,17 @@ describe('Routing Calculations', () => {
});

test('alternative path', async () => {
mockedTrRouting.mockRouteFunction.mockResolvedValue(alternativesResult);
mockedRoutingUtils.mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest);
mockedRouteFunction.mockResolvedValue(alternativesResult);
mockGetRouteByMode.mockResolvedValue(walkingRouteNoWaypointTest);

const result = await Routing.calculate(attributes);

expect(Object.keys(result)).toEqual(['transit', 'walking']);
const transitResult = result['transit'] as TransitRoutingResultData;

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');

expect(transitResult.paths.length).toEqual(2);

Expand All @@ -176,14 +185,14 @@ describe('Routing Calculations', () => {
});

test('alternative path 60 minutes duration', async () => {
mockedTrRouting.mockRouteFunction.mockResolvedValue(alternativesResult);
mockedRoutingUtils.mockGetRouteByMode.mockResolvedValue(walkingRoute60MinutesDurationTest);
mockedRouteFunction.mockResolvedValue(alternativesResult);
mockGetRouteByMode.mockResolvedValue(walkingRoute60MinutesDurationTest);

const result = await Routing.calculate(attributes);

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');

expect(Object.keys(result)).toEqual(['transit', 'walking']);
const transitResult = result['transit'] as TransitRoutingResultData;
Expand All @@ -198,16 +207,16 @@ describe('Routing Calculations', () => {
test('multiple modes, with correct results', async () => {
const routingModes = ['transit', 'cycling', 'walking', 'rail'] as RoutingOrTransitMode[];
attributes.routingModes = routingModes;
mockedTrRouting.mockRouteFunction.mockResolvedValue(alternativesResult);
mockedRoutingUtils.mockGetRouteByMode.mockResolvedValue(walkingRoute60MinutesDurationTest);
mockedRouteFunction.mockResolvedValue(alternativesResult);
mockGetRouteByMode.mockResolvedValue(walkingRoute60MinutesDurationTest);

const result = await Routing.calculate(attributes);

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(3);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'cycling');
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'rail');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(3);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'cycling');
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'rail');

expect(Object.keys(result)).toEqual(routingModes);
for (let i = 0; i < routingModes.length; i++) {
Expand All @@ -220,17 +229,17 @@ describe('Routing Calculations', () => {
test('multiple modes, with rejected results', async () => {
const routingModes = ['transit', 'cycling', 'walking', 'rail'] as RoutingOrTransitMode[];
attributes.routingModes = routingModes;
mockedTrRouting.mockRouteFunction.mockRejectedValue(new TrError('test', 'ERRORCODE'));
mockedRoutingUtils.mockGetRouteByMode.mockRejectedValueOnce('Just a string');
mockedRoutingUtils.mockGetRouteByMode.mockRejectedValue(new TrError('test', 'ERRORCODE'));
mockedRouteFunction.mockRejectedValue(new TrError('test', 'ERRORCODE'));
mockGetRouteByMode.mockRejectedValueOnce('Just a string');
mockGetRouteByMode.mockRejectedValue(new TrError('test', 'ERRORCODE'));

const result = await Routing.calculate(attributes);

expect(mockedTrRouting.mockRouteFunction).toHaveBeenCalledTimes(1);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledTimes(3);
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'cycling');
expect(mockedRoutingUtils.mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'rail');
expect(mockedRouteFunction).toHaveBeenCalledTimes(1);
expect(mockGetRouteByMode).toHaveBeenCalledTimes(3);
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'walking');
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'cycling');
expect(mockGetRouteByMode).toHaveBeenCalledWith(attributes.originGeojson, attributes.destinationGeojson, 'rail');

expect(Object.keys(result)).toEqual(routingModes);
for (let i = 0; i < routingModes.length; i++) {
Expand Down
Loading

0 comments on commit 4bbe2ce

Please sign in to comment.