Skip to content

Commit b1d8a3f

Browse files
authored
Revert "Clean up constants (#255)" (#277)
This reverts commit e266496.
1 parent ab7cda6 commit b1d8a3f

8 files changed

Lines changed: 57 additions & 130 deletions

File tree

src/routers/v1/MultiRouteRouter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import type Request from 'express';
33
import AnalyticsUtils from '../../utils/AnalyticsUtils';
44
import ApplicationRouter from '../../appdev/ApplicationRouter';
5-
import Constants from '../../utils/Constants';
65
import LogUtils from '../../utils/LogUtils';
76
import RouteUtils from '../../utils/RouteUtils';
87

8+
const CURRENT_LOCATION = 'Current Location';
9+
910
/**
1011
* Router object that returns an array of the best available route for each
1112
* destination, specified from a single start destination.
@@ -38,7 +39,7 @@ class MultiRouteRouter extends ApplicationRouter<Array<Object>> {
3839

3940
// get array of routes for each destination
4041
const multiRoutes = destinationNames.map((destinationName, index) => RouteUtils.getRoutes(
41-
Constants.CURRENT_LOCATION,
42+
CURRENT_LOCATION,
4243
destinationName,
4344
end[index],
4445
start,

src/routers/v1/PlacesAutocompleteRouter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// @flow
22
import LRU from 'lru-cache';
33
import ApplicationRouter from '../../appdev/ApplicationRouter';
4-
import Constants from '../../utils/Constants';
54
import RequestUtils from '../../utils/RequestUtils';
65

7-
const cache = LRU(Constants.AUTOCOMPLETE_CACHE_OPTIONS);
6+
const cacheOptions = {
7+
max: 10000,
8+
maxAge: 1000 * 60 * 60 * 24 * 5,
9+
};
10+
const cache = LRU(cacheOptions);
811

912
class PlacesAutocompleteRouter extends ApplicationRouter<string> {
1013
constructor() {
@@ -30,12 +33,12 @@ class PlacesAutocompleteRouter extends ApplicationRouter<string> {
3033
// not in cache
3134
const options = {
3235
method: 'GET',
33-
url: Constants.GOOGLE_AUTOCOMPLETE_URL,
36+
url: 'https://maps.googleapis.com/maps/api/place/autocomplete/json',
3437
qs: {
3538
input: query,
3639
key: process.env.PLACES_KEY,
37-
location: Constants.GOOGLE_PLACE_LOCATION,
38-
radius: Constants.AUTOCOMPLETE_RADIUS,
40+
location: '42.4440,-76.5019',
41+
radius: 24140,
3942
strictbounds: '',
4043
},
4144
};

src/routers/v1/SearchRouter.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import RequestUtils from '../../utils/RequestUtils';
66
import SearchUtils from '../../utils/SearchUtils';
77
import Constants from '../../utils/Constants';
88

9-
const queryToPredictionsCache = LRU(Constants.QUERY_PREDICTIONS_CACHE_OPTIONS);
9+
const queryToPredictionsCacheOptions = {
10+
max: 10000, // Maximum size of cache
11+
maxAge: 1000 * 60 * 60 * 24 * 5, // Maximum age in milliseconds
12+
};
13+
const queryToPredictionsCache = LRU(queryToPredictionsCacheOptions);
14+
const GOOGLE_PLACE = 'googlePlace';
15+
const GOOGLE_PLACE_LOCATION = '42.4440,-76.5019';
1016

1117
class SearchRouter extends ApplicationRouter<Array<Object>> {
1218
constructor() {
@@ -33,11 +39,11 @@ class SearchRouter extends ApplicationRouter<Array<Object>> {
3339
// not in cache
3440
const options = {
3541
...Constants.GET_OPTIONS,
36-
url: Constants.GOOGLE_AUTOCOMPLETE_URL,
42+
url: 'https://maps.googleapis.com/maps/api/place/autocomplete/json',
3743
qs: {
3844
input: query,
3945
key: process.env.PLACES_KEY,
40-
location: Constants.GOOGLE_PLACE_LOCATION,
46+
location: GOOGLE_PLACE_LOCATION,
4147
radius: 24140,
4248
strictbounds: '',
4349
},
@@ -53,7 +59,7 @@ class SearchRouter extends ApplicationRouter<Array<Object>> {
5359
const googlePredictions = await Promise.all(predictions.map(async (p): Promise<Object> => {
5460
const placeIDCoords = await SearchUtils.getCoordsForPlaceID(p.place_id);
5561
return {
56-
type: Constants.GOOGLE_PLACE,
62+
type: GOOGLE_PLACE,
5763
detail: p.structured_formatting.secondary_text,
5864
name: p.structured_formatting.main_text,
5965
placeID: p.place_id,

src/utils/AllStopUtils.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import Constants from './Constants';
33
import { PYTHON_APP } from './EnvUtils';
44
import RequestUtils from './RequestUtils';
55

6-
// TODO: investigate why importing from Constants.js results in failure of getting routes
6+
const SEC_IN_MS = 1000;
7+
const MIN_IN_MS = SEC_IN_MS * 60;
8+
const HOUR_IN_MS = MIN_IN_MS * 60;
9+
const DEG_EXACT_PRECISION = 6; // 6 degrees of precision is about a 111 mm, is exact point
710
const DEG_EQ_PRECISION = 5; // 5 degrees of precision is about a 1.1 meters, is a stop
11+
const DEG_NEARBY_PRECISION = 4; // 4 degrees of precision is about 11 meters, stop nearby
12+
const DEG_WALK_PRECISION = 3; // 3 degrees of precision is about 111 meters, stop walkable
13+
const DEG_KM_PRECISION = 2; // 3 degrees of precision is about 1 km, stop barely walkable
814

915
/**
1016
* Used for finding stops at or nearby a point
@@ -49,8 +55,8 @@ const updateFunc = async () => {
4955

5056
RequestUtils.updateObjectOnInterval(
5157
updateFunc,
52-
Constants.HOUR_IN_MS,
53-
Constants.MIN_IN_MS,
58+
HOUR_IN_MS,
59+
MIN_IN_MS,
5460
null,
5561
);
5662

@@ -76,10 +82,7 @@ function fetchPrecisionMaps() {
7682
* @returns {Promise<void>}
7783
*/
7884
async function getPrecisionMap(degreesPrecision: ?number = DEG_EQ_PRECISION) {
79-
if (
80-
degreesPrecision < Constants.DEG_MIN_PRECISION
81-
|| degreesPrecision > Constants.DEG_MAX_PRECISION
82-
) {
85+
if (degreesPrecision < 1 || degreesPrecision > 6) {
8386
return null;
8487
}
8588
const stops = await fetchAllStops();
@@ -105,7 +108,7 @@ async function getPrecisionMap(degreesPrecision: ?number = DEG_EQ_PRECISION) {
105108
* @param degreesPrecision
106109
* @returns {Promise<boolean>}
107110
*/
108-
async function isStopsWithinPrecision(point: Object, degreesPrecision: ?number = Constants.DEG_EQ_PRECISION) {
111+
async function isStopsWithinPrecision(point: Object, degreesPrecision: ?number = DEG_EQ_PRECISION) {
109112
const stops = await getPrecisionMap(degreesPrecision);
110113
const lat = parseFloat(point.lat).toFixed(degreesPrecision);
111114
let found = stops[lat]; // found stop(s) at lat
@@ -133,6 +136,11 @@ function isStop(point: Object) {
133136
}
134137

135138
export default {
139+
DEG_EQ_PRECISION,
140+
DEG_EXACT_PRECISION,
141+
DEG_KM_PRECISION,
142+
DEG_NEARBY_PRECISION,
143+
DEG_WALK_PRECISION,
136144
fetchAllStops,
137145
isStop,
138146
isStopsWithinPrecision,

src/utils/AnalyticsUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// @flow
22
import crypto from 'crypto';
33
import LRU from 'lru-cache';
4-
import Constants from './Constants';
54
import LogUtils from './LogUtils';
65

7-
const routesCalculationsCache = LRU(Constants.ROUTES_CALC_CACHE_OPTIONS);
6+
const routesCalculationsCache = LRU({
7+
max: 1000, // max 1000 routes in storage
8+
maxAge: 1000 * 60 * 15, // max age 15 minutes
9+
});
810

911
function getUniqueId(numBytes: ?number = 10) {
1012
return crypto.randomBytes(numBytes).toString('hex');

src/utils/Constants.js

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,11 @@
1-
/* Cache constants */
2-
// A cache containing Google Autocomplete results
3-
const AUTOCOMPLETE_CACHE_OPTIONS = {
4-
max: 10000, // Max 10000 autocomplete results
5-
maxAge: 1000 * 60 * 60 * 24 * 5, // Max age in 5 days
6-
};
7-
8-
// A cache containing saved routes IDs to routes
9-
const ROUTES_CALC_CACHE_OPTIONS = {
10-
max: 1000, // Max 1000 routes
11-
maxAge: 1000 * 60 * 15, // Max age in 15 minutes
12-
};
13-
14-
// A cache mapping a /search's request query string to its Google autocomplete predictions
15-
const QUERY_PREDICTIONS_CACHE_OPTIONS = {
16-
max: 10000, // Max 1000 predictions
17-
maxAge: 1000 * 60 * 60 * 24 * 5, // Max age in 5 days
18-
};
19-
20-
/* Count constants */
21-
// The minimum fuzzy string matching ratio that a word has to match a target word for /search
22-
const MIN_FUZZ_RATIO = 75;
23-
24-
/* Delay Buffer constants */
25-
// A buffer to account for routes in past 20 minutes with delays
26-
const FIRST_DELAY_BUFFER_IN_MINUTES = 20;
27-
// An additional buffer to account for time needed to walk from current location to bus stop
28-
const SECOND_DELAY_BUFFER_IN_MINUTES = 40;
29-
30-
/* Distance & Speed constants */
31-
// > 3.0 suggests getting off bus earlier and walk half a mile instead of waiting longer
32-
const MAX_WALK_DIST_PER_LEG = 2000;
33-
// The distance (in meters) within which to return Google place results for autocomplete.
34-
const AUTOCOMPLETE_RADIUS = 24140;
35-
const WALK_SPEED = 3.0;
36-
37-
/* Degrees Precision constants */
38-
const DEG_MIN_PRECISION = 1;
39-
const DEG_KM_PRECISION = 2; // 2 degrees of precision is about 1 km, a barely walkable stop
40-
const DEG_WALK_PRECISION = 3; // 3 degrees of precision is about 111 meters, a walkable stop
41-
const DEG_NEARBY_PRECISION = 4; // 4 degrees of precision is about 11 meters, a nearby stop
42-
const DEG_EQ_PRECISION = 5; // 5 degrees of precision is about 1.1 meters, a stop
43-
const DEG_EXACT_PRECISION = 6; // 6 degrees of precision is about 111 mm, an exact point
44-
const DEG_MAX_PRECISION = 6;
45-
46-
/* String & URL constants */
47-
const BUS_STOP = 'busStop';
48-
const CURRENT_LOCATION = 'Current Location';
49-
const GOOGLE_AUTOCOMPLETE_URL = 'https://maps.googleapis.com/maps/api/place/autocomplete/json';
50-
const GOOGLE_PLACE = 'googlePlace';
51-
const GOOGLE_PLACE_LOCATION = '42.4440,-76.5019';
52-
const GOOGLE_PLACES_URL = 'https://maps.googleapis.com/maps/api/place/details/json';
53-
const TOKEN_URL = 'https://gateway.api.cloud.wso2.com:443/token';
54-
55-
/* Time constants */
56-
const SEC_IN_MS = 1000;
57-
const MIN_IN_MS = SEC_IN_MS * 60;
58-
const HOUR_IN_MS = MIN_IN_MS * 60;
591
const THREE_SEC_IN_MS = 3000;
60-
const TOKEN_EXPIRATION_WINDOW_IN_MS = 500;
612

62-
/* Request constants */
633
const GET_OPTIONS = {
644
method: 'GET',
655
headers: { 'Cache-Control': 'no-cache' },
666
timeout: THREE_SEC_IN_MS,
677
};
688

699
export default {
70-
AUTOCOMPLETE_CACHE_OPTIONS,
71-
BUS_STOP,
72-
CURRENT_LOCATION,
73-
DEG_EQ_PRECISION,
74-
DEG_EXACT_PRECISION,
75-
DEG_KM_PRECISION,
76-
DEG_MAX_PRECISION,
77-
DEG_MIN_PRECISION,
78-
DEG_NEARBY_PRECISION,
79-
DEG_WALK_PRECISION,
80-
FIRST_DELAY_BUFFER_IN_MINUTES,
8110
GET_OPTIONS,
82-
GOOGLE_AUTOCOMPLETE_URL,
83-
GOOGLE_PLACE,
84-
GOOGLE_PLACE_LOCATION,
85-
GOOGLE_PLACES_URL,
86-
HOUR_IN_MS,
87-
MIN_FUZZ_RATIO,
88-
MAX_WALK_DIST_PER_LEG,
89-
QUERY_PREDICTIONS_CACHE_OPTIONS,
90-
AUTOCOMPLETE_RADIUS,
91-
ROUTES_CALC_CACHE_OPTIONS,
92-
SEC_IN_MS,
93-
SECOND_DELAY_BUFFER_IN_MINUTES,
94-
THREE_SEC_IN_MS,
95-
TOKEN_EXPIRATION_WINDOW_IN_MS,
96-
TOKEN_URL,
97-
WALK_SPEED,
9811
};

src/utils/GraphhopperUtils.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import {
22
GHOPPER_BUS,
33
GHOPPER_WALKING,
44
} from './EnvUtils';
5-
import Constants from './Constants';
65
import LogUtils from './LogUtils';
76
import RequestUtils from './RequestUtils';
87

8+
// buffer to account for routes in past 20 minutes with delays
9+
const FIRST_DELAY_BUFFER_IN_MINUTES = 20;
10+
11+
// additional buffer to account for time needed to walk from current location to bus stop
12+
const SECOND_DELAY_BUFFER_IN_MINUTES = 40;
13+
914
/**
1015
* https://graphhopper.com/api/1/docs/routing/#output
1116
* @param end
@@ -23,9 +28,9 @@ const getGraphhopperBusParams = (
2328
'ch.disable': true,
2429
'pt.arrive_by': arriveBy,
2530
'pt.earliest_departure_time': getDepartureTimeDate(departureTimeQuery, arriveBy, delayBufferMinutes),
26-
'pt.max_walk_distance_per_leg': Constants.MAX_WALK_DIST_PER_LEG,
31+
'pt.max_walk_distance_per_leg': 2000,
2732
'pt.profile': true,
28-
'pt.walk_speed': Constants.WALK_SPEED,
33+
'pt.walk_speed': 3.0, // > 3.0 suggests getting off bus earlier and walk half a mile instead of waiting longer
2934
elevation: false,
3035
point: [start, end],
3136
points_encoded: false,
@@ -215,7 +220,7 @@ async function fetchRoutes(end: string, start: string, departureTimeDateNow: str
215220
start,
216221
departureTimeDateNow,
217222
isArriveByQuery,
218-
Constants.FIRST_DELAY_BUFFER_IN_MINUTES,
223+
FIRST_DELAY_BUFFER_IN_MINUTES,
219224
sharedOptions,
220225
);
221226

@@ -225,7 +230,7 @@ async function fetchRoutes(end: string, start: string, departureTimeDateNow: str
225230
start,
226231
departureTimeDateNow,
227232
isArriveByQuery,
228-
Constants.SECOND_DELAY_BUFFER_IN_MINUTES,
233+
SECOND_DELAY_BUFFER_IN_MINUTES,
229234
sharedOptions,
230235
);
231236

@@ -270,13 +275,7 @@ async function fetchRoutes(end: string, start: string, departureTimeDateNow: str
270275
} else {
271276
LogUtils.log(
272277
busRouteBufferedFirstRequest && busRouteBufferedFirstRequest.body,
273-
getGraphhopperBusParams(
274-
end,
275-
start,
276-
departureTimeDateNow,
277-
isArriveByQuery,
278-
Constants.FIRST_DELAY_BUFFER_IN_MINUTES,
279-
),
278+
getGraphhopperBusParams(end, start, departureTimeDateNow, isArriveByQuery, FIRST_DELAY_BUFFER_IN_MINUTES),
280279
`Routing failed: ${GHOPPER_BUS || 'undefined graphhopper bus env'}`,
281280
);
282281
}
@@ -287,13 +286,7 @@ async function fetchRoutes(end: string, start: string, departureTimeDateNow: str
287286
} else {
288287
LogUtils.log(
289288
busRouteBufferedSecondRequest && busRouteBufferedSecondRequest.body,
290-
getGraphhopperBusParams(
291-
end,
292-
start,
293-
departureTimeDateNow,
294-
isArriveByQuery,
295-
Constants.SECOND_DELAY_BUFFER_IN_MINUTES,
296-
),
289+
getGraphhopperBusParams(end, start, departureTimeDateNow, isArriveByQuery, SECOND_DELAY_BUFFER_IN_MINUTES),
297290
`Routing failed: ${GHOPPER_BUS || 'undefined graphhopper bus env'}`,
298291
);
299292
}

src/utils/TokenUtils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @flow
22
import request from 'request';
33

4-
import Constants from './Constants';
54
import { TOKEN } from './EnvUtils';
65
import LogUtils from './LogUtils';
76

7+
const TOKEN_EXPIRATION_WINDOW_IN_MS = 500;
88
let credentials = { basic_token: TOKEN, access_token: '', expiration_date: '' };
99

1010
function isAccessTokenExpired(): boolean {
@@ -14,7 +14,7 @@ function isAccessTokenExpired(): boolean {
1414

1515
const currentTime = new Date().getTime();
1616
const tokenExpirationTime = (new Date(credentials.expiration_date)).getTime();
17-
return tokenExpirationTime - currentTime < Constants.TOKEN_EXPIRATION_WINDOW_IN_MS;
17+
return tokenExpirationTime - currentTime < TOKEN_EXPIRATION_WINDOW_IN_MS;
1818
}
1919

2020
function fetchAccessToken(): void {
@@ -25,10 +25,11 @@ function fetchAccessToken(): void {
2525
const basicAuthHeader = `Basic ${credentials.basic_token}`;
2626
const options = {
2727
method: 'POST',
28-
url: Constants.TOKEN_URL,
28+
url: 'https://gateway.api.cloud.wso2.com:443/token',
2929
qs: { grant_type: 'client_credentials' },
3030
headers: {
3131
Authorization: basicAuthHeader,
32+
'Postman-Token': '42201611-965d-4832-a4c5-060ad3ff3b83',
3233
'Cache-Control': 'no-cache',
3334
},
3435
};

0 commit comments

Comments
 (0)