From 87a2fff3b7fd151f321f8b9f0f9c9df8fcde1ad3 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Mon, 7 Nov 2022 22:11:45 -0500 Subject: [PATCH] Replace stats transformer with resource --- .../Servers/ResourceUtilizationController.php | 22 +------------ app/Http/Resources/StatsResource.php | 26 +++++++++++++++ app/Models/Server.php | 15 +++++++++ app/Providers/AppServiceProvider.php | 2 ++ .../Api/Client/StatsTransformer.php | 33 ------------------- 5 files changed, 44 insertions(+), 54 deletions(-) create mode 100644 app/Http/Resources/StatsResource.php delete mode 100644 app/Transformers/Api/Client/StatsTransformer.php diff --git a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php index dcaf48115..77ddf6171 100644 --- a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php +++ b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php @@ -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(); } } diff --git a/app/Http/Resources/StatsResource.php b/app/Http/Resources/StatsResource.php new file mode 100644 index 000000000..f4ba1e35a --- /dev/null +++ b/app/Http/Resources/StatsResource.php @@ -0,0 +1,26 @@ + $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, + ], + ]; + } +} diff --git a/app/Models/Server.php b/app/Models/Server.php index d7cc649c0..bcb9dfdbb 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -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(); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d4ffdadbb..29c001eb2 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -49,6 +49,8 @@ class AppServiceProvider extends ServiceProvider 'task' => Models\Task::class, 'user' => Models\User::class, ]); + + \Pterodactyl\Http\Resources\StatsResource::withoutWrapping(); } /** diff --git a/app/Transformers/Api/Client/StatsTransformer.php b/app/Transformers/Api/Client/StatsTransformer.php deleted file mode 100644 index 6b323b315..000000000 --- a/app/Transformers/Api/Client/StatsTransformer.php +++ /dev/null @@ -1,33 +0,0 @@ - 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), - ], - ]; - } -}