Basic server status implementation

This commit is contained in:
Lance Pioch 2024-04-18 03:53:28 -04:00
parent 4c5072b5c0
commit 556ab76fc5
3 changed files with 55 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace App\Filament\Resources;
use App\Enums\ContainerStatus;
use App\Enums\ServerState;
use App\Filament\Resources\ServerResource\Pages;
use App\Models\Allocation;
use App\Models\Egg;
@ -511,6 +513,40 @@ class ServerResource extends Resource
return $table
->searchable(false)
->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')
->hidden()
->label('UUID')

View File

@ -5,6 +5,7 @@ namespace App\Models;
use App\Exceptions\Service\HasActiveServersException;
use App\Repositories\Daemon\DaemonConfigurationRepository;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
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);
}
}

View File

@ -394,4 +394,9 @@ class Server extends Model
throw new DaemonConnectionException($exception);
}
}
public function retrieveStatus()
{
return $this->node->serverStatuses();
}
}