Replace stats transformer with resource
This commit is contained in:
parent
032e4f2e31
commit
87a2fff3b7
5 changed files with 44 additions and 54 deletions
|
@ -2,40 +2,20 @@
|
||||||
|
|
||||||
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use Pterodactyl\Models\Server;
|
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\Controllers\Api\Client\ClientApiController;
|
||||||
use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest;
|
use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest;
|
||||||
|
|
||||||
class ResourceUtilizationController extends ClientApiController
|
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
|
* 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
|
* 20 seconds at a time to ensure that repeated requests to this endpoint do not cause
|
||||||
* a flood of unnecessary API calls.
|
* a flood of unnecessary API calls.
|
||||||
*
|
*
|
||||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
||||||
*/
|
*/
|
||||||
public function __invoke(GetServerRequest $request, Server $server): array
|
public function __invoke(GetServerRequest $request, Server $server): array
|
||||||
{
|
{
|
||||||
$key = "resources:$server->uuid";
|
return $server->getStats();
|
||||||
$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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
26
app/Http/Resources/StatsResource.php
Normal file
26
app/Http/Resources/StatsResource.php
Normal 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,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,11 +5,13 @@ namespace Pterodactyl\Models;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Illuminate\Database\Query\JoinClause;
|
use Illuminate\Database\Query\JoinClause;
|
||||||
use Znck\Eloquent\Traits\BelongsToThrough;
|
use Znck\Eloquent\Traits\BelongsToThrough;
|
||||||
|
use Pterodactyl\Http\Resources\StatsResource;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||||
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
||||||
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
|
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -379,4 +381,17 @@ class Server extends Model
|
||||||
throw new ServerStateConflictException($this);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,8 @@ class AppServiceProvider extends ServiceProvider
|
||||||
'task' => Models\Task::class,
|
'task' => Models\Task::class,
|
||||||
'user' => Models\User::class,
|
'user' => Models\User::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
\Pterodactyl\Http\Resources\StatsResource::withoutWrapping();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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),
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue