瀏覽代碼

refactor: optimize importExcelTags method for improved data handling and reduced concurrency

Dave 3 周之前
父節點
當前提交
9d3388a186
共有 1 個文件被更改,包括 20 次插入18 次删除
  1. 20 18
      apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

+ 20 - 18
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

@@ -308,13 +308,6 @@ export class VideoMediaService {
       if (value instanceof Uint8Array) return Buffer.from(value);
       if (Array.isArray(value)) return Buffer.from(value);
       if (typeof value === 'string') return Buffer.from(value, 'binary');
-      if (typeof value === 'object') {
-        try {
-          return Buffer.from(value as ArrayLike<number>);
-        } catch {
-          return undefined;
-        }
-      }
       return undefined;
     };
 
@@ -361,12 +354,21 @@ export class VideoMediaService {
       throw new BadRequestException('No sheet found in Excel file');
     }
 
-    const rows = XLSX.utils.sheet_to_json(sheet, {
-      header: 1,
-      defval: '',
-    }) as unknown as any[][];
+    const rangeRef = sheet['!ref'] ?? 'A1:A1';
+    const range = XLSX.utils.decode_range(rangeRef);
+    const rowsAH: Array<{ idRaw: unknown; tagsRaw: unknown }> = [];
+
+    for (let r = range.s.r; r <= range.e.r; r += 1) {
+      const idCell = sheet[XLSX.utils.encode_cell({ r, c: 0 })];
+      const tagsCell = sheet[XLSX.utils.encode_cell({ r, c: 7 })];
+      rowsAH.push({
+        idRaw: idCell?.v ?? '',
+        tagsRaw: tagsCell?.v ?? '',
+      });
+    }
+
     this.logger.log(
-      `[importExcelTags] Excel data rows: ${Math.max(0, rows.length - 1)}`,
+      `[importExcelTags] Excel data rows: ${Math.max(0, rowsAH.length - 1)}`,
     );
 
     const errors: Array<{ rowNumber: number; idRaw: unknown; reason: string }> =
@@ -383,11 +385,11 @@ export class VideoMediaService {
     let skippedInvalidId = 0;
     let skippedEmptyTags = 0;
 
-    for (let i = 1; i < rows.length; i += 1) {
-      const row = rows[i] ?? [];
+    for (let i = 1; i < rowsAH.length; i += 1) {
+      const row = rowsAH[i] ?? { idRaw: '', tagsRaw: '' };
       const rowNumber = i + 1;
-      const idRaw = row[0];
-      const tagsCell = row[7];
+      const idRaw = row.idRaw;
+      const tagsCell = row.tagsRaw;
       const tagsRaw =
         typeof tagsCell === 'string'
           ? tagsCell
@@ -436,7 +438,7 @@ export class VideoMediaService {
     let updated = 0;
 
     const totalCandidates = candidates.length;
-    const concurrency = 5;
+    const concurrency = 2;
     let cursor = 0;
     let nextLogStart = 1;
 
@@ -519,7 +521,7 @@ export class VideoMediaService {
     await Promise.all(workers);
 
     return {
-      totalRows: rows.length,
+      totalRows: rowsAH.length,
       candidateRows,
       updated,
       skippedInvalidId,