Skip to content

Commit

Permalink
feature: update packages and node version (major)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu <matthieu.perrin@montreal.ca>
  • Loading branch information
mtlmatt committed May 17, 2024
1 parent 2914769 commit 3cd328d
Show file tree
Hide file tree
Showing 10 changed files with 16,612 additions and 7,881 deletions.
5,195 changes: 2,599 additions & 2,596 deletions le-taxi-api-node.js/package-lock.json

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions le-taxi-api-node.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@turf/boolean-contains": "6.5.0",
"@turf/helpers": "6.5.0",
"@turf/length": "6.5.0",
"@villedemontreal/scripting": "1.5.0",
"@villedemontreal/scripting": "2.1.8",
"body-parser": "1.20.2",
"class-transformer": "0.5.1",
"class-validator": "0.13.2",
Expand All @@ -36,7 +36,7 @@
"es6-shim": "0.35.8",
"express": "4.19.2",
"express-async-errors": "3.1.1",
"express-prom-bundle": "6.6.0",
"express-prom-bundle": "7.0.0",
"fs": "0.0.2",
"geojson": "0.5.0",
"http-status-codes": "2.3.0",
Expand All @@ -46,50 +46,50 @@
"JSONStream": "1.3.5",
"jszip": "3.10.1",
"lodash": "4.17.21",
"lru-cache": "6.0.0",
"lru-cache": "10.2.2",
"mockit-express": "0.5.0",
"moment": "2.30.1",
"mongodb": "4.17.2",
"njwt": "1.2.0",
"mongodb": "6.6.2",
"njwt": "2.0.0",
"nock": "13.5.4",
"passhelp": "1.1.1",
"pg": "8.11.5",
"pg-query-stream": "4.5.5",
"pino": "7.11.0",
"prom-client": "14.2.0",
"pino": "9.1.0",
"prom-client": "15.1.2",
"promise": "8.3.0",
"reflect-metadata": "0.2.2",
"request": "2.88.2",
"serialize-error": "8.1.0",
"shortid": "2.2.16",
"superagent": "6.1.0",
"uuid": "8.3.2",
"superagent": "9.0.2",
"uuid": "9.0.1",
"uuid4": "2.0.3",
"xss": "1.0.15",
"zlib": "1.0.5"
},
"devDependencies": {
"@types/bunyan": "1.8.11",
"@types/chai": "4.3.16",
"@types/config": "0.0.41",
"@types/config": "3.3.4",
"@types/cors": "2.8.17",
"@types/express": "4.17.21",
"@types/geojson": "7946.0.14",
"@types/lodash": "4.17.3",
"@types/lru-cache": "5.1.1",
"@types/mocha": "9.1.1",
"@types/node": "16.18.97",
"@types/lodash": "4.17.4",
"@types/lru-cache": "7.10.9",
"@types/mocha": "10.0.6",
"@types/node": "20.12.12",
"@types/pg": "8.11.6",
"@types/request": "2.48.12",
"@types/superagent": "4.1.24",
"@types/superagent": "8.1.7",
"@villedemontreal/lint-config": "1.7.7",
"chai": "4.4.1",
"chai-http": "4.4.0",
"mocha": "9.2.2",
"rimraf": "3.0.2",
"sonarqube-scanner": "2.9.1",
"mocha": "10.4.0",
"rimraf": "5.0.7",
"sonarqube-scanner": "3.5.0",
"source-map-support": "0.5.21",
"typescript": "4.9.5"
"typescript": "5.4.5"
},
"peerDependencies": {
"js-yaml": "^3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Licensed under the AGPL-3.0 license.
// See LICENSE file in the project root for full license information.
import { configs } from '../../config/configs';
import { InquiryTypes } from '../inquiry/inquiry.dto';
import { ICoordinates } from '../shared/coordinates/coordinates';
import { getMongoDb } from '../shared/taxiMongo/taxiMongo';
import { TaxiStatus } from './../../libs/taxiStatus';
import { latestTaxiPositionMapper } from './latestTaxiPosition.mapper';
import { LatestTaxiPositionModel, LatestTaxiPositionModelExtended } from './latestTaxiPosition.model';
import { ObjectId } from "mongodb";
import { configs } from "../../config/configs";
import { InquiryTypes } from "../inquiry/inquiry.dto";
import { ICoordinates } from "../shared/coordinates/coordinates";
import { getMongoDb } from "../shared/taxiMongo/taxiMongo";
import { TaxiStatus } from "./../../libs/taxiStatus";
import { latestTaxiPositionMapper } from "./latestTaxiPosition.mapper";
import {
LatestTaxiPositionModel,
LatestTaxiPositionModelExtended,
} from "./latestTaxiPosition.model";

class LatestTaxiPositionRepository {
public async findClosestTaxis(
Expand All @@ -16,46 +20,68 @@ class LatestTaxiPositionRepository {
): Promise<LatestTaxiPositionModelExtended[]> {
const db = getMongoDb();

const filters = inquiryTypes.map(inquiryType => ({
const filters = inquiryTypes.map((inquiryType) => ({
isPromoted: true,
status: TaxiStatus.Free,
...operatorsCondition(operators),
...transportationTypeCondition(inquiryType),
location: {
$near: {
$geometry: { type: 'Point', coordinates: [coordinate.lon, coordinate.lat] },
$maxDistance: getArbitraryMaxDistance(inquiryType)
}
}
$geometry: {
type: "Point",
coordinates: [coordinate.lon, coordinate.lat],
},
$maxDistance: getArbitraryMaxDistance(inquiryType),
},
},
}));

const results = await Promise.all(filters.map(filter => db.collection('latestTaxiPositions').findOne(filter)));
const results = await Promise.all(
filters.map((filter) =>
db.collection("latestTaxiPositions").findOne(filter)
)
);

return results
.map((result, i) => latestTaxiPositionMapper.mongoToLatestTaxiPositionModelExtended(result, inquiryTypes[i]))
.filter(result => !!result);
.map((result, i) =>
latestTaxiPositionMapper.mongoToLatestTaxiPositionModelExtended(
result,
inquiryTypes[i]
)
)
.filter((result) => !!result);
}

public async getLatestTaxiPositionByTaxiId(id: string): Promise<LatestTaxiPositionModel> {
public async getLatestTaxiPositionByTaxiId(
id: string
): Promise<LatestTaxiPositionModel> {
const db = getMongoDb();
const result = await db.collection('latestTaxiPositions').findOne({ _id: id });
const result = await db
.collection("latestTaxiPositions")
.findOne({ _id: (id as unknown) as ObjectId });
return latestTaxiPositionMapper.mongoToLatestTaxiPositionModel(result);
}

public async getLatestTaxiPositions(): Promise<LatestTaxiPositionModel[]> {
const mongoDb = getMongoDb();
const results = await mongoDb
.collection('latestTaxiPositions')
.collection("latestTaxiPositions")
.find({})
.toArray();
return results.map(result => latestTaxiPositionMapper.mongoToLatestTaxiPositionModel(result));
return results.map((result) =>
latestTaxiPositionMapper.mongoToLatestTaxiPositionModel(result)
);
}

public async saveLatestTaxiPositions(latestTaxiPositions: LatestTaxiPositionModel[]): Promise<void> {
public async saveLatestTaxiPositions(
latestTaxiPositions: LatestTaxiPositionModel[]
): Promise<void> {
if (latestTaxiPositions.length === 0) return;

const db = getMongoDb();
const bulk = await db.collection('latestTaxiPositions').initializeUnorderedBulkOp();
const bulk = await db
.collection("latestTaxiPositions")
.initializeUnorderedBulkOp();
for (const pos of latestTaxiPositions) {
const { lat, lon, taxiId, status, ...doc } = pos;
if (status === TaxiStatus.Off) {
Expand All @@ -66,12 +92,12 @@ class LatestTaxiPositionRepository {
.upsert()
.replaceOne({
location: {
type: 'Point',
coordinates: [lon, lat]
type: "Point",
coordinates: [lon, lat],
},
_id: taxiId,
status,
...doc
...doc,
});
}
}
Expand All @@ -80,21 +106,23 @@ class LatestTaxiPositionRepository {
}

function operatorsCondition(operators: number[]): any {
return operators && operators.length > 0 ? { 'taxi.operatorId': { $in: operators } } : {};
return operators && operators.length > 0
? { "taxi.operatorId": { $in: operators } }
: {};
}

function transportationTypeCondition(inquiryTypes: InquiryTypes): any {
switch (inquiryTypes) {
case InquiryTypes.Minivan:
return {
'taxi.isMpv': true,
'taxi.isSpecialNeedVehicle': false
"taxi.isMpv": true,
"taxi.isSpecialNeedVehicle": false,
};
case InquiryTypes.SpecialNeed:
return { 'taxi.isSpecialNeedVehicle': true };
return { "taxi.isSpecialNeedVehicle": true };
default:
case InquiryTypes.Standard:
return { 'taxi.isSpecialNeedVehicle': false };
return { "taxi.isSpecialNeedVehicle": false };
}
}

Expand Down
28 changes: 18 additions & 10 deletions le-taxi-api-node.js/src/features/shared/caching/modelMapCache.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
// Licensed under the AGPL-3.0 license.
// See LICENSE file in the project root for full license information.
import * as assert from 'assert';
import * as LRUCache from 'lru-cache';
import { ModelMap } from './modelMap';
import { defaultLRUCacheConfig, ICacheConfig } from './taxiCacheConfig';
import * as assert from "assert";
import { LRUCache } from "lru-cache";
import { ModelMap } from "./modelMap";
import { defaultLRUCacheConfig, ICacheConfig } from "./taxiCacheConfig";

export class ModelMapCache<TModel> {
public static createFromSingle<TModel>(
singleAccessor: (key: string) => Promise<TModel>,
cacheConfig: ICacheConfig = defaultLRUCacheConfig
): ModelMapCache<TModel> {
const accessor = async (keys: string[]) => {
assert(keys && keys.length <= 1, 'A ModelMap fed by a single accessor can only be queried by a single key');
assert(
keys && keys.length <= 1,
"A ModelMap fed by a single accessor can only be queried by a single key"
);

const key = keys[0];
return {
[key]: await singleAccessor(key)
[key]: await singleAccessor(key),
};
};

Expand All @@ -38,16 +41,19 @@ export class ModelMapCache<TModel> {
this.accessor = accessor;
this.cache = new LRUCache<string, TModel>({
max: cacheConfig.maxCapacity,
maxAge: cacheConfig.maxAge
ttl: cacheConfig.maxAge,
});
}

public async getByKeys(keys: string[]): Promise<ModelMap<TModel>> {
const sanitizedKeys = this.sanitizeCacheKeys(keys);

const volatileModels = this.getFromCache(sanitizedKeys);
const keysNotInCache = sanitizedKeys.filter(key => volatileModels && !volatileModels[key]);
const persistedModels = keysNotInCache.length && (await this.accessor(keysNotInCache));
const keysNotInCache = sanitizedKeys.filter(
(key) => volatileModels && !volatileModels[key]
);
const persistedModels =
keysNotInCache.length && (await this.accessor(keysNotInCache));
this.setInCache(persistedModels);

return { ...volatileModels, ...persistedModels };
Expand All @@ -71,6 +77,8 @@ export class ModelMapCache<TModel> {
}

private setInCache(modelMap: ModelMap<TModel>) {
Object.entries(modelMap).forEach(([key, value]) => this.cache.set(key, value));
Object.entries(modelMap).forEach(([key, value]) =>
this.cache.set(key, value)
);
}
}
27 changes: 19 additions & 8 deletions le-taxi-api-node.js/src/features/shared/taxiMongo/taxiMongo.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
// Licensed under the AGPL-3.0 license.
// See LICENSE file in the project root for full license information.
import * as assert from 'assert';
import { Db, MongoClient } from 'mongodb';
import { configs } from '../../../config/configs';
import * as assert from "assert";
import { Db, MongoClient } from "mongodb";
import { configs } from "../../../config/configs";

let db: Db;

let client: MongoClient;

export async function connectToMongoDb() {
client = await MongoClient.connect(buildMongoConnectionString(), {
keepAlive: true,
maxPoolSize: configs.dataSources.mongo.poolSize
maxPoolSize: configs.dataSources.mongo.poolSize,
});
db = client.db('vdm_txp');
db = client.db("vdm_txp");
return db;
}

function buildMongoConnectionString(): string {
const { host1, host2, host3, port, username, password, defaultauthdb, options } = configs.dataSources.mongo;
const {
host1,
host2,
host3,
port,
username,
password,
defaultauthdb,
options,
} = configs.dataSources.mongo;
return `mongodb://${username}:${password}@${host1}:${port},${host2}:${port},${host3}:${port}/${defaultauthdb}?${options}`;
}

export function getMongoDb() {
assert.ok(db, `no instance of db mongo. Does connectToMongoDb() have been called ??`);
assert.ok(
db,
`no instance of db mongo. Does connectToMongoDb() have been called ??`
);
return db;
}
Loading

0 comments on commit 3cd328d

Please sign in to comment.