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:
@@ -1,51 +1,67 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Modules\Webstatement\Console;
|
namespace Modules\Webstatement\Console;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Modules\Webstatement\Http\Controllers\MigrasiController;
|
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()
|
||||||
{
|
{
|
||||||
/**
|
$processParameter = $this->option('process_parameter');
|
||||||
* The name and signature of the console command.
|
$period = $this->option('period');
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $signature = 'webstatement:process-daily-migration
|
|
||||||
{--process_parameter= : To process migration parameter true/false}';
|
|
||||||
|
|
||||||
/**
|
// Log start of process
|
||||||
* The console command description.
|
Log::info('Starting daily data migration process', [
|
||||||
*
|
'process_parameter' => $processParameter ?? 'false',
|
||||||
* @var string
|
'period' => $period ?? '-1 day'
|
||||||
*/
|
]);
|
||||||
protected $description = 'Process data migration for the previous day\'s period';
|
|
||||||
|
|
||||||
/**
|
$this->info('Starting daily data migration process...');
|
||||||
* Execute the console command.
|
$this->info('Process Parameter: ' . ($processParameter ?? 'False'));
|
||||||
*
|
$this->info('Period: ' . ($period ?? '-1 day (default)'));
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
$processParameter = $this->option('process_parameter');
|
|
||||||
|
|
||||||
$this->info('Starting daily data migration process...');
|
try {
|
||||||
$this->info('Process Parameter: ' . ($processParameter ?? 'False'));
|
$controller = app(MigrasiController::class);
|
||||||
|
$response = $controller->index($processParameter, $period);
|
||||||
|
|
||||||
try {
|
$responseData = json_decode($response->getContent(), true);
|
||||||
$controller = app(MigrasiController::class);
|
$message = $responseData['message'] ?? 'Process completed';
|
||||||
$response = $controller->index($processParameter);
|
|
||||||
|
|
||||||
$responseData = json_decode($response->getContent(), true);
|
$this->info($message);
|
||||||
$this->info($responseData['message'] ?? 'Process completed');
|
Log::info('Daily migration process completed successfully', ['message' => $message]);
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->error('Error processing daily migration: ' . $e->getMessage());
|
$errorMessage = 'Error processing daily migration: ' . $e->getMessage();
|
||||||
return Command::FAILURE;
|
$this->error($errorMessage);
|
||||||
}
|
Log::error($errorMessage, ['exception' => $e->getTraceAsString()]);
|
||||||
|
|
||||||
|
return Command::FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -101,30 +101,99 @@
|
|||||||
return response()->json(['error' => $e->getMessage()], 500);
|
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) {
|
$disk = Storage::disk('sftpStatement');
|
||||||
foreach (self::PARAMETER_PROCESSES as $process) {
|
|
||||||
$this->processData($process, '_parameter');
|
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'));
|
// Tentukan periode yang akan diproses
|
||||||
if (!$disk->exists($period)) {
|
$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([
|
return response()->json([
|
||||||
"message" => "Period {$period} folder not found in SFTP storage"
|
'message' => $successMessage
|
||||||
], 404);
|
]);
|
||||||
|
} 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) {
|
// Jika periode sudah dalam format Ymd (8 digit)
|
||||||
$this->processData($process, $period);
|
if (preg_match('/^\d{8}$/', $period)) {
|
||||||
|
Log::info('Using provided period in Ymd format', ['period' => $period]);
|
||||||
|
return $period;
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
// Jika periode dalam format relative date (contoh: -2 days, -1 week, etc.)
|
||||||
'message' => "Data processing for period {$period} has been queued successfully"
|
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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user