From 55b81eb2342d1ab6e62795fec35de8e649e597d7 Mon Sep 17 00:00:00 2001 From: alexeh Date: Fri, 19 Apr 2024 10:41:36 +0300 Subject: [PATCH] Implement testing endpoint, to be removed --- .../import-data/import-data.controller.ts | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/api/src/modules/import-data/import-data.controller.ts b/api/src/modules/import-data/import-data.controller.ts index f8f7ab3fd..ebf8a714e 100644 --- a/api/src/modules/import-data/import-data.controller.ts +++ b/api/src/modules/import-data/import-data.controller.ts @@ -3,6 +3,7 @@ import { Get, Inject, Post, + Query, UnauthorizedException, UploadedFile, UseGuards, @@ -38,6 +39,7 @@ export class ImportDataController { constructor( public readonly importDataService: ImportDataService, private readonly eudr: EudrImportService, + private readonly importProgressEmitter: ImportProgressEmitter, @Inject(IWebSocketServiceToken) private emitter: IWebSocketService, ) {} @@ -111,23 +113,45 @@ export class ImportDataController { @Public() @Get('/sockets/start') - async test(): Promise { - for (const n of Array(10).keys()) { - this.emitter.emit('DATA_IMPORT_PROGRESS' as any, { - message: `Hello World ${n}`, - }); - } + async startEmittingProgress(@Query() time: any): Promise { + const steps = [ + 'VALIDATING_DATA', + 'IMPORTING_DATA', + 'GEOCODING', + 'CALCULATING_IMPACT', + 'FINISHED', + ]; - return 'Hello World'; - } + const totalTime: number = parseInt(time.time) * 1000 || 30000; + const stepTime: number = totalTime / steps.length; - @Public() - @Get('/sockets/stop') - async stop(): Promise { - this.emitter.emit('DATA_IMPORT_FINISHED' as any, { - message: `Hello World`, - }); + function sleep(ms: number): Promise { + return new Promise((resolve: any) => setTimeout(resolve, ms)); + } - return 'Hello World'; + for (const step of steps) { + const progressIncrement: number = 10; + const numberOfIncrements: number = 100 / progressIncrement; + for (let i: number = 0; i <= 100; i += progressIncrement) { + if (step === 'VALIDATING_DATA') { + this.importProgressEmitter.emitImportProgress({ progress: i }); + } + if (step === 'IMPORTING_DATA') { + this.importProgressEmitter.emitImportProgress({ progress: i }); + } + if (step === 'GEOCODING') { + this.importProgressEmitter.emitGeocodingProgress({ progress: i }); + } + if (step === 'CALCULATING_IMPACT') { + this.importProgressEmitter.emitImpactCalculationProgress({ + progress: i, + }); + } + // if (step === 'FINISHED') { + // this.importProgressEmitter.emitImportFinished(); + // } + await sleep(stepTime / numberOfIncrements); // Wait a fraction of the step's total time before the next update + } + } } }