Replace GRPC call to geography with AMQP RPC

This commit is contained in:
Romain Thouvenin 2024-03-22 22:33:24 +01:00
parent 579415c300
commit 1fd15430a1
6 changed files with 44 additions and 30 deletions

View File

@ -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');

View File

@ -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<RedisClientOptions>({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({

View File

@ -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<RouteResponse> =>
this.query.routeProvider.getRoute({
waypoints: (candidate.getProps().carpoolPath as CarpoolPathItem[]).map(
(carpoolPathItem: CarpoolPathItem) => carpoolPathItem,
(carpoolPathItem: CarpoolPathItem) => ({
lon: carpoolPathItem.lon,
lat: carpoolPathItem.lat,
}),
),
detailsSettings: detailsSettings,
});

View File

@ -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;

View File

@ -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(

View File

@ -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<RouteResponse>;
}
@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<GeorouterService>(
GRPC_GEOROUTER_SERVICE_NAME,
);
}
constructor(
@Inject(GEOGRAPHY_SERVICE) private readonly client: ClientProxy,
) {}
getRoute = async (request: RouteRequest): Promise<RouteResponse> => {
try {
return await lastValueFrom(this.georouterService.getRoute(request));
} catch (error: any) {
throw error;
}
return lastValueFrom(this.client.send('getRoute', request));
};
}