Skip to content

Commit

Permalink
Auto update resources on server list (#737)
Browse files Browse the repository at this point in the history
* auto update resources on server list

* use Arr::get helper
  • Loading branch information
Boy132 authored Dec 1, 2024
1 parent 355810c commit e5433b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/Filament/App/Resources/ServerResource/Pages/ListServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,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')
Expand All @@ -40,7 +42,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 = Arr::get($server->resources(), 'uptime', 0);

if ($uptime === 0) {
return 'Offline';
Expand All @@ -52,7 +54,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(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 : '');
Expand All @@ -61,8 +63,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 = 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'
Expand All @@ -84,7 +86,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 = 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'
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
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;
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;
Expand Down Expand Up @@ -431,6 +433,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 Arr::get(app(DaemonServerRepository::class)->setServer($this)->getDetails(), 'utilization', []);
});
}

public function condition(): Attribute
{
return Attribute::make(
Expand Down

0 comments on commit e5433b7

Please sign in to comment.