mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-10-27 15:16:51 +01:00 
			
		
		
		
	Auto update resources on server list (#737)
* auto update resources on server list * use Arr::get helper
This commit is contained in:
		
							parent
							
								
									355810c549
								
							
						
					
					
						commit
						e5433b7aab
					
				| @ -10,6 +10,7 @@ use Carbon\CarbonInterface; | |||||||
| use Filament\Resources\Pages\ListRecords; | use Filament\Resources\Pages\ListRecords; | ||||||
| use Filament\Tables\Columns\Layout\Stack; | use Filament\Tables\Columns\Layout\Stack; | ||||||
| use Filament\Tables\Table; | use Filament\Tables\Table; | ||||||
|  | use Illuminate\Support\Arr; | ||||||
| use Illuminate\Support\Number; | use Illuminate\Support\Number; | ||||||
| 
 | 
 | ||||||
| class ListServers extends ListRecords | class ListServers extends ListRecords | ||||||
| @ -21,6 +22,7 @@ class ListServers extends ListRecords | |||||||
|         return $table |         return $table | ||||||
|             ->paginated(false) |             ->paginated(false) | ||||||
|             ->query(fn () => auth()->user()->can('viewList server') ? Server::query() : auth()->user()->accessibleServers()) |             ->query(fn () => auth()->user()->can('viewList server') ? Server::query() : auth()->user()->accessibleServers()) | ||||||
|  |             ->poll('15s') | ||||||
|             ->columns([ |             ->columns([ | ||||||
|                 Stack::make([ |                 Stack::make([ | ||||||
|                     ServerEntryColumn::make('server_entry') |                     ServerEntryColumn::make('server_entry') | ||||||
| @ -40,7 +42,7 @@ class ListServers extends ListRecords | |||||||
|     // @phpstan-ignore-next-line
 |     // @phpstan-ignore-next-line
 | ||||||
|     private function uptime(Server $server): string |     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) { |         if ($uptime === 0) { | ||||||
|             return 'Offline'; |             return 'Offline'; | ||||||
| @ -52,7 +54,7 @@ class ListServers extends ListRecords | |||||||
|     // @phpstan-ignore-next-line
 |     // @phpstan-ignore-next-line
 | ||||||
|     private function cpu(Server $server): string |     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) . '%'; |         $max = Number::format($server->cpu, locale: auth()->user()->language) . '%'; | ||||||
| 
 | 
 | ||||||
|         return $cpu . ($server->cpu > 0 ? ' Of ' . $max : ''); |         return $cpu . ($server->cpu > 0 ? ' Of ' . $max : ''); | ||||||
| @ -61,8 +63,8 @@ class ListServers extends ListRecords | |||||||
|     // @phpstan-ignore-next-line
 |     // @phpstan-ignore-next-line
 | ||||||
|     private function memory(Server $server): string |     private function memory(Server $server): string | ||||||
|     { |     { | ||||||
|         $latestMemoryUsed = collect(cache()->get("servers.{$server->id}.memory_bytes"))->last() ?? 0; |         $latestMemoryUsed = Arr::get($server->resources(), 'memory_bytes', 0); | ||||||
|         $totalMemory = collect(cache()->get("servers.{$server->id}.memory_limit_bytes"))->last() ?? 0; |         $totalMemory = Arr::get($server->resources(), 'memory_limit_bytes', 0); | ||||||
| 
 | 
 | ||||||
|         $used = config('panel.use_binary_prefix') |         $used = config('panel.use_binary_prefix') | ||||||
|             ? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' |             ? 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
 |     // @phpstan-ignore-next-line
 | ||||||
|     private function disk(Server $server): string |     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') |         $used = config('panel.use_binary_prefix') | ||||||
|             ? Number::format($usedDisk / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' |             ? Number::format($usedDisk / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' | ||||||
|  | |||||||
| @ -5,11 +5,13 @@ namespace App\Models; | |||||||
| use App\Enums\ContainerStatus; | use App\Enums\ContainerStatus; | ||||||
| use App\Enums\ServerState; | use App\Enums\ServerState; | ||||||
| use App\Exceptions\Http\Connection\DaemonConnectionException; | use App\Exceptions\Http\Connection\DaemonConnectionException; | ||||||
|  | use App\Repositories\Daemon\DaemonServerRepository; | ||||||
| use GuzzleHttp\Exception\GuzzleException; | use GuzzleHttp\Exception\GuzzleException; | ||||||
| use Illuminate\Database\Eloquent\Casts\Attribute; | use Illuminate\Database\Eloquent\Casts\Attribute; | ||||||
| use Illuminate\Database\Eloquent\Relations\BelongsToMany; | use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||||||
| use Illuminate\Notifications\Notifiable; | use Illuminate\Notifications\Notifiable; | ||||||
| use Illuminate\Database\Query\JoinClause; | use Illuminate\Database\Query\JoinClause; | ||||||
|  | use Illuminate\Support\Arr; | ||||||
| use Illuminate\Support\Facades\Http; | use Illuminate\Support\Facades\Http; | ||||||
| use Psr\Http\Message\ResponseInterface; | use Psr\Http\Message\ResponseInterface; | ||||||
| use Illuminate\Database\Eloquent\Relations\HasOne; | use Illuminate\Database\Eloquent\Relations\HasOne; | ||||||
| @ -431,6 +433,14 @@ class Server extends Model | |||||||
|         return cache()->get("servers.$this->uuid.container.status") ?? 'missing'; |         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 |     public function condition(): Attribute | ||||||
|     { |     { | ||||||
|         return Attribute::make( |         return Attribute::make( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Boy132
						Boy132