Преглед изворни кода

refactor(excel-import): remove debug logging from database excel reader

vtugulan пре 6 месеци
родитељ
комит
20d27a6915
1 измењених фајлова са 1 додато и 215 уклоњено
  1. 1 215
      app/lib/excel-import/database-excel-reader.ts

+ 1 - 215
app/lib/excel-import/database-excel-reader.ts

@@ -3,35 +3,12 @@ import * as XLSX from 'xlsx';
 import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
 import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
 import { prisma } from '@/lib/prisma';
 import { prisma } from '@/lib/prisma';
 
 
-// Simple logger utility for debugging
-const logger = {
-  debug: (message: string, ...args: any[]) => {
-    console.debug(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
-  },
-  info: (message: string, ...args: any[]) => {
-    console.info(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
-  },
-  warn: (message: string, ...args: any[]) => {
-    console.warn(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
-  },
-  error: (message: string, ...args: any[]) => {
-    console.error(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
-  }
-};
-
 export class DatabaseExcelReaderService {
 export class DatabaseExcelReaderService {
   async readExcelFromDatabase(
   async readExcelFromDatabase(
     fileId: string,
     fileId: string,
     layoutConfig: any,
     layoutConfig: any,
     onProgress: (progress: ImportProgress) => void
     onProgress: (progress: ImportProgress) => void
   ): Promise<ReadSectionData[]> {
   ): Promise<ReadSectionData[]> {
-    logger.info('Starting Excel file import from database', {
-      fileId,
-      layoutConfigSections: layoutConfig.sections?.length || 0
-    });
-
-    const startTime = Date.now();
-
     try {
     try {
       // Fetch file from database
       // Fetch file from database
       const fileRecord = await prisma.file.findUnique({
       const fileRecord = await prisma.file.findUnique({
@@ -42,33 +19,17 @@ export class DatabaseExcelReaderService {
         throw new Error(`File with ID ${fileId} not found in database`);
         throw new Error(`File with ID ${fileId} not found in database`);
       }
       }
 
 
-      logger.info('File retrieved from database', {
-        filename: fileRecord.filename,
-        size: fileRecord.size,
-        mimetype: fileRecord.mimetype
-      });
-
       // Convert Buffer to ArrayBuffer for xlsx library
       // Convert Buffer to ArrayBuffer for xlsx library
       const arrayBuffer = fileRecord.data.buffer.slice(
       const arrayBuffer = fileRecord.data.buffer.slice(
         fileRecord.data.byteOffset,
         fileRecord.data.byteOffset,
         fileRecord.data.byteOffset + fileRecord.data.byteLength
         fileRecord.data.byteOffset + fileRecord.data.byteLength
       );
       );
 
 
-      logger.debug('Loading Excel workbook from buffer...');
       const workbook = XLSX.read(arrayBuffer, { type: 'array' });
       const workbook = XLSX.read(arrayBuffer, { type: 'array' });
 
 
-      logger.info('Excel workbook loaded successfully from database', {
-        worksheets: workbook.SheetNames.map(name => ({
-          name,
-          rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length
-        }))
-      });
-
       const results: ReadSectionData[] = [];
       const results: ReadSectionData[] = [];
       const totalSections = layoutConfig.sections?.length || 0;
       const totalSections = layoutConfig.sections?.length || 0;
 
 
-      logger.info('Processing Excel import from database', { totalSections });
-
       // Initialize progress
       // Initialize progress
       onProgress({
       onProgress({
         importId: 0,
         importId: 0,
@@ -83,18 +44,11 @@ export class DatabaseExcelReaderService {
 
 
       for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
       for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
         const section = layoutConfig.sections[sectionIndex];
         const section = layoutConfig.sections[sectionIndex];
-        logger.debug(`Processing section ${sectionIndex + 1}/${totalSections}`, {
-          sectionName: section.name,
-          sheetName: section.sheetName,
-          startingRow: section.startingRow,
-          endingRow: section.endingRow
-        });
 
 
         const worksheet = workbook.Sheets[section.sheetName];
         const worksheet = workbook.Sheets[section.sheetName];
 
 
         if (!worksheet) {
         if (!worksheet) {
           const error = `Worksheet '${section.sheetName}' not found`;
           const error = `Worksheet '${section.sheetName}' not found`;
-          logger.warn(error, { availableWorksheets: workbook.SheetNames });
 
 
           onProgress({
           onProgress({
             importId: 0,
             importId: 0,
@@ -117,26 +71,10 @@ export class DatabaseExcelReaderService {
           onProgress
           onProgress
         );
         );
         results.push(sectionData);
         results.push(sectionData);
-
-        logger.info(`Section ${section.name} processed successfully`, {
-          rowsProcessed: sectionData.data.length,
-          fields: sectionData.fields.length
-        });
       }
       }
 
 
-      const totalTime = Date.now() - startTime;
-      logger.info('Excel file import from database completed', {
-        totalSections: results.length,
-        totalRows: results.reduce((sum, section) => sum + section.data.length, 0),
-        totalTimeMs: totalTime
-      });
-
       return results;
       return results;
     } catch (error) {
     } catch (error) {
-      logger.error('Error reading Excel file from database', {
-        error: error instanceof Error ? error.message : String(error),
-        stack: error instanceof Error ? error.stack : undefined
-      });
       throw error;
       throw error;
     }
     }
   }
   }
@@ -146,12 +84,6 @@ export class DatabaseExcelReaderService {
     layoutConfig: any,
     layoutConfig: any,
     onProgress: (progress: ImportProgress) => void
     onProgress: (progress: ImportProgress) => void
   ): Promise<ReadSectionData[]> {
   ): Promise<ReadSectionData[]> {
-    logger.info('Starting Excel file import from buffer', {
-      bufferSize: buffer.length,
-      layoutConfigSections: layoutConfig.sections?.length || 0
-    });
-
-    const startTime = Date.now();
 
 
     try {
     try {
       // Convert Buffer to ArrayBuffer for xlsx library
       // Convert Buffer to ArrayBuffer for xlsx library
@@ -160,21 +92,11 @@ export class DatabaseExcelReaderService {
         buffer.byteOffset + buffer.byteLength
         buffer.byteOffset + buffer.byteLength
       );
       );
 
 
-      logger.debug('Loading Excel workbook from buffer...');
       const workbook = XLSX.read(arrayBuffer, { type: 'array' });
       const workbook = XLSX.read(arrayBuffer, { type: 'array' });
 
 
-      logger.info('Excel workbook loaded successfully from buffer', {
-        worksheets: workbook.SheetNames.map(name => ({
-          name,
-          rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length
-        }))
-      });
-
       const results: ReadSectionData[] = [];
       const results: ReadSectionData[] = [];
       const totalSections = layoutConfig.sections?.length || 0;
       const totalSections = layoutConfig.sections?.length || 0;
 
 
-      logger.info('Processing Excel import from buffer', { totalSections });
-
       // Initialize progress
       // Initialize progress
       onProgress({
       onProgress({
         importId: 0,
         importId: 0,
@@ -189,18 +111,11 @@ export class DatabaseExcelReaderService {
 
 
       for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
       for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
         const section = layoutConfig.sections[sectionIndex];
         const section = layoutConfig.sections[sectionIndex];
-        logger.debug(`Processing section ${sectionIndex + 1}/${totalSections}`, {
-          sectionName: section.name,
-          sheetName: section.sheetName,
-          startingRow: section.startingRow,
-          endingRow: section.endingRow
-        });
 
 
         const worksheet = workbook.Sheets[section.sheetName];
         const worksheet = workbook.Sheets[section.sheetName];
 
 
         if (!worksheet) {
         if (!worksheet) {
           const error = `Worksheet '${section.sheetName}' not found`;
           const error = `Worksheet '${section.sheetName}' not found`;
-          logger.warn(error, { availableWorksheets: workbook.SheetNames });
 
 
           onProgress({
           onProgress({
             importId: 0,
             importId: 0,
@@ -223,26 +138,10 @@ export class DatabaseExcelReaderService {
           onProgress
           onProgress
         );
         );
         results.push(sectionData);
         results.push(sectionData);
-
-        logger.info(`Section ${section.name} processed successfully`, {
-          rowsProcessed: sectionData.data.length,
-          fields: sectionData.fields.length
-        });
       }
       }
 
 
-      const totalTime = Date.now() - startTime;
-      logger.info('Excel file import from buffer completed', {
-        totalSections: results.length,
-        totalRows: results.reduce((sum, section) => sum + section.data.length, 0),
-        totalTimeMs: totalTime
-      });
-
       return results;
       return results;
     } catch (error) {
     } catch (error) {
-      logger.error('Error reading Excel file from buffer', {
-        error: error instanceof Error ? error.message : String(error),
-        stack: error instanceof Error ? error.stack : undefined
-      });
       throw error;
       throw error;
     }
     }
   }
   }
@@ -254,24 +153,9 @@ export class DatabaseExcelReaderService {
     totalSections: number,
     totalSections: number,
     onProgress: (progress: ImportProgress) => void
     onProgress: (progress: ImportProgress) => void
   ): Promise<ReadSectionData> {
   ): Promise<ReadSectionData> {
-    const sectionStartTime = Date.now();
-    logger.info(`Starting section processing from worksheet`, {
-      sectionName: section.name,
-      sheetName: section.sheetName,
-      sectionIndex: sectionIndex + 1,
-      totalSections
-    });
-
     const startingRow = section.startingRow || 2; // Default to 2 to skip header
     const startingRow = section.startingRow || 2; // Default to 2 to skip header
     const endingRow = section.endingRow || Infinity;
     const endingRow = section.endingRow || Infinity;
 
 
-    logger.debug('Section configuration', {
-      sectionName: section.name,
-      startingRow,
-      endingRow,
-      fieldsCount: section.fields?.length || 0
-    });
-
     // Convert worksheet to JSON array
     // Convert worksheet to JSON array
     const worksheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
     const worksheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
 
 
@@ -279,20 +163,14 @@ export class DatabaseExcelReaderService {
     const data: Record<string, any>[] = [];
     const data: Record<string, any>[] = [];
     const totalRows = Math.min(endingRow, worksheetData.length) - startingRow + 1;
     const totalRows = Math.min(endingRow, worksheetData.length) - startingRow + 1;
 
 
-    let processedRows = 0;
-    let skippedRows = 0;
-
     for (let rowNum = startingRow; rowNum <= Math.min(endingRow, worksheetData.length); rowNum++) {
     for (let rowNum = startingRow; rowNum <= Math.min(endingRow, worksheetData.length); rowNum++) {
       const row = worksheetData[rowNum - 1]; // Convert to 0-based index
       const row = worksheetData[rowNum - 1]; // Convert to 0-based index
 
 
       if (!row || row.every(cell => cell === null || cell === undefined || cell === '')) {
       if (!row || row.every(cell => cell === null || cell === undefined || cell === '')) {
-        skippedRows++;
-        logger.debug(`Skipping empty row ${rowNum}`);
         continue;
         continue;
       }
       }
 
 
       const rowData: Record<string, any> = {};
       const rowData: Record<string, any> = {};
-      let fieldsProcessed = 0;
 
 
       // Map cell values based on field configuration
       // Map cell values based on field configuration
       for (const field of section.fields || []) {
       for (const field of section.fields || []) {
@@ -300,14 +178,6 @@ export class DatabaseExcelReaderService {
           const cellAddress = this.parseCellAddress(field.cellPosition, rowNum);
           const cellAddress = this.parseCellAddress(field.cellPosition, rowNum);
           const cellValue = row[cellAddress.col - 1]; // Convert to 0-based index
           const cellValue = row[cellAddress.col - 1]; // Convert to 0-based index
 
 
-          logger.debug(`Processing field`, {
-            fieldName: field.name,
-            cellPosition: field.cellPosition,
-            cellAddress,
-            rawValue: cellValue,
-            rowNum
-          });
-
           if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
           if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
             const value = this.convertCellValue(
             const value = this.convertCellValue(
               field.dataType,
               field.dataType,
@@ -315,20 +185,13 @@ export class DatabaseExcelReaderService {
               cellValue,
               cellValue,
               field.parsedType || FieldTypeEnum.String
               field.parsedType || FieldTypeEnum.String
             );
             );
-            logger.debug(`Value converted`, {
-              fieldName: field.name,
-              originalValue: cellValue,
-              convertedValue: value,
-              fieldType: field.parsedType || FieldTypeEnum.String
-            });
 
 
             // Map to the correct column name for Prisma model
             // Map to the correct column name for Prisma model
             const columnName = field.importTableColumnName;
             const columnName = field.importTableColumnName;
             rowData[columnName] = value;
             rowData[columnName] = value;
-            fieldsProcessed++;
           }
           }
         } catch (error) {
         } catch (error) {
-          logger.error(`Error processing field ${field.name} at row ${rowNum}`, {
+          console.log(`Error processing field ${field.name} at row ${rowNum}`, {
             error: error instanceof Error ? error.message : String(error),
             error: error instanceof Error ? error.message : String(error),
             field,
             field,
             rowNum
             rowNum
@@ -339,18 +202,6 @@ export class DatabaseExcelReaderService {
       // Only add non-empty rows
       // Only add non-empty rows
       if (Object.keys(rowData).length > 0) {
       if (Object.keys(rowData).length > 0) {
         data.push(rowData);
         data.push(rowData);
-        processedRows++;
-
-        if (processedRows <= 5 || processedRows % 100 === 0) {
-          logger.debug(`Row processed`, {
-            rowNum,
-            fieldsProcessed,
-            rowDataKeys: Object.keys(rowData),
-            dataLength: data.length
-          });
-        }
-      } else {
-        logger.debug(`Skipping row with no valid data`, { rowNum });
       }
       }
 
 
       // Update progress every 100 rows
       // Update progress every 100 rows
@@ -368,16 +219,6 @@ export class DatabaseExcelReaderService {
       }
       }
     }
     }
 
 
-    const sectionTime = Date.now() - sectionStartTime;
-    logger.info(`Section processing completed`, {
-      sectionName: section.name,
-      processedRows,
-      skippedRows,
-      totalRows,
-      dataRows: data.length,
-      processingTimeMs: sectionTime
-    });
-
     const result = {
     const result = {
       id: section.id || 0,
       id: section.id || 0,
       name: section.name || '',
       name: section.name || '',
@@ -391,30 +232,16 @@ export class DatabaseExcelReaderService {
       data
       data
     };
     };
 
 
-    logger.debug('Section result', {
-      sectionName: section.name,
-      resultSummary: {
-        id: result.id,
-        name: result.name,
-        tableName: result.tableName,
-        dataRows: result.data.length,
-        fields: result.fields.length
-      }
-    });
-
     return result;
     return result;
   }
   }
 
 
   private parseCellAddress(cellPosition: string, rowNumber: number): { row: number; col: number } {
   private parseCellAddress(cellPosition: string, rowNumber: number): { row: number; col: number } {
-    logger.debug(`Parsing cell address: ${cellPosition}`);
 
 
     let match = cellPosition.match(/([A-Z]+)(\d+)/);
     let match = cellPosition.match(/([A-Z]+)(\d+)/);
     if (!match) {
     if (!match) {
-      logger.warn(`Invalid cell position format: ${cellPosition}, trying to add row number ${rowNumber}`);
       const appendedCellPosition = `${cellPosition}${rowNumber}`;
       const appendedCellPosition = `${cellPosition}${rowNumber}`;
       match = appendedCellPosition.match(/([A-Z]+)(\d+)/);
       match = appendedCellPosition.match(/([A-Z]+)(\d+)/);
       if (!match) {
       if (!match) {
-        logger.warn(`Invalid cell position format: ${appendedCellPosition}, using default 1,1`);
         return { row: 1, col: 1 };
         return { row: 1, col: 1 };
       }
       }
     }
     }
@@ -422,18 +249,10 @@ export class DatabaseExcelReaderService {
     const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
     const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
     const row = parseInt(match[2]);
     const row = parseInt(match[2]);
 
 
-    logger.debug(`Parsed cell address`, {
-      original: cellPosition,
-      row,
-      col
-    });
-
     return { row, col };
     return { row, col };
   }
   }
 
 
   private mapSectionType(type: string): SectionTypeEnum {
   private mapSectionType(type: string): SectionTypeEnum {
-    logger.debug(`Mapping section type: ${type}`);
-
     const mappedType = (() => {
     const mappedType = (() => {
       switch (type?.toLowerCase()) {
       switch (type?.toLowerCase()) {
         case 'grid':
         case 'grid':
@@ -445,17 +264,10 @@ export class DatabaseExcelReaderService {
       }
       }
     })();
     })();
 
 
-    logger.debug(`Section type mapped`, {
-      originalType: type,
-      mappedType: SectionTypeEnum[mappedType]
-    });
-
     return mappedType;
     return mappedType;
   }
   }
 
 
   private mapFields(fields: any[]): LayoutSectionField[] {
   private mapFields(fields: any[]): LayoutSectionField[] {
-    logger.debug(`Mapping ${fields.length} fields`);
-
     const mappedFields = fields.map((field, index) => {
     const mappedFields = fields.map((field, index) => {
       const mappedField = {
       const mappedField = {
         id: field.id || index,
         id: field.id || index,
@@ -468,14 +280,6 @@ export class DatabaseExcelReaderService {
         parsedType: this.mapFieldType(field.dataType)
         parsedType: this.mapFieldType(field.dataType)
       };
       };
 
 
-      logger.debug(`Field mapped`, {
-        index,
-        originalName: field.name,
-        mappedName: mappedField.name,
-        cellPosition: mappedField.cellPosition,
-        parsedType: FieldTypeEnum[mappedField.parsedType]
-      });
-
       return mappedField;
       return mappedField;
     });
     });
 
 
@@ -504,26 +308,14 @@ export class DatabaseExcelReaderService {
       }
       }
     })();
     })();
 
 
-    logger.debug(`Field type mapped`, {
-      originalDataType: dataType,
-      mappedType: FieldTypeEnum[mappedType]
-    });
-
     return mappedType;
     return mappedType;
   }
   }
 
 
   private convertCellValue(dataType: string, dataTypeFormat: string | undefined, value: any, fieldType: FieldTypeEnum): any {
   private convertCellValue(dataType: string, dataTypeFormat: string | undefined, value: any, fieldType: FieldTypeEnum): any {
     if (value === null || value === undefined) {
     if (value === null || value === undefined) {
-      logger.debug(`Converting null/undefined value to null`, { fieldType: FieldTypeEnum[fieldType] });
       return null;
       return null;
     }
     }
 
 
-    logger.debug(`Converting cell value`, {
-      originalValue: value,
-      originalType: typeof value,
-      targetFieldType: FieldTypeEnum[fieldType]
-    });
-
     const convertedValue = (() => {
     const convertedValue = (() => {
       if (dataType === "DATE")
       if (dataType === "DATE")
         return XLSX.SSF.format(dataTypeFormat || 'yyyy-mm-dd', value);
         return XLSX.SSF.format(dataTypeFormat || 'yyyy-mm-dd', value);
@@ -533,14 +325,12 @@ export class DatabaseExcelReaderService {
           if (typeof value === 'number') {
           if (typeof value === 'number') {
             // Excel time is fraction of a day
             // Excel time is fraction of a day
             const result = value * 24 * 60 * 60 * 1000; // Convert to milliseconds
             const result = value * 24 * 60 * 60 * 1000; // Convert to milliseconds
-            logger.debug(`Time conversion`, { original: value, converted: result });
             return result;
             return result;
           }
           }
           return value;
           return value;
 
 
         case FieldTypeEnum.Decimal:
         case FieldTypeEnum.Decimal:
           const decimalResult = parseFloat(value.toString()) || 0;
           const decimalResult = parseFloat(value.toString()) || 0;
-          logger.debug(`Decimal conversion`, { original: value, converted: decimalResult });
           return decimalResult;
           return decimalResult;
 
 
         case FieldTypeEnum.Date:
         case FieldTypeEnum.Date:
@@ -548,22 +338,18 @@ export class DatabaseExcelReaderService {
             // Excel date is days since 1900-01-01
             // Excel date is days since 1900-01-01
             const excelEpoch = new Date(1900, 0, 1);
             const excelEpoch = new Date(1900, 0, 1);
             const dateResult = new Date(excelEpoch.getTime() + (value - 1) * 24 * 60 * 60 * 1000);
             const dateResult = new Date(excelEpoch.getTime() + (value - 1) * 24 * 60 * 60 * 1000);
-            logger.debug(`Date conversion`, { original: value, converted: dateResult });
             return dateResult;
             return dateResult;
           }
           }
           const dateResult = new Date(value);
           const dateResult = new Date(value);
-          logger.debug(`Date conversion from string`, { original: value, converted: dateResult });
           return dateResult;
           return dateResult;
 
 
         case FieldTypeEnum.Numeric:
         case FieldTypeEnum.Numeric:
           const numericResult = parseInt(value.toString()) || 0;
           const numericResult = parseInt(value.toString()) || 0;
-          logger.debug(`Numeric conversion`, { original: value, converted: numericResult });
           return numericResult;
           return numericResult;
 
 
         case FieldTypeEnum.String:
         case FieldTypeEnum.String:
         default:
         default:
           const stringResult = value.toString();
           const stringResult = value.toString();
-          logger.debug(`String conversion`, { original: value, converted: stringResult });
           return stringResult;
           return stringResult;
       }
       }
     })();
     })();