feat(webstatement): tambah parameter period ke ProcessDailyMigration command

Menambahkan parameter --period pada command ProcessDailyMigration untuk fleksibilitas pemrosesan data harian.

Perubahan yang dilakukan:

- Menambahkan parameter --period dengan default '-1 day' pada command ProcessDailyMigration
- Memungkinkan input period dalam berbagai format:
  - Format Ymd (contoh: 20250120)
  - Format relative date (contoh: '-2 days', '-1 week')
  - Default fallback ke '-1 day' jika parameter kosong atau format tidak valid

- Update method index di MigrasiController untuk menerima dan memproses parameter period
- Menambahkan method determinePeriod untuk konversi dan validasi parameter period
- Menggunakan Carbon untuk parsing dan konversi tanggal
- Menambahkan logging detail untuk tracking parameter input dan hasil konversi period
- Menambahkan validasi dan error handling jika format periode tidak sesuai
- Mempertahankan backward compatibility agar command lama tetap berjalan seperti sebelumnya
- Update deskripsi command dan signature agar dokumentasi CLI lebih jelas

Tujuan perubahan:

- Memberikan fleksibilitas bagi tim operasional untuk menjalankan migrasi data dengan periode yang spesifik
- Memudahkan eksekusi ulang data harian atau data backdate tanpa modifikasi kode
- Memastikan proses migrasi lebih aman, transparan, dan dapat dipantau melalui logging
This commit is contained in:
Daeng Deni Mardaeni
2025-07-21 11:30:55 +07:00
parent f37707b2f6
commit e5c33bf631
2 changed files with 139 additions and 54 deletions

View File

@@ -1,51 +1,67 @@
<?php
namespace Modules\Webstatement\Console;
namespace Modules\Webstatement\Console;
use Exception;
use Illuminate\Console\Command;
use Modules\Webstatement\Http\Controllers\MigrasiController;
use Exception;
use Illuminate\Console\Command;
use Modules\Webstatement\Http\Controllers\MigrasiController;
use Illuminate\Support\Facades\Log;
class ProcessDailyMigration extends Command
class ProcessDailyMigration extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'webstatement:process-daily-migration
{--process_parameter= : To process migration parameter true/false}
{--period= : Period to process (default: -1 day, format: Ymd or relative date)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Process data migration for the specified period (default: previous day)';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'webstatement:process-daily-migration
{--process_parameter= : To process migration parameter true/false}';
$processParameter = $this->option('process_parameter');
$period = $this->option('period');
/**
* The console command description.
*
* @var string
*/
protected $description = 'Process data migration for the previous day\'s period';
// Log start of process
Log::info('Starting daily data migration process', [
'process_parameter' => $processParameter ?? 'false',
'period' => $period ?? '-1 day'
]);
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$processParameter = $this->option('process_parameter');
$this->info('Starting daily data migration process...');
$this->info('Process Parameter: ' . ($processParameter ?? 'False'));
$this->info('Period: ' . ($period ?? '-1 day (default)'));
$this->info('Starting daily data migration process...');
$this->info('Process Parameter: ' . ($processParameter ?? 'False'));
try {
$controller = app(MigrasiController::class);
$response = $controller->index($processParameter, $period);
try {
$controller = app(MigrasiController::class);
$response = $controller->index($processParameter);
$responseData = json_decode($response->getContent(), true);
$message = $responseData['message'] ?? 'Process completed';
$responseData = json_decode($response->getContent(), true);
$this->info($responseData['message'] ?? 'Process completed');
$this->info($message);
Log::info('Daily migration process completed successfully', ['message' => $message]);
return Command::SUCCESS;
} catch (Exception $e) {
$this->error('Error processing daily migration: ' . $e->getMessage());
return Command::FAILURE;
}
return Command::SUCCESS;
} catch (Exception $e) {
$errorMessage = 'Error processing daily migration: ' . $e->getMessage();
$this->error($errorMessage);
Log::error($errorMessage, ['exception' => $e->getTraceAsString()]);
return Command::FAILURE;
}
}
}

View File

@@ -101,30 +101,99 @@
return response()->json(['error' => $e->getMessage()], 500);
}
}
public function index($processParameter = false)
/**
* Proses migrasi data dengan parameter dan periode yang dapat dikustomisasi
*
* @param bool|string $processParameter Flag untuk memproses parameter
* @param string|null $period Periode yang akan diproses (default: -1 day)
* @return JsonResponse
*/
public function index($processParameter = false, $period = null)
{
$disk = Storage::disk('sftpStatement');
try {
Log::info('Starting migration process', [
'process_parameter' => $processParameter,
'period' => $period
]);
if ($processParameter) {
foreach (self::PARAMETER_PROCESSES as $process) {
$this->processData($process, '_parameter');
$disk = Storage::disk('sftpStatement');
if ($processParameter) {
Log::info('Processing parameter data');
foreach (self::PARAMETER_PROCESSES as $process) {
$this->processData($process, '_parameter');
}
Log::info('Parameter processes completed successfully');
return response()->json(['message' => 'Parameter processes completed successfully']);
}
return response()->json(['message' => 'Parameter processes completed successfully']);
}
$period = date('Ymd', strtotime('-1 day'));
if (!$disk->exists($period)) {
// Tentukan periode yang akan diproses
$targetPeriod = $this->determinePeriod($period);
Log::info('Processing data for period', ['period' => $targetPeriod]);
if (!$disk->exists($targetPeriod)) {
$errorMessage = "Period {$targetPeriod} folder not found in SFTP storage";
Log::warning($errorMessage);
return response()->json([
"message" => $errorMessage
], 404);
}
foreach (self::DATA_PROCESSES as $process) {
$this->processData($process, $targetPeriod);
}
$successMessage = "Data processing for period {$targetPeriod} has been queued successfully";
Log::info($successMessage);
return response()->json([
"message" => "Period {$period} folder not found in SFTP storage"
], 404);
'message' => $successMessage
]);
} catch (Exception $e) {
Log::error('Error in migration index method: ' . $e->getMessage());
return response()->json(['error' => $e->getMessage()], 500);
}
}
/**
* Tentukan periode berdasarkan input atau gunakan default
*
* @param string|null $period Input periode
* @return string Periode dalam format Ymd
*/
private function determinePeriod($period = null): string
{
if ($period === null) {
// Default: -1 day
$calculatedPeriod = date('Ymd', strtotime('-1 day'));
Log::info('Using default period', ['period' => $calculatedPeriod]);
return $calculatedPeriod;
}
foreach (self::DATA_PROCESSES as $process) {
$this->processData($process, $period);
// Jika periode sudah dalam format Ymd (8 digit)
if (preg_match('/^\d{8}$/', $period)) {
Log::info('Using provided period in Ymd format', ['period' => $period]);
return $period;
}
return response()->json([
'message' => "Data processing for period {$period} has been queued successfully"
]);
// Jika periode dalam format relative date (contoh: -2 days, -1 week, etc.)
try {
$calculatedPeriod = date('Ymd', strtotime($period));
Log::info('Calculated period from relative date', [
'input' => $period,
'calculated' => $calculatedPeriod
]);
return $calculatedPeriod;
} catch (Exception $e) {
Log::warning('Invalid period format, using default', [
'input' => $period,
'error' => $e->getMessage()
]);
return date('Ymd', strtotime('-1 day'));
}
}
}