presseportale/app/Console/Commands/ReportApiUsage.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

92 lines
3.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\Api\ApiUsageReporter;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class ReportApiUsage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'api:usage-report
{--from= : Startzeitpunkt, z.B. 2026-04-01 oder 2026-04-01 00:00:00}
{--to= : Endzeitpunkt}
{--user= : Auf User-ID filtern}
{--status= : Auf HTTP-Statuscode filtern}
{--top=10 : Anzahl Top-Zeilen pro Abschnitt}
{--no-report : Keinen JSON-Report schreiben}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Erstellt einen Report aus den protokollierten API-Usage-Logs.';
/**
* Execute the console command.
*/
public function handle(ApiUsageReporter $reporter): int
{
$top = max(1, (int) $this->option('top'));
$userId = $this->option('user') !== null ? (int) $this->option('user') : null;
$statusCode = $this->option('status') !== null ? (int) $this->option('status') : null;
$report = $reporter->report(
from: $this->option('from') !== null ? (string) $this->option('from') : null,
to: $this->option('to') !== null ? (string) $this->option('to') : null,
userId: $userId,
statusCode: $statusCode,
top: $top,
);
$this->info('API-Usage-Report');
$this->newLine();
$this->line("Requests: {$report['summary']['total_requests']}");
$this->line("Eindeutige User: {$report['summary']['unique_users']}");
$this->line("Eindeutige Tokens: {$report['summary']['unique_tokens']}");
$this->line("2xx: {$report['summary']['successful_requests']}");
$this->line("4xx: {$report['summary']['client_error_requests']}");
$this->line("5xx: {$report['summary']['server_error_requests']}");
$this->line("Durchschnitt Dauer: {$report['summary']['average_duration_ms']} ms");
$this->renderRows('Top Pfade', ['Pfad', 'Requests'], $report['top_paths']);
$this->renderRows('Statuscodes', ['Status', 'Requests'], $report['status_codes']);
$this->renderRows('Top User', ['User-ID', 'Requests'], $report['top_users']);
$this->renderRows('Top Tokens', ['Token-ID', 'Requests'], $report['top_tokens']);
if (! (bool) $this->option('no-report')) {
$path = 'migration/api-usage-'.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 list<string> $headers
* @param list<array{value: mixed, requests: int}> $rows
*/
private function renderRows(string $title, array $headers, array $rows): void
{
if ($rows === []) {
return;
}
$this->newLine();
$this->line($title);
$this->table(
$headers,
collect($rows)
->map(fn (array $row): array => [$row['value'], $row['requests']])
->all()
);
}
}