$series */ private function trend(array $series, string $suffix = '%'): array { $cur = (int) (end($series) ?: 0); $prev = (int) ($series[max(0, count($series) - 8)] ?? $cur); $d = $cur - $prev; return [ 'dir' => $d > 1 ? 'up' : ($d < -1 ? 'down' : 'flat'), 'text' => ($d >= 0 ? '+' : '').$d.$suffix, ]; } /** A handful of notable systemd units (failed + running first), cached. */ private function liveServices(FleetService $fleet, ?Server $active): array { if (! $active || ! $active->credential_exists) { return []; } try { $svc = Cache::remember("svc:{$active->id}", 30, fn () => $fleet->systemd($active, 1)['services']); } catch (Throwable) { return []; } $rank = fn (array $s): int => match ($s['sub']) { 'failed' => 0, 'running' => 1, default => 2, }; usort($svc, fn ($a, $b) => [$rank($a), $a['name']] <=> [$rank($b), $b['name']]); return array_slice($svc, 0, 8); } public function render(FleetService $fleet) { $active = $this->activeServer(); // Metrics come from the poller (DB + cache) — no SSH on web render. $latest = $active ? $fleet->latest($active) : null; $cpu = (int) ($latest['cpu'] ?? $active?->cpu ?? 0); $mem = (int) ($latest['mem'] ?? $active?->mem ?? 0); $disk = (int) ($latest['disk'] ?? $active?->disk ?? 0); $load = (float) ($latest['load'] ?? 0); $hist = $active ? $fleet->history($active) : ['cpu' => [], 'mem' => []]; $cpuSeries = $this->pad($hist['cpu'], $cpu); $memSeries = $this->pad($hist['mem'], $mem); $diskSeries = $this->series(max(4, $disk), 40, 2); $loadSeries = $this->series(max(8, (int) round($load * 18)), 40, 6); return view('livewire.dashboard', [ 'active' => $active, 'cpu' => $cpu, 'mem' => $mem, 'disk' => $disk, 'load' => round($load, 2), 'cpuSeries' => $cpuSeries, 'memSeries' => $memSeries, 'diskSeries' => $diskSeries, 'loadSeries' => $loadSeries, 'cpuTrend' => $this->trend($cpuSeries), 'memTrend' => $this->trend($memSeries), 'diskTrend' => $this->trend($diskSeries), 'loadTrend' => $this->trend($loadSeries, ''), 'services' => $this->liveServices($fleet, $active), 'events' => AuditEvent::with('server')->latest()->limit(6)->get(), ]); } }