From 1fd15430a1ab068cfa2e87458af4a4c9085b34bd Mon Sep 17 00:00:00 2001 From: Romain Thouvenin Date: Fri, 22 Mar 2024 22:33:24 +0100 Subject: [PATCH] Replace GRPC call to geography with AMQP RPC --- src/modules/ad/ad.di-tokens.ts | 1 + src/modules/ad/ad.module.ts | 15 +++++++++++ .../match/completer/route.completer.ts | 11 +++++--- .../application/queries/match/match.query.ts | 16 +++++++----- .../ad/core/domain/path-creator.service.ts | 6 ++--- src/modules/ad/infrastructure/georouter.ts | 25 ++++++------------- 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/modules/ad/ad.di-tokens.ts b/src/modules/ad/ad.di-tokens.ts index bc6dfb0..d520776 100644 --- a/src/modules/ad/ad.di-tokens.ts +++ b/src/modules/ad/ad.di-tokens.ts @@ -19,3 +19,4 @@ export const AD_CONFIGURATION_REPOSITORY = Symbol( 'AD_CONFIGURATION_REPOSITORY', ); export const GEOGRAPHY_PACKAGE = Symbol('GEOGRAPHY_PACKAGE'); +export const GEOGRAPHY_SERVICE = Symbol('GEOGRAPHY_SERVICE'); diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 03c760b..b6d0398 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -12,6 +12,7 @@ import { MATCHING_REPOSITORY, AD_CONFIGURATION_REPOSITORY, GEOGRAPHY_PACKAGE, + GEOGRAPHY_SERVICE, } from './ad.di-tokens'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; import { AdRepository } from './infrastructure/ad.repository'; @@ -64,6 +65,20 @@ const imports = [ }), }, ]), + ClientsModule.register([ + { + name: GEOGRAPHY_SERVICE, + transport: Transport.RMQ, + options: { + //TODO read from config + urls: [`${process.env.MESSAGE_BROKER_URI}`], + queue: 'geography', + queueOptions: { + durable: true, + }, + }, + }, + ]), CacheModule.registerAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ diff --git a/src/modules/ad/core/application/queries/match/completer/route.completer.ts b/src/modules/ad/core/application/queries/match/completer/route.completer.ts index aca1a73..cbea178 100644 --- a/src/modules/ad/core/application/queries/match/completer/route.completer.ts +++ b/src/modules/ad/core/application/queries/match/completer/route.completer.ts @@ -1,9 +1,9 @@ import { CandidateEntity } from '@modules/ad/core/domain/candidate.entity'; -import { Completer } from './completer.abstract'; -import { MatchQuery } from '../match.query'; -import { Step } from '../../../types/step.type'; import { CarpoolPathItem } from '@modules/ad/core/domain/value-objects/carpool-path-item.value-object'; import { RouteResponse } from '../../../ports/georouter.port'; +import { Step } from '../../../types/step.type'; +import { MatchQuery } from '../match.query'; +import { Completer } from './completer.abstract'; export class RouteCompleter extends Completer { protected readonly type: RouteCompleterType; @@ -48,7 +48,10 @@ export class RouteCompleter extends Completer { ): Promise => this.query.routeProvider.getRoute({ waypoints: (candidate.getProps().carpoolPath as CarpoolPathItem[]).map( - (carpoolPathItem: CarpoolPathItem) => carpoolPathItem, + (carpoolPathItem: CarpoolPathItem) => ({ + lon: carpoolPathItem.lon, + lat: carpoolPathItem.lat, + }), ), detailsSettings: detailsSettings, }); diff --git a/src/modules/ad/core/application/queries/match/match.query.ts b/src/modules/ad/core/application/queries/match/match.query.ts index f671550..1d61ecf 100644 --- a/src/modules/ad/core/application/queries/match/match.query.ts +++ b/src/modules/ad/core/application/queries/match/match.query.ts @@ -1,9 +1,5 @@ import { QueryBase } from '@mobicoop/ddd-library'; -import { AlgorithmType } from '../../types/algorithm.types'; -import { Waypoint } from '../../types/waypoint.type'; import { Frequency, Role } from '@modules/ad/core/domain/ad.types'; -import { MatchRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/match.request.dto'; -import { DateTimeTransformerPort } from '../../ports/datetime-transformer.port'; import { Path, PathCreator, @@ -11,8 +7,12 @@ import { TypedRoute, } from '@modules/ad/core/domain/path-creator.service'; import { Point } from '@modules/ad/core/domain/value-objects/point.value-object'; -import { Route } from '../../types/route.type'; +import { MatchRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/match.request.dto'; +import { DateTimeTransformerPort } from '../../ports/datetime-transformer.port'; import { GeorouterPort } from '../../ports/georouter.port'; +import { AlgorithmType } from '../../types/algorithm.types'; +import { Route } from '../../types/route.type'; +import { Waypoint } from '../../types/waypoint.type'; export class MatchQuery extends QueryBase { id?: string; @@ -209,7 +209,10 @@ export class MatchQuery extends QueryBase { pathCreator.getBasePaths().map(async (path: Path) => ({ type: path.type, route: await this.routeProvider.getRoute({ - waypoints: path.waypoints, + waypoints: path.waypoints.map((p) => ({ + lon: p.lon, + lat: p.lat, + })), }), })), ) @@ -225,6 +228,7 @@ export class MatchQuery extends QueryBase { } }); } catch (e: any) { + console.log(e.stack || e); throw new Error('Unable to find a route for given waypoints'); } return this; diff --git a/src/modules/ad/core/domain/path-creator.service.ts b/src/modules/ad/core/domain/path-creator.service.ts index ed34c0c..9a1a10b 100644 --- a/src/modules/ad/core/domain/path-creator.service.ts +++ b/src/modules/ad/core/domain/path-creator.service.ts @@ -1,7 +1,7 @@ -import { Role } from './ad.types'; -import { Point } from './value-objects/point.value-object'; -import { PathCreatorException } from './match.errors'; import { Route } from '../application/types/route.type'; +import { Role } from './ad.types'; +import { PathCreatorException } from './match.errors'; +import { Point } from './value-objects/point.value-object'; export class PathCreator { constructor( diff --git a/src/modules/ad/infrastructure/georouter.ts b/src/modules/ad/infrastructure/georouter.ts index 1280791..8692fde 100644 --- a/src/modules/ad/infrastructure/georouter.ts +++ b/src/modules/ad/infrastructure/georouter.ts @@ -1,35 +1,26 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { ClientProxy } from '@nestjs/microservices'; import { Observable, lastValueFrom } from 'rxjs'; -import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; -import { ClientGrpc } from '@nestjs/microservices'; -import { GRPC_GEOROUTER_SERVICE_NAME } from '@src/app.constants'; +import { GEOGRAPHY_SERVICE } from '../ad.di-tokens'; import { GeorouterPort, RouteRequest, RouteResponse, } from '../core/application/ports/georouter.port'; -import { GEOGRAPHY_PACKAGE } from '../ad.di-tokens'; interface GeorouterService { getRoute(request: RouteRequest): Observable; } @Injectable() -export class Georouter implements GeorouterPort, OnModuleInit { +export class Georouter implements GeorouterPort { private georouterService: GeorouterService; - constructor(@Inject(GEOGRAPHY_PACKAGE) private readonly client: ClientGrpc) {} - - onModuleInit() { - this.georouterService = this.client.getService( - GRPC_GEOROUTER_SERVICE_NAME, - ); - } + constructor( + @Inject(GEOGRAPHY_SERVICE) private readonly client: ClientProxy, + ) {} getRoute = async (request: RouteRequest): Promise => { - try { - return await lastValueFrom(this.georouterService.getRoute(request)); - } catch (error: any) { - throw error; - } + return lastValueFrom(this.client.send('getRoute', request)); }; }