Add superadmin ops health dashboard
This commit is contained in:
103
app/Filament/Widgets/QueueHealthWidget.php
Normal file
103
app/Filament/Widgets/QueueHealthWidget.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user