Skip to content

Commit

Permalink
trRouting: Move the types defined in TrRoutingService to their own …
Browse files Browse the repository at this point in the history
…files

This will allow to import those types without necessarily importing the
file containing the calculation functions.
  • Loading branch information
tahini committed Sep 20, 2024
1 parent bc94725 commit 61dcfe7
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getRouteByMode } from 'chaire-lib-common/lib/services/routing/RoutingUt
import { routingServiceManager as trRoutingServiceManager } from 'chaire-lib-common/lib/services/trRouting/TrRoutingServiceManager';
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/trRouting/TrRoutingService';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/types';
import { TransitRouteQueryOptions, HostPort } from 'chaire-lib-common/lib/api/TrRouting';

type TransitOrRouteCalculatorResult =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { default as mockedTrRouting } from 'chaire-lib-common/lib/test/services/
import { TestUtils } from 'chaire-lib-common/lib/test';

import { pathNoTransferRouteResult, pathOneTransferRouteResult } from 'chaire-lib-common/lib/test/services/trRouting/TrRoutingConstantsStubs';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/types';
import { Routing } from '../Routing';
import { validateAndCreateTripRoutingAttributes } from 'chaire-lib-common/lib/services/routing/RoutingAttributes';
import { TripRoutingQueryAttributes } from 'chaire-lib-common/lib/services/routing/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Preferences from '../../config/Preferences';
import { Route } from './RoutingService';
import { RoutingOrTransitMode, RoutingMode } from '../../config/routingModes';
import TrError, { ErrorMessage } from '../../utils/TrError';
import { TrRoutingRoute } from '../trRouting/TrRoutingService';
import { TrRoutingRoute } from '../trRouting/types';

export const pathIsRoute = (path: Route | TrRoutingRoute | undefined): path is Route => {
return typeof (path as any).distance === 'number';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _get from 'lodash/get';

import Preferences from '../../config/Preferences';
import { TrRoutingV2 } from '../../api/TrRouting';
import { TrRoutingRoute } from '../trRouting/TrRoutingService';
import { TrRoutingRoute } from '../trRouting/types';
import { Route, RouteResults } from './RoutingService';
import { getRouteByMode } from './RoutingUtils';
import TrError, { ErrorMessage } from '../../utils/TrError';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import { TrRoutingRoute } from './TrRoutingService';
import { TrRoutingRoute } from './types';
import { secondsSinceMidnightToTimeStr, secondsToMinutes } from '../../utils/DateTimeUtils';
import { TrRoutingV2 } from '../../api/TrRouting';

Expand Down
104 changes: 1 addition & 103 deletions packages/chaire-lib-common/src/services/trRouting/TrRoutingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ import serviceLocator from '../../utils/ServiceLocator';
import TrError from '../../utils/TrError';
import * as TrRoutingApi from '../../api/TrRouting';
import * as Status from '../../utils/Status';

export enum ErrorCodes {
NoRoutingFound = 'TRROUTING_NO_ROUTING_FOUND',
NoAccessAtOrigin = 'TRROUTING_NO_ROUTING_NO_ACCESS_AT_ORIGIN',
NoAccessAtDestination = 'TRROUTING_NOT_ROUTING_NO_ACCESS_AT_DESTINATION',
NoAccessAtOriginAndDestination = 'TRROUTING_NOT_ROUTING_NO_ACCESS_AT_ORIGIN_AND_DESTINATION',
NoServiceAtOrigin = 'TRROUTING_NO_ROUTING_NO_SERVICE_FROM_ORIGIN',
NoServiceAtDestination = 'TRROUTING_NO_ROUTING_NO_SERVICE_TO_DESTINATION',
ServerNotRunning = 'TRROUTING_SERVER_NOT_RUNNING',
NoAccessAtPlace = 'TRROUTING_NOT_ROUTING_NO_ACCESS_AT_PLACE',
NoServiceAtPlace = 'TRROUTING_NO_ROUTING_NO_SERVICE_AT_PLACE',
OtherError = 'TRROUTING_ERROR_UNKNOWN',
MissingData = 'TRROUTING_MISSING_DATA',
DataError = 'TRROUTING_INVALID_DATA',
QueryError = 'TRROUTING_QUERY_ERROR'
}
import { ErrorCodes, TrRoutingResultAccessibilityMap, TrRoutingRouteResult } from './types';

const errorCodeByReason = (
reason: TrRoutingApi.TrRoutingNoRoutingReason | TrRoutingApi.TrRoutingV2.NoRoutingAccessibilityReason
Expand All @@ -50,93 +35,6 @@ const errorCodeByReason = (
}
};

interface TrRoutingBaseQueryOptions {
/**
* Minimum waiting time, in seconds
*/
minWaitingTime: number;
/**
* Maximum walking time from origin, in seconds
*/
maxAccessTravelTime: number;
/**
* Maximum walking time to reach destination, in seconds
*/
maxEgressTravelTime: number;
/**
* Maximum transfer time, in seconds
*/
maxTransferTravelTime: number;
/**
* Maximum total travel time, in seconds
*/
maxTravelTime: number;
/**
* The UUID of the scenario to use for the calculation
*/
scenarioId: string;
/**
* Arrival or departure time of the trip, in seconds since midnight
*/
timeOfTrip: number;
/**
* Whether the time is arrival or departure time
*/
timeOfTripType: 'arrival' | 'departure';
/**
* Maximum wait time at first transit stop
*/
maxFirstWaitingTime?: number;
}

export interface RoutingQueryOptions extends TrRoutingBaseQueryOptions {
/**
* Whether to calculate alternatives
*/
alternatives?: boolean;
/**
* An array containing the origin and destination points. Any of the 2 can
* be undefined if there is no origin or destination (for example
* accessibility maps)
*/
originDestination?: [GeoJSON.Feature<GeoJSON.Point>, GeoJSON.Feature<GeoJSON.Point>];
/**
* UUID of od trip
*
* FIXME: Are originDestination and odTripUuid mutually exclusive, but one of them is mandatory?
*/
odTripUuid?: string;
}

export interface AccessibilityMapQueryOptions extends TrRoutingBaseQueryOptions {
/**
* To point from/to which to get the accessibility map
*/
location: GeoJSON.Feature<GeoJSON.Point>;
accessibleNodes?: { ids: string[]; durations: number[] };
}

export type TrRoutingResultPath = { type: 'path'; path: TrRoutingApi.TrRoutingPath };
export type TrRoutingResultAlternatives = {
type: 'withAlternatives';
alternatives: TrRoutingApi.TrRoutingAlternative[];
};

// Api agnostic version of the accessibility map result
// FIXME Not changed since V1, should we revisit?
export type TrRoutingResultAccessibilityMap = { type: 'nodes'; nodes: TrRoutingApi.TrRoutingApiNode[] };

export type TrRoutingResult = TrRoutingResultPath | TrRoutingResultAlternatives;

// For each route to be self contained, the query parameters are added to each result
export type TrRoutingRoute = TrRoutingApi.TrRoutingV2.SingleRouteResult & {
originDestination: [GeoJSON.Feature<GeoJSON.Point>, GeoJSON.Feature<GeoJSON.Point>];
timeOfTrip: number;
timeOfTripType: 'arrival' | 'departure';
};
// API agnostic version of the route result
export type TrRoutingRouteResult = { routes: TrRoutingRoute[]; totalRoutesCalculated: number };

const apiV2ToRouteResult = (apiResult: TrRoutingApi.TrRoutingV2.RouteSuccessResult): TrRoutingRouteResult => {
// For each route to be self contained, concatenate the query with each result
const allRoutes = apiResult.result.routes.map((route) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import { TrRoutingService, ErrorCodes } from '../TrRoutingService';
import { TrRoutingService } from '../TrRoutingService';
import { TransitRouteQueryOptions, TrRoutingConstants } from '../../../api/TrRouting';
import serviceLocator from '../../../utils/ServiceLocator';
import TrError from '../../../utils/TrError';
import { TestUtils } from '../../../test';
import fetchMock from 'jest-fetch-mock';
import * as Status from '../../../utils/Status';
import { pathNoTransferRouteV2 } from '../../../test/services/trRouting/TrRoutingConstantsStubs';
import { ErrorCodes } from '../types';

const trRoutingService = new TrRoutingService;
const trRoutingService = new TrRoutingService();

const origin = TestUtils.makePoint([-73.745618, 45.368994]);
const destination = TestUtils.makePoint([-73.742861, 45.361682]);
Expand Down
111 changes: 111 additions & 0 deletions packages/chaire-lib-common/src/services/trRouting/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2022, Polytechnique Montreal and contributors
*
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
// This file contains types for the trRouting calculations
import * as TrRoutingApi from '../../api/TrRouting';

export enum ErrorCodes {
NoRoutingFound = 'TRROUTING_NO_ROUTING_FOUND',
NoAccessAtOrigin = 'TRROUTING_NO_ROUTING_NO_ACCESS_AT_ORIGIN',
NoAccessAtDestination = 'TRROUTING_NOT_ROUTING_NO_ACCESS_AT_DESTINATION',
NoAccessAtOriginAndDestination = 'TRROUTING_NOT_ROUTING_NO_ACCESS_AT_ORIGIN_AND_DESTINATION',
NoServiceAtOrigin = 'TRROUTING_NO_ROUTING_NO_SERVICE_FROM_ORIGIN',
NoServiceAtDestination = 'TRROUTING_NO_ROUTING_NO_SERVICE_TO_DESTINATION',
ServerNotRunning = 'TRROUTING_SERVER_NOT_RUNNING',
NoAccessAtPlace = 'TRROUTING_NOT_ROUTING_NO_ACCESS_AT_PLACE',
NoServiceAtPlace = 'TRROUTING_NO_ROUTING_NO_SERVICE_AT_PLACE',
OtherError = 'TRROUTING_ERROR_UNKNOWN',
MissingData = 'TRROUTING_MISSING_DATA',
DataError = 'TRROUTING_INVALID_DATA',
QueryError = 'TRROUTING_QUERY_ERROR'
}

interface TrRoutingBaseQueryOptions {
/**
* Minimum waiting time, in seconds
*/
minWaitingTime: number;
/**
* Maximum walking time from origin, in seconds
*/
maxAccessTravelTime: number;
/**
* Maximum walking time to reach destination, in seconds
*/
maxEgressTravelTime: number;
/**
* Maximum transfer time, in seconds
*/
maxTransferTravelTime: number;
/**
* Maximum total travel time, in seconds
*/
maxTravelTime: number;
/**
* The UUID of the scenario to use for the calculation
*/
scenarioId: string;
/**
* Arrival or departure time of the trip, in seconds since midnight
*/
timeOfTrip: number;
/**
* Whether the time is arrival or departure time
*/
timeOfTripType: 'arrival' | 'departure';
/**
* Maximum wait time at first transit stop
*/
maxFirstWaitingTime?: number;
}

export interface RoutingQueryOptions extends TrRoutingBaseQueryOptions {
/**
* Whether to calculate alternatives
*/
alternatives?: boolean;
/**
* An array containing the origin and destination points. Any of the 2 can
* be undefined if there is no origin or destination (for example
* accessibility maps)
*/
originDestination?: [GeoJSON.Feature<GeoJSON.Point>, GeoJSON.Feature<GeoJSON.Point>];
/**
* UUID of od trip
*
* FIXME: Are originDestination and odTripUuid mutually exclusive, but one of them is mandatory?
*/
odTripUuid?: string;
}

export interface AccessibilityMapQueryOptions extends TrRoutingBaseQueryOptions {
/**
* To point from/to which to get the accessibility map
*/
location: GeoJSON.Feature<GeoJSON.Point>;
accessibleNodes?: { ids: string[]; durations: number[] };
}

export type TrRoutingResultPath = { type: 'path'; path: TrRoutingApi.TrRoutingPath };
export type TrRoutingResultAlternatives = {
type: 'withAlternatives';
alternatives: TrRoutingApi.TrRoutingAlternative[];
};

// Api agnostic version of the accessibility map result
// FIXME Not changed since V1, should we revisit?
export type TrRoutingResultAccessibilityMap = { type: 'nodes'; nodes: TrRoutingApi.TrRoutingApiNode[] };

export type TrRoutingResult = TrRoutingResultPath | TrRoutingResultAlternatives;

// For each route to be self contained, the query parameters are added to each result
export type TrRoutingRoute = TrRoutingApi.TrRoutingV2.SingleRouteResult & {
originDestination: [GeoJSON.Feature<GeoJSON.Point>, GeoJSON.Feature<GeoJSON.Point>];
timeOfTrip: number;
timeOfTripType: 'arrival' | 'departure';
};
// API agnostic version of the route result
export type TrRoutingRouteResult = { routes: TrRoutingRoute[]; totalRoutesCalculated: number };
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* License text available at https://opensource.org/licenses/MIT
*/
import { TrRoutingPath, TrRoutingV2 } from '../../../api/TrRouting';
import { TrRoutingRoute } from '../../../services/trRouting/TrRoutingService';
import { TrRoutingRoute } from '../../../services/trRouting/types';
import TestUtils from '../../TestUtils';

// TODO These constant paths seem to have as base an actual reply from trRouting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Feature, FeatureCollection, LineString, Point } from 'geojson';
import { RoutingMode, RoutingOrTransitMode } from 'chaire-lib-common/lib/config/routingModes';
import * as TrRoutingApi from 'chaire-lib-common/lib/api/TrRouting';
import { TrRoutingRoute } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingRoute } from 'chaire-lib-common/lib/services/trRouting/types';
import { Route } from 'chaire-lib-common/lib/services/routing/RoutingService';
import APIResponseBase from './APIResponseBase';
import { TripRoutingQueryAttributes } from 'chaire-lib-common/lib/services/routing/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import _sum from 'lodash/sum';
import { AccessibilityMapAttributes } from 'transition-common/lib/services/accessibilityMap/TransitAccessibilityMapRouting';
import TrError from 'chaire-lib-common/lib/utils/TrError';
import { routingServiceManager as trRoutingServiceManager } from 'chaire-lib-common/lib/services/trRouting/TrRoutingServiceManager';
import { TrRoutingResultAccessibilityMap } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingResultAccessibilityMap } from 'chaire-lib-common/lib/services/trRouting/types';
import { AccessibilityMapQueryOptions } from 'chaire-lib-common/lib/api/TrRouting';
import { secondsSinceMidnightToTimeStr } from 'chaire-lib-common/lib/utils/DateTimeUtils';
import { _toInteger, _isBlank } from 'chaire-lib-common/lib/utils/LodashExtensions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
TransitAccessibilityMapResult,
TransitAccessibilityMapWithPolygonResult
} from 'transition-common/lib/services/accessibilityMap/TransitAccessibilityMapResult';
import { TrRoutingResultAccessibilityMap } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingResultAccessibilityMap } from 'chaire-lib-common/lib/services/trRouting/types';
import { RoutingMode } from 'chaire-lib-common/lib/config/routingModes';
import { SegmentToGeoJSONFromPaths } from 'transition-common/lib/services/transitRouting/TransitRoutingResult';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SegmentToGeoJSONFromPaths } from 'transition-common/lib/services/transi
import { getDefaultCsvAttributes, getDefaultStepsAttributes } from './ResultAttributes';
import { OdTripRouteOutput, OdTripRouteResult } from './types';
import { unparse } from 'papaparse';
import { ErrorCodes, TrRoutingRoute } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { ErrorCodes, TrRoutingRoute } from 'chaire-lib-common/lib/services/trRouting/types';
import { TransitRoutingResultData } from 'chaire-lib-common/lib/services/routing/TransitRoutingResult';
import { routeToUserObject } from 'chaire-lib-common/lib/services/trRouting/TrRoutingResultConversion';
import TrError from 'chaire-lib-common/lib/utils/TrError';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { UnimodalRoutingResult } from 'chaire-lib-common/lib/services/routing/Ro
import Path from 'transition-common/lib/services/path/Path';
import PathCollection from 'transition-common/lib/services/path/PathCollection';
import TrError from 'chaire-lib-common/lib/utils/TrError';
import { ErrorCodes } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { ErrorCodes } from 'chaire-lib-common/lib/services/trRouting/types';


const absoluteDir = `${directoryManager.userDataDirectory}/1/exports`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import routeOdTrip from '../TrRoutingOdTrip';
import { simplePathResult, alternativesResult, walkingRouteResult, cyclingRouteResult } from './TrRoutingResultStub';
import { BaseOdTrip } from 'transition-common/lib/services/odTrip/BaseOdTrip';
import TrError from 'chaire-lib-common/lib/utils/TrError';
import { ErrorCodes } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { ErrorCodes } from 'chaire-lib-common/lib/services/trRouting/types';
import { routeToUserObject } from 'chaire-lib-common/src/services/trRouting/TrRoutingResultConversion';
import { Routing } from 'chaire-lib-backend/lib/services/routing/Routing';
import { TransitRoutingQueryAttributes } from 'chaire-lib-common/lib/services/routing/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/types';
import { RouteResults } from 'chaire-lib-common/lib/services/routing/RoutingService';
import { pathNoTransferRouteResult, pathOneTransferRouteResult } from 'chaire-lib-common/lib/test/services/trRouting/TrRoutingConstantsStubs';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import _sum from 'lodash/sum';
import _uniq from 'lodash/uniq';

import { TrRoutingResultAccessibilityMap } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingResultAccessibilityMap } from 'chaire-lib-common/lib/services/trRouting/types';
import {
categories,
detailedCategories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* License text available at https://opensource.org/licenses/MIT
*/
import { pathNoTransferRouteResult, pathOneTransferRouteResult } from 'chaire-lib-common/lib/test/services/trRouting/TrRoutingConstantsStubs';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingRouteResult } from 'chaire-lib-common/lib/services/trRouting/types';

// TODO tahini: this is considered a test file, so we need a test, should be a mock
test('Dummy', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TrRoutingV2 } from 'chaire-lib-common/lib/api/TrRouting';
import { Route } from 'chaire-lib-common/lib/services/routing/RoutingService';
import { RoutingOrTransitMode } from 'chaire-lib-common/lib/config/routingModes';
import { TransitRoutingAttributes } from 'transition-common/lib/services/transitRouting/TransitRouting';
import { TrRoutingRoute } from 'chaire-lib-common/lib/services/trRouting/TrRoutingService';
import { TrRoutingRoute } from 'chaire-lib-common/lib/services/trRouting/types';
import { pathIsRoute } from 'chaire-lib-common/lib/services/routing/RoutingResult';

export interface TransitRoutingResultsProps extends WithTranslation {
Expand Down

0 comments on commit 61dcfe7

Please sign in to comment.