presseportale/app/Console/Commands/AnalyzeLegacyApiAccessLogs.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

87 lines
2.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\Api\LegacyApiAccessLogAnalyzer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class AnalyzeLegacyApiAccessLogs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'api:analyze-legacy-access-logs
{paths* : Access-log files or glob patterns}
{--top=20 : Number of top entries per section}
{--no-report : Keinen JSON-Report schreiben}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Analysiert Legacy-API-Zugriffe aus Webserver-Access-Logs.';
/**
* Execute the console command.
*/
public function handle(LegacyApiAccessLogAnalyzer $analyzer): int
{
$paths = array_map('strval', (array) $this->argument('paths'));
$top = max(1, (int) $this->option('top'));
$report = $analyzer->analyze($paths, $top);
$this->info('Legacy-API-Access-Log-Auswertung');
$this->newLine();
$this->line("Dateien: {$report['summary']['files']}");
$this->line("Log-Zeilen: {$report['summary']['total_lines']}");
$this->line("Legacy-API-Requests: {$report['summary']['matched_requests']}");
$this->line("Requests mit api_key: {$report['summary']['legacy_key_requests']}");
$this->line("Eindeutige Client-IPs: {$report['summary']['unique_client_ips']}");
$this->line("Eindeutige API-Key-Fingerprints: {$report['summary']['unique_api_key_fingerprints']}");
if ($report['missing_paths'] !== []) {
$this->newLine();
$this->warn('Nicht lesbare Pfade:');
foreach ($report['missing_paths'] as $path) {
$this->line(" - {$path}");
}
}
$this->renderTopTable('Top Endpoints', $report['endpoints']);
$this->renderTopTable('Top Client-IPs', $report['client_ips']);
$this->renderTopTable('Statuscodes', $report['status_codes']);
if (! (bool) $this->option('no-report')) {
$path = 'migration/legacy-api-access-'.now()->format('Ymd-His').'.json';
Storage::disk('local')->put($path, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$this->newLine();
$this->line("Report geschrieben: storage/app/private/{$path}");
}
return self::SUCCESS;
}
/**
* @param array<string, int> $rows
*/
private function renderTopTable(string $title, array $rows): void
{
if ($rows === []) {
return;
}
$this->newLine();
$this->line($title);
$this->table(
['Wert', 'Requests'],
collect($rows)
->map(fn (int $count, string $value): array => [$value, $count])
->values()
->all()
);
}
}