Basic server status implementation
This commit is contained in:
		
							parent
							
								
									4c5072b5c0
								
							
						
					
					
						commit
						556ab76fc5
					
				| @ -2,6 +2,8 @@ | |||||||
| 
 | 
 | ||||||
| namespace App\Filament\Resources; | namespace App\Filament\Resources; | ||||||
| 
 | 
 | ||||||
|  | use App\Enums\ContainerStatus; | ||||||
|  | use App\Enums\ServerState; | ||||||
| use App\Filament\Resources\ServerResource\Pages; | use App\Filament\Resources\ServerResource\Pages; | ||||||
| use App\Models\Allocation; | use App\Models\Allocation; | ||||||
| use App\Models\Egg; | use App\Models\Egg; | ||||||
| @ -511,6 +513,40 @@ class ServerResource extends Resource | |||||||
|         return $table |         return $table | ||||||
|             ->searchable(false) |             ->searchable(false) | ||||||
|             ->columns([ |             ->columns([ | ||||||
|  |                 Tables\Columns\TextColumn::make('status') | ||||||
|  |                     ->default('unknown') | ||||||
|  |                     ->badge() | ||||||
|  |                     ->default(function (Server $server) { | ||||||
|  |                         if ($server->status !== null) { | ||||||
|  |                             return $server->status; | ||||||
|  |                         } | ||||||
|  | 
 | ||||||
|  |                         $statuses = collect($server->retrieveStatus()) | ||||||
|  |                             ->mapWithKeys(function ($status) { | ||||||
|  |                                 return [$status['configuration']['uuid'] => $status['state']]; | ||||||
|  |                             })->all(); | ||||||
|  | 
 | ||||||
|  |                         return $statuses[$server->uuid] ?? 'node_fail'; | ||||||
|  |                     }) | ||||||
|  |                     ->icon(fn ($state) => match ($state) { | ||||||
|  |                         'node_fail' => 'tabler-server-off', | ||||||
|  |                         'running' => 'tabler-heartbeat', | ||||||
|  |                         'removing' => 'tabler-heart-x', | ||||||
|  |                         'offline' => 'tabler-heart-off', | ||||||
|  |                         'paused' => 'tabler-heart-pause', | ||||||
|  |                         'installing' => 'tabler-heart-bolt', | ||||||
|  |                         'suspended' => 'tabler-heart-cancel', | ||||||
|  |                         default => 'tabler-heart-question', | ||||||
|  |                     }) | ||||||
|  |                     ->color(fn (string $state): string => match ($state) { | ||||||
|  |                         'running' => 'success', | ||||||
|  |                         'installing', 'restarting' => 'primary', | ||||||
|  |                         'paused', 'removing' => 'warning', | ||||||
|  |                         'node_fail', 'install_failed', 'suspended' => 'danger', | ||||||
|  |                         default => 'gray', | ||||||
|  |                     }) | ||||||
|  |                 , | ||||||
|  | 
 | ||||||
|                 Tables\Columns\TextColumn::make('uuid') |                 Tables\Columns\TextColumn::make('uuid') | ||||||
|                     ->hidden() |                     ->hidden() | ||||||
|                     ->label('UUID') |                     ->label('UUID') | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ namespace App\Models; | |||||||
| use App\Exceptions\Service\HasActiveServersException; | use App\Exceptions\Service\HasActiveServersException; | ||||||
| use App\Repositories\Daemon\DaemonConfigurationRepository; | use App\Repositories\Daemon\DaemonConfigurationRepository; | ||||||
| use Exception; | use Exception; | ||||||
|  | use Illuminate\Support\Facades\Http; | ||||||
| use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||||
| use Symfony\Component\Yaml\Yaml; | use Symfony\Component\Yaml\Yaml; | ||||||
| use Illuminate\Notifications\Notifiable; | use Illuminate\Notifications\Notifiable; | ||||||
| @ -288,4 +289,17 @@ class Node extends Model | |||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public function serverStatuses(): array | ||||||
|  |     { | ||||||
|  |         try { | ||||||
|  |             /** @var \Illuminate\Http\Client\Response $response */ | ||||||
|  |             $response = Http::daemon($this)->connectTimeout(1)->timeout(1)->get('/api/servers'); | ||||||
|  |             $statuses = $response->json(); | ||||||
|  |         } catch (Exception) { | ||||||
|  |             $statuses = []; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return cache()->remember("nodes.$this->id.servers", now()->addSeconds(2), fn () => $statuses); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -394,4 +394,9 @@ class Server extends Model | |||||||
|             throw new DaemonConnectionException($exception); |             throw new DaemonConnectionException($exception); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public function retrieveStatus() | ||||||
|  |     { | ||||||
|  |         return $this->node->serverStatuses(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Lance Pioch
						Lance Pioch