From 3b348cd8e657802caa71fe80e000f3d10584fd1f Mon Sep 17 00:00:00 2001 From: Boy132 Date: Sun, 1 Dec 2024 15:58:45 +0100 Subject: [PATCH 1/2] auto update resources on server list --- .../Resources/ServerResource/Pages/ListServers.php | 11 ++++++----- app/Models/Server.php | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/Filament/App/Resources/ServerResource/Pages/ListServers.php b/app/Filament/App/Resources/ServerResource/Pages/ListServers.php index 8becc75542..10f74bab97 100644 --- a/app/Filament/App/Resources/ServerResource/Pages/ListServers.php +++ b/app/Filament/App/Resources/ServerResource/Pages/ListServers.php @@ -21,6 +21,7 @@ public function table(Table $table): Table return $table ->paginated(false) ->query(fn () => auth()->user()->can('viewList server') ? Server::query() : auth()->user()->accessibleServers()) + ->poll('15s') ->columns([ Stack::make([ ServerEntryColumn::make('server_entry') @@ -40,7 +41,7 @@ public function table(Table $table): Table // @phpstan-ignore-next-line private function uptime(Server $server): string { - $uptime = collect(cache()->get("servers.{$server->id}.uptime"))->last() ?? 0; + $uptime = $server->resources()['uptime'] ?? 0; if ($uptime === 0) { return 'Offline'; @@ -52,7 +53,7 @@ private function uptime(Server $server): string // @phpstan-ignore-next-line private function cpu(Server $server): string { - $cpu = Number::format(collect(cache()->get("servers.{$server->id}.cpu_absolute"))->last() ?? 0, maxPrecision: 2, locale: auth()->user()->language) . '%'; + $cpu = Number::format($server->resources()['cpu_absolute'] ?? 0, maxPrecision: 2, locale: auth()->user()->language) . '%'; $max = Number::format($server->cpu, locale: auth()->user()->language) . '%'; return $cpu . ($server->cpu > 0 ? ' Of ' . $max : ''); @@ -61,8 +62,8 @@ private function cpu(Server $server): string // @phpstan-ignore-next-line private function memory(Server $server): string { - $latestMemoryUsed = collect(cache()->get("servers.{$server->id}.memory_bytes"))->last() ?? 0; - $totalMemory = collect(cache()->get("servers.{$server->id}.memory_limit_bytes"))->last() ?? 0; + $latestMemoryUsed = $server->resources()['memory_bytes'] ?? 0; + $totalMemory = $server->resources()['memory_limit_bytes'] ?? 0; $used = config('panel.use_binary_prefix') ? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' @@ -84,7 +85,7 @@ private function memory(Server $server): string // @phpstan-ignore-next-line private function disk(Server $server): string { - $usedDisk = collect(cache()->get("servers.{$server->id}.disk_bytes"))->last() ?? 0; + $usedDisk = $server->resources()['disk_bytes'] ?? 0; $used = config('panel.use_binary_prefix') ? Number::format($usedDisk / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' diff --git a/app/Models/Server.php b/app/Models/Server.php index fbad9c9103..5d7917a84e 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -5,6 +5,7 @@ use App\Enums\ContainerStatus; use App\Enums\ServerState; use App\Exceptions\Http\Connection\DaemonConnectionException; +use App\Repositories\Daemon\DaemonServerRepository; use GuzzleHttp\Exception\GuzzleException; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -431,6 +432,14 @@ public function retrieveStatus(): string return cache()->get("servers.$this->uuid.container.status") ?? 'missing'; } + public function resources(): array + { + return cache()->remember("resources:$this->uuid", now()->addSeconds(15), function () { + // @phpstan-ignore-next-line + return app(DaemonServerRepository::class)->setServer($this)->getDetails()['utilization'] ?? []; + }); + } + public function condition(): Attribute { return Attribute::make( From 6976edb8555d55a5d5b64cba91081cc7cd0f9494 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Sun, 1 Dec 2024 16:03:00 +0100 Subject: [PATCH 2/2] use Arr::get helper --- .../Resources/ServerResource/Pages/ListServers.php | 11 ++++++----- app/Models/Server.php | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Filament/App/Resources/ServerResource/Pages/ListServers.php b/app/Filament/App/Resources/ServerResource/Pages/ListServers.php index 10f74bab97..88f6eee38c 100644 --- a/app/Filament/App/Resources/ServerResource/Pages/ListServers.php +++ b/app/Filament/App/Resources/ServerResource/Pages/ListServers.php @@ -10,6 +10,7 @@ use Filament\Resources\Pages\ListRecords; use Filament\Tables\Columns\Layout\Stack; use Filament\Tables\Table; +use Illuminate\Support\Arr; use Illuminate\Support\Number; class ListServers extends ListRecords @@ -41,7 +42,7 @@ public function table(Table $table): Table // @phpstan-ignore-next-line private function uptime(Server $server): string { - $uptime = $server->resources()['uptime'] ?? 0; + $uptime = Arr::get($server->resources(), 'uptime', 0); if ($uptime === 0) { return 'Offline'; @@ -53,7 +54,7 @@ private function uptime(Server $server): string // @phpstan-ignore-next-line private function cpu(Server $server): string { - $cpu = Number::format($server->resources()['cpu_absolute'] ?? 0, maxPrecision: 2, locale: auth()->user()->language) . '%'; + $cpu = Number::format(Arr::get($server->resources(), 'cpu_absolute', 0), maxPrecision: 2, locale: auth()->user()->language) . '%'; $max = Number::format($server->cpu, locale: auth()->user()->language) . '%'; return $cpu . ($server->cpu > 0 ? ' Of ' . $max : ''); @@ -62,8 +63,8 @@ private function cpu(Server $server): string // @phpstan-ignore-next-line private function memory(Server $server): string { - $latestMemoryUsed = $server->resources()['memory_bytes'] ?? 0; - $totalMemory = $server->resources()['memory_limit_bytes'] ?? 0; + $latestMemoryUsed = Arr::get($server->resources(), 'memory_bytes', 0); + $totalMemory = Arr::get($server->resources(), 'memory_limit_bytes', 0); $used = config('panel.use_binary_prefix') ? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' @@ -85,7 +86,7 @@ private function memory(Server $server): string // @phpstan-ignore-next-line private function disk(Server $server): string { - $usedDisk = $server->resources()['disk_bytes'] ?? 0; + $usedDisk = Arr::get($server->resources(), 'disk_bytes', 0); $used = config('panel.use_binary_prefix') ? Number::format($usedDisk / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' diff --git a/app/Models/Server.php b/app/Models/Server.php index 5d7917a84e..a849f637ca 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Query\JoinClause; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Http; use Psr\Http\Message\ResponseInterface; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -436,7 +437,7 @@ public function resources(): array { return cache()->remember("resources:$this->uuid", now()->addSeconds(15), function () { // @phpstan-ignore-next-line - return app(DaemonServerRepository::class)->setServer($this)->getDetails()['utilization'] ?? []; + return Arr::get(app(DaemonServerRepository::class)->setServer($this)->getDetails(), 'utilization', []); }); }