Compare commits

...

2 commits

Author SHA1 Message Date
Lance Pioch
6ffb7fd7f6 Show warning for a server's allocations if it's currently running 2022-11-07 22:12:24 -05:00
Lance Pioch
87a2fff3b7 Replace stats transformer with resource 2022-11-07 22:11:45 -05:00
7 changed files with 59 additions and 54 deletions

View file

@ -61,8 +61,15 @@ class ServerViewController extends Controller
{
$allocations = $server->node->allocations->toBase();
try {
$stats = $server->getStats();
} catch (\Exception) {
// Can't connect to daemon
}
return $this->view->make('admin.servers.view.build', [
'server' => $server,
'currentState' => $stats['current_state'] ?? null,
'assigned' => $allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
'unassigned' => $allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
]);

View file

@ -2,40 +2,20 @@
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Carbon\Carbon;
use Pterodactyl\Models\Server;
use Illuminate\Cache\Repository;
use Pterodactyl\Transformers\Api\Client\StatsTransformer;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest;
class ResourceUtilizationController extends ClientApiController
{
/**
* ResourceUtilizationController constructor.
*/
public function __construct(private Repository $cache, private DaemonServerRepository $repository)
{
parent::__construct();
}
/**
* Return the current resource utilization for a server. This value is cached for up to
* 20 seconds at a time to ensure that repeated requests to this endpoint do not cause
* a flood of unnecessary API calls.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function __invoke(GetServerRequest $request, Server $server): array
{
$key = "resources:$server->uuid";
$stats = $this->cache->remember($key, Carbon::now()->addSeconds(20), function () use ($server) {
return $this->repository->setServer($server)->getDetails();
});
return $this->fractal->item($stats)
->transformWith($this->getTransformer(StatsTransformer::class))
->toArray();
return $server->getStats();
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Pterodactyl\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class StatsResource extends JsonResource
{
public bool $preserveKeys = true;
public function toArray($request): array
{
return [
'current_state' => $this['state'] ?? 'stopped',
'is_suspended' => $this['is_suspended'] ?? false,
'resources' => [
'memory_bytes' => $this['utilization.memory_bytes'] ?? 0,
'cpu_absolute' => $this['utilization.cpu_absolute'] ?? 0,
'disk_bytes' => $this['utilization.disk_bytes'] ?? 0,
'network_rx_bytes' => $this['utilization.network.rx_bytes'] ?? 0,
'network_tx_bytes' => $this['utilization.network.tx_bytes'] ?? 0,
'uptime' => $this['utilization.uptime'] ?? 0,
],
];
}
}

View file

@ -5,11 +5,13 @@ namespace Pterodactyl\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Query\JoinClause;
use Znck\Eloquent\Traits\BelongsToThrough;
use Pterodactyl\Http\Resources\StatsResource;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
/**
@ -379,4 +381,17 @@ class Server extends Model
throw new ServerStateConflictException($this);
}
}
public function getStats(): array
{
$resourceService = app()->make(DaemonServerRepository::class);
$stats = cache()->remember(
"resources:$this->uuid",
now()->addSeconds(20),
fn () => $resourceService->setServer($this)->getDetails()
);
return (new StatsResource($stats))->resolve();
}
}

View file

@ -49,6 +49,8 @@ class AppServiceProvider extends ServiceProvider
'task' => Models\Task::class,
'user' => Models\User::class,
]);
\Pterodactyl\Http\Resources\StatsResource::withoutWrapping();
}
/**

View file

@ -1,33 +0,0 @@
<?php
namespace Pterodactyl\Transformers\Api\Client;
use Illuminate\Support\Arr;
class StatsTransformer extends BaseClientTransformer
{
public function getResourceName(): string
{
return 'stats';
}
/**
* Transform stats from the daemon into a result set that can be used in
* the client API.
*/
public function transform(array $data): array
{
return [
'current_state' => Arr::get($data, 'state', 'stopped'),
'is_suspended' => Arr::get($data, 'is_suspended', false),
'resources' => [
'memory_bytes' => Arr::get($data, 'utilization.memory_bytes', 0),
'cpu_absolute' => Arr::get($data, 'utilization.cpu_absolute', 0),
'disk_bytes' => Arr::get($data, 'utilization.disk_bytes', 0),
'network_rx_bytes' => Arr::get($data, 'utilization.network.rx_bytes', 0),
'network_tx_bytes' => Arr::get($data, 'utilization.network.tx_bytes', 0),
'uptime' => Arr::get($data, 'utilization.uptime', 0),
],
];
}
}

View file

@ -129,6 +129,14 @@
<h3 class="box-title">Allocation Management</h3>
</div>
<div class="box-body">
@if ($currentState === 'running')
<div class="alert alert-warning">
Warning, your server is currently running.
If you remove allocations without restarting,
then you can run into issues using them elsewhere.
</div>
@endif
<div class="form-group">
<label for="pAllocation" class="control-label">Game Port</label>
<select id="pAllocation" name="allocation_id" class="form-control">