From 58a5db7303cce4d13c444cce311b2bba47e16fa8 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Fri, 6 Jun 2025 21:00:12 +0700 Subject: [PATCH] refactor(webstatement): sederhanakan logika pemrosesan data transfer dana - Menghapus penggunaan batching data (`transferBatch`) untuk penyederhanaan logika: - Menghapus konstanta `CHUNK_SIZE`. - Menghapus variabel dan metode terkait batching, seperti `transferBatch`, `addToBatch`, dan `saveBatch`. - Memproses dan menyimpan setiap baris data langsung melalui metode `saveRecord`. - Memperbarui metode `processRow`: - Menggantikan logika penambahan batch dengan langsung memanggil `saveRecord`. Signed-off-by: Daeng Deni Mardaeni --- app/Jobs/ProcessFundsTransferDataJob.php | 65 +++--------------------- 1 file changed, 6 insertions(+), 59 deletions(-) diff --git a/app/Jobs/ProcessFundsTransferDataJob.php b/app/Jobs/ProcessFundsTransferDataJob.php index 3bfcfb7..05d6220 100644 --- a/app/Jobs/ProcessFundsTransferDataJob.php +++ b/app/Jobs/ProcessFundsTransferDataJob.php @@ -20,12 +20,10 @@ private const MAX_EXECUTION_TIME = 86400; // 24 hours in seconds private const FILENAME = 'ST.FUNDS.TRANSFER.csv'; private const DISK_NAME = 'sftpStatement'; - private const CHUNK_SIZE = 1000; // Process data in chunks to reduce memory usage private string $period = ''; private int $processedCount = 0; private int $errorCount = 0; - private array $transferBatch = []; /** * Create a new job instance. @@ -63,7 +61,6 @@ set_time_limit(self::MAX_EXECUTION_TIME); $this->processedCount = 0; $this->errorCount = 0; - $this->transferBatch = []; } private function processPeriod() @@ -114,23 +111,10 @@ $headers = (new TempFundsTransfer())->getFillable(); $rowCount = 0; - $chunkCount = 0; while (($row = fgetcsv($handle, 0, self::CSV_DELIMITER)) !== false) { $rowCount++; $this->processRow($row, $headers, $rowCount, $filePath); - - // Process in chunks to avoid memory issues - if (count($this->transferBatch) >= self::CHUNK_SIZE) { - $this->saveBatch(); - $chunkCount++; - Log::info("Processed chunk $chunkCount ({$this->processedCount} records so far)"); - } - } - - // Process any remaining records - if (!empty($this->transferBatch)) { - $this->saveBatch(); } fclose($handle); @@ -152,24 +136,18 @@ } $data = array_combine($headers, $row); - $this->addToBatch($data, $rowCount, $filePath); + $this->saveRecord($data, $rowCount, $filePath); } - /** - * Add record to batch instead of saving immediately - */ - private function addToBatch(array $data, int $rowCount, string $filePath) + private function saveRecord(array $data, int $rowCount, string $filePath) : void { try { if (isset($data['_id']) && $data['_id'] !== '_id') { - // Add timestamp fields - $now = now(); - $data['created_at'] = $now; - $data['updated_at'] = $now; - - // Add to transfer batch - $this->transferBatch[] = $data; + TempFundsTransfer::updateOrCreate( + ['_id' => $data['_id']], + $data + ); $this->processedCount++; } } catch (Exception $e) { @@ -178,37 +156,6 @@ } } - /** - * Save batched records to the database - */ - private function saveBatch() - : void - { - try { - if (!empty($this->transferBatch)) { - // Process in smaller chunks for better memory management - foreach ($this->transferBatch as $entry) { - // Extract all stmt_entry_ids from the current chunk - $entryIds = array_column($entry, '_id'); - - // Delete existing records with these IDs to avoid conflicts - TempFundsTransfer::whereIn('_id', $entryIds)->delete(); - - // Insert all records in the chunk at once - TempFundsTransfer::insert($entry); - } - - // Reset entry batch after processing - $this->transferBatch = []; - } - } catch (Exception $e) { - Log::error("Error in saveBatch: " . $e->getMessage()); - $this->errorCount += count($this->transferBatch); - // Reset batch even if there's an error to prevent reprocessing the same failed records - $this->transferBatch = []; - } - } - private function cleanup(string $tempFilePath) : void {