From 2e725cdcce72787edb0c04593c8197a44eb0f2dd Mon Sep 17 00:00:00 2001 From: liulw2022 <107342816+liulw2022@users.noreply.github.com> Date: Mon, 18 Dec 2023 02:33:33 +0800 Subject: [PATCH] Add endpoint of upload status (#181) --- backend/src/upload/file.processor.ts | 10 ++++++++++ backend/src/upload/upload.controller.ts | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/backend/src/upload/file.processor.ts b/backend/src/upload/file.processor.ts index 0f3abd2b..b73b889b 100644 --- a/backend/src/upload/file.processor.ts +++ b/backend/src/upload/file.processor.ts @@ -39,7 +39,11 @@ export class FileProcessor { fs.mkdirSync(tempUnzipPath, { recursive: true }); } + // Start the unzip process with a progress of 0% + await job.progress(0); + // Stream the zip file and extract to the temporary directory + fs.createReadStream(filePath) .pipe(unzip.Extract({ path: tempUnzipPath })) .on('error', (error) => { @@ -47,6 +51,7 @@ export class FileProcessor { }) .on('close', async () => { await this.handleUnzippedContent(tempUnzipPath, filePath); + await job.progress(60); }); } catch (error) { console.error(`Error processing file: ${job.data.filePath}`, error); @@ -121,6 +126,8 @@ export class FileProcessor { // TODO(Seb-sti1): (when rest working) add valhalla here + await job.progress(65); + // Upload all data and images to the database await Promise.all([ ...data.map(async (data) => { @@ -145,6 +152,7 @@ export class FileProcessor { ); }), ]); + await job.progress(99); } // Delete the unzipped file @@ -161,6 +169,8 @@ export class FileProcessor { console.log( `File processed successfully and imported into Database: ${filePath}`, ); + await job.progress(100); + console.log(`Job ${job.id} completed`); } catch (error) { console.error(`Error processing file: ${job.data.filePath}`, error); } diff --git a/backend/src/upload/upload.controller.ts b/backend/src/upload/upload.controller.ts index 9a8a8509..25fde2ac 100644 --- a/backend/src/upload/upload.controller.ts +++ b/backend/src/upload/upload.controller.ts @@ -7,6 +7,7 @@ import { Post, UploadedFile, UseInterceptors, + Get, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { InjectQueue } from '@nestjs/bull'; @@ -79,4 +80,25 @@ export class UploadController { }; } } + @Get('status') + async getUploadStatus() { + const jobs = await this.fileQueue.getJobs([ + 'waiting', + 'active', + 'completed', + 'failed', + ]); + const jobStatus = await Promise.all( + jobs.map(async (job) => ({ + id: job.id, + name: job.name, + timestamp: job.timestamp, + jobData: job.data, + status: await job.getState(), + progress: await job.progress(), + })), + ); + console.log('Job Status:', jobStatus); + return jobStatus; + } }