Files
fotospiel-app/app/Filament/Widgets/QueueHealthWidget.php
Codex Agent 2fc8232d57
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Add superadmin ops health dashboard
2026-01-01 21:07:33 +01:00

104 lines
3.4 KiB
PHP

<?php
namespace App\Filament\Widgets;
use Filament\Widgets\Widget;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
class QueueHealthWidget extends Widget
{
protected string $view = 'filament.widgets.queue-health';
protected ?string $pollingInterval = '60s';
protected function getViewData(): array
{
$snapshot = Cache::get('storage:queue-health:last');
$stalledMinutes = (int) config('storage-monitor.queue_health.stalled_minutes', 10);
if (! is_array($snapshot)) {
return [
'snapshotMissing' => true,
'connection' => (string) config('queue.default'),
'snapshotLabel' => __('admin.ops_health.snapshot_missing'),
'queues' => [],
'alerts' => [],
'alertCount' => 0,
'stalledAssets' => 0,
'stalledMinutes' => $stalledMinutes,
];
}
$queues = collect($snapshot['queues'] ?? [])
->map(fn (array $queue) => $this->formatQueue($queue))
->values()
->all();
$alerts = collect($snapshot['alerts'] ?? [])
->map(fn (array $alert) => $this->formatAlert($alert))
->values()
->all();
$snapshotAge = $this->formatSnapshotAge($snapshot['generated_at'] ?? null);
$snapshotLabel = $snapshotAge
? __('admin.ops_health.snapshot_age', ['age' => $snapshotAge])
: __('admin.ops_health.snapshot_missing');
return [
'snapshotMissing' => false,
'connection' => (string) ($snapshot['connection'] ?? config('queue.default')),
'snapshotLabel' => $snapshotLabel,
'queues' => $queues,
'alerts' => $alerts,
'alertCount' => count($alerts),
'stalledAssets' => (int) ($snapshot['stalled_assets'] ?? 0),
'stalledMinutes' => $stalledMinutes,
];
}
private function formatQueue(array $queue): array
{
$size = (int) ($queue['size'] ?? 0);
$failed = (int) ($queue['failed'] ?? 0);
$limits = $queue['limits'] ?? [];
return [
'name' => (string) ($queue['queue'] ?? 'default'),
'size' => $size,
'size_label' => $size < 0 ? '-' : number_format($size),
'failed' => $failed,
'failed_label' => number_format($failed),
'severity' => (string) ($queue['severity'] ?? 'unknown'),
'warning' => (int) ($limits['warning'] ?? 0),
'critical' => (int) ($limits['critical'] ?? 0),
];
}
private function formatAlert(array $alert): array
{
return [
'queue' => $alert['queue'] ?? null,
'type' => (string) ($alert['type'] ?? 'unknown'),
'severity' => (string) ($alert['severity'] ?? 'warning'),
'size' => $alert['size'] ?? null,
'failed' => $alert['failed'] ?? null,
'count' => $alert['count'] ?? null,
'older_than_minutes' => $alert['older_than_minutes'] ?? null,
];
}
private function formatSnapshotAge(?string $timestamp): ?string
{
if (! $timestamp) {
return null;
}
try {
return Carbon::parse($timestamp)->diffForHumans();
} catch (\Throwable) {
return null;
}
}
}