Simplify states and statuses and resolve #362

This commit is contained in:
Lance Pioch 2024-06-17 18:18:48 -04:00
parent 7986505b99
commit 958e8fac8a
4 changed files with 49 additions and 60 deletions

View File

@ -12,6 +12,7 @@ enum ContainerStatus: string
case Paused = 'paused'; case Paused = 'paused';
case Dead = 'dead'; case Dead = 'dead';
case Removing = 'removing'; case Removing = 'removing';
case Offline = 'offline';
// HTTP Based // HTTP Based
case Missing = 'missing'; case Missing = 'missing';
@ -27,6 +28,7 @@ enum ContainerStatus: string
self::Dead => 'tabler-heart-x', self::Dead => 'tabler-heart-x',
self::Removing => 'tabler-heart-down', self::Removing => 'tabler-heart-down',
self::Missing => 'tabler-heart-question', self::Missing => 'tabler-heart-question',
self::Offline => 'tabler-heart-bolt',
}; };
} }
@ -41,6 +43,7 @@ enum ContainerStatus: string
self::Dead => 'danger', self::Dead => 'danger',
self::Removing => 'warning', self::Removing => 'warning',
self::Missing => 'danger', self::Missing => 'danger',
self::Offline => 'gray',
}; };
} }
} }

View File

@ -39,47 +39,19 @@ class EditServer extends EditRecord
'lg' => 4, 'lg' => 4,
]) ])
->schema([ ->schema([
Forms\Components\ToggleButtons::make('docker') Forms\Components\ToggleButtons::make('condition')
->label('Container Status')->inline()->inlineLabel() ->label('')
->formatStateUsing(function ($state, Server $server) { ->inline()
if ($server->node_id === null) { ->formatStateUsing(fn (Server $server) => $server->condition)
return 'unknown'; ->options(fn ($state) => collect(ContainerStatus::cases())->merge(ServerState::cases())
} ->filter(fn ($condition) => $condition->value === $state)
->mapWithKeys(fn ($state) => [$state->value => str($state->value)->replace('_', ' ')->ucwords()])
/** @var DaemonServerRepository $service */ )
$service = resolve(DaemonServerRepository::class); ->colors(collect(ContainerStatus::cases())->merge(ServerState::cases())->mapWithKeys(
$details = $service->setServer($server)->getDetails(); fn ($status) => [$status->value => $status->color()]
return $details['state'] ?? 'unknown';
})
->options(fn ($state) => collect(ContainerStatus::cases())->filter(fn ($containerStatus) => $containerStatus->value === $state)->mapWithKeys(
fn (ContainerStatus $state) => [$state->value => str($state->value)->replace('_', ' ')->ucwords()]
)) ))
->colors(collect(ContainerStatus::cases())->mapWithKeys( ->icons(collect(ContainerStatus::cases())->merge(ServerState::cases())->mapWithKeys(
fn (ContainerStatus $status) => [$status->value => $status->color()] fn ($status) => [$status->value => $status->icon()]
))
->icons(collect(ContainerStatus::cases())->mapWithKeys(
fn (ContainerStatus $status) => [$status->value => $status->icon()]
))
->columnSpan([
'default' => 1,
'sm' => 2,
'md' => 2,
'lg' => 2,
]),
Forms\Components\ToggleButtons::make('status')
->label('Server State')->inline()->inlineLabel()
->helperText('')
->formatStateUsing(fn ($state) => $state ?? ServerState::Normal)
->options(fn ($state) => collect(ServerState::cases())->filter(fn ($serverState) => $serverState->value === $state)->mapWithKeys(
fn (ServerState $state) => [$state->value => str($state->value)->replace('_', ' ')->ucwords()]
))
->colors(collect(ServerState::cases())->mapWithKeys(
fn (ServerState $state) => [$state->value => $state->color()]
))
->icons(collect(ServerState::cases())->mapWithKeys(
fn (ServerState $state) => [$state->value => $state->icon()]
)) ))
->columnSpan([ ->columnSpan([
'default' => 1, 'default' => 1,

View File

@ -29,28 +29,11 @@ class ListServers extends ListRecords
Group::make('egg.name')->getDescriptionFromRecordUsing(fn (Server $server): string => str($server->egg->description)->limit(150)), Group::make('egg.name')->getDescriptionFromRecordUsing(fn (Server $server): string => str($server->egg->description)->limit(150)),
]) ])
->columns([ ->columns([
Tables\Columns\TextColumn::make('status') Tables\Columns\TextColumn::make('condition')
->default('unknown') ->default('unknown')
->badge() ->badge()
->default(fn (Server $server) => $server->status ?? $server->retrieveStatus()) ->icon(fn (Server $server) => $server->conditionIcon())
->icon(fn ($state) => match ($state) { ->color(fn (Server $server) => $server->conditionColor()),
'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 ($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')

View File

@ -3,10 +3,12 @@
namespace App\Models; namespace App\Models;
use App\Casts\EndpointCollection; use App\Casts\EndpointCollection;
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\Models\Objects\Endpoint; use App\Models\Objects\Endpoint;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
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;
@ -409,6 +411,35 @@ class Server extends Model
return cache()->get("servers.$this->uuid.container.status") ?? 'missing'; return cache()->get("servers.$this->uuid.container.status") ?? 'missing';
} }
public function condition(): Attribute
{
return Attribute::make(
get: fn () => $this->status?->value ?? $this->retrieveStatus(),
);
}
public function conditionIcon(): string
{
if ($this->status === null) {
$containerStatus = ContainerStatus::from($this->retrieveStatus());
return $containerStatus->icon();
}
return $this->status->icon();
}
public function conditionColor(): string
{
if ($this->status === null) {
$containerStatus = ContainerStatus::from($this->retrieveStatus());
return $containerStatus->color();
}
return $this->status->color();
}
public function getPrimaryEndpoint(): ?Endpoint public function getPrimaryEndpoint(): ?Endpoint
{ {
$endpoint = $this->ports->first(); $endpoint = $this->ports->first();