diff --git a/app/Commands/AbdmPruneLogs.php b/app/Commands/AbdmPruneLogs.php index 92c43c6..4f18e5c 100644 --- a/app/Commands/AbdmPruneLogs.php +++ b/app/Commands/AbdmPruneLogs.php @@ -9,14 +9,15 @@ class AbdmPruneLogs extends BaseCommand { protected $group = 'ABDM'; protected $name = 'abdm:prune-logs'; - protected $description = 'Delete ABDM log rows older than the retention window (default 2 days).'; - protected $usage = 'abdm:prune-logs [--days 2] [--table all] [--batch 2000] [--dry-run] [--optimize]'; + protected $description = 'Strip bulky ABDM log payloads and delete rows past the retention window.'; + protected $usage = 'abdm:prune-logs [--days 30] [--strip-payload-days 2] [--table all] [--batch 2000] [--dry-run] [--optimize]'; protected $arguments = []; protected $options = [ - '--days' => 'Retention window in days (default: 2).', + '--days' => 'Delete rows older than this many days (default: 30).', + '--strip-payload-days' => 'Blank request/response JSON older than this many days (default: 2, 0 disables).', '--table' => 'all | abdm_api_logs | abdm_hiu_workflows (default: all).', '--batch' => 'Rows deleted per statement (default: 2000).', - '--dry-run' => 'Report what would be deleted without deleting.', + '--dry-run' => 'Report what would be changed without writing.', '--optimize' => 'Rebuild the table afterwards to release disk space.', ]; @@ -31,9 +32,17 @@ class AbdmPruneLogs extends BaseCommand public function run(array $params) { - $days = (int) (CLI::getOption('days') ?? 2); + $days = (int) (CLI::getOption('days') ?? 30); if ($days <= 0) { - $days = 2; + $days = 30; + } + + // Payloads (FHIR bundles, ABHA cards) are ~99% of the size, so they go + // early while the request/response metadata stays for the audit trail. + $stripDays = CLI::getOption('strip-payload-days'); + $stripDays = $stripDays === null ? 2 : (int) $stripDays; + if ($stripDays < 0) { + $stripDays = 0; } $batch = (int) (CLI::getOption('batch') ?? 2000); @@ -59,9 +68,13 @@ class AbdmPruneLogs extends BaseCommand $db = \Config\Database::connect(); $cutoff = date('Y-m-d H:i:s', strtotime('-' . $days . ' days')); + $stripCutoff = $stripDays > 0 ? date('Y-m-d H:i:s', strtotime('-' . $stripDays . ' days')) : ''; CLI::write('ABDM Log Prune' . ($dryRun ? ' (dry-run)' : ''), 'yellow'); - CLI::write('Retention: ' . $days . ' day(s) — cutoff ' . $cutoff); + CLI::write('Delete rows older than ' . $days . ' day(s) — cutoff ' . $cutoff); + if ($stripCutoff !== '') { + CLI::write('Strip payloads older than ' . $stripDays . ' day(s) — cutoff ' . $stripCutoff); + } foreach ($tables as $table) { if (! $db->tableExists($table)) { @@ -69,13 +82,37 @@ class AbdmPruneLogs extends BaseCommand continue; } - $hasUpdatedAt = in_array('updated_at', $db->getFieldNames($table) ?? [], true); + $fields = $db->getFieldNames($table) ?? []; + $hasUpdatedAt = in_array('updated_at', $fields, true); $age = $hasUpdatedAt ? '((created_at IS NOT NULL AND created_at < ?) OR (created_at IS NULL AND updated_at IS NOT NULL AND updated_at < ?))' : '(created_at IS NOT NULL AND created_at < ?)'; $binds = $hasUpdatedAt ? [$cutoff, $cutoff] : [$cutoff]; $where = $age . self::GUARDS[$table]; + if ($stripCutoff !== '') { + $payloadCols = array_values(array_intersect(['request_json', 'response_json'], $fields)); + if ($payloadCols !== []) { + $sets = []; + $notEmpty = []; + foreach ($payloadCols as $col) { + $sets[] = $col . ' = NULL'; + $notEmpty[] = $col . ' IS NOT NULL'; + } + $stripWhere = '(created_at IS NOT NULL AND created_at < ?) AND (' . implode(' OR ', $notEmpty) . ')'; + + $strippable = (int) ($db->query('SELECT COUNT(*) AS c FROM ' . $table . ' WHERE ' . $stripWhere, [$stripCutoff]) + ->getRowArray()['c'] ?? 0); + + if ($strippable > 0 && $dryRun) { + CLI::write($table . ': ' . $strippable . ' row(s) would have payloads stripped.'); + } elseif ($strippable > 0) { + $db->query('UPDATE ' . $table . ' SET ' . implode(', ', $sets) . ' WHERE ' . $stripWhere, [$stripCutoff]); + CLI::write($table . ': stripped payloads on ' . $strippable . ' row(s).', 'green'); + } + } + } + $total = (int) ($db->query('SELECT COUNT(*) AS c FROM ' . $table . ' WHERE ' . $where, $binds) ->getRowArray()['c'] ?? 0); diff --git a/scripts/install_hms_cron.sh b/scripts/install_hms_cron.sh index 598af72..29edc2a 100644 --- a/scripts/install_hms_cron.sh +++ b/scripts/install_hms_cron.sh @@ -38,7 +38,7 @@ $BLOCK_START * * * * * cd $PROJECT_DIR && $PHP_BIN spark snomed:process-coding >> $LOG_DIR/snomed_process.log 2>&1 */2 * * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:hiu-poll --limit=30 >> $LOG_DIR/abdm_hiu_poll.log 2>&1 */5 * * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:hiu-retry --limit=20 >> $LOG_DIR/abdm_hiu_retry.log 2>&1 -30 2 * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:prune-logs --days=2 --optimize >> $LOG_DIR/abdm_prune_logs.log 2>&1 +30 2 * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:prune-logs --days=30 --strip-payload-days=2 --optimize >> $LOG_DIR/abdm_prune_logs.log 2>&1 $BLOCK_END EOF