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), - ], - ]; - } -}