Auto update resources on server list (#737)

* auto update resources on server list

* use Arr::get helper
This commit is contained in:
Boy132 2024-12-01 18:12:58 +01:00 committed by GitHub
parent 355810c549
commit e5433b7aab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 5 deletions

View File

@ -10,6 +10,7 @@ use Carbon\CarbonInterface;
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
@ -21,6 +22,7 @@ class ListServers extends ListRecords
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 +42,7 @@ class ListServers extends ListRecords
// @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';
@ -52,7 +54,7 @@ class ListServers extends ListRecords
// @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 : '');
@ -61,8 +63,8 @@ class ListServers extends ListRecords
// @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'
@ -84,7 +86,7 @@ class ListServers extends ListRecords
// @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'

View File

@ -5,11 +5,13 @@ namespace App\Models;
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;
@ -431,6 +433,14 @@ class Server extends Model
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(