2018-03-17 19:01:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2021-03-21 19:29:18 +00:00
|
|
|
use Carbon\Carbon;
|
2018-03-17 19:01:53 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2021-03-21 19:29:18 +00:00
|
|
|
use Illuminate\Cache\Repository;
|
2018-03-17 19:01:53 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\StatsTransformer;
|
2019-09-06 03:33:27 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2018-03-17 19:01:53 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest;
|
|
|
|
|
|
|
|
class ResourceUtilizationController extends ClientApiController
|
|
|
|
{
|
2019-08-17 23:03:10 +00:00
|
|
|
/**
|
|
|
|
* ResourceUtilizationController constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private Repository $cache, private DaemonServerRepository $repository)
|
2019-08-17 23:03:10 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2018-03-17 19:01:53 +00:00
|
|
|
/**
|
2021-03-21 19:29:18 +00:00
|
|
|
* 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.
|
2018-03-17 19:01:53 +00:00
|
|
|
*
|
2019-08-17 23:03:10 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2018-03-17 19:01:53 +00:00
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function __invoke(GetServerRequest $request, Server $server): array
|
2018-03-17 19:01:53 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
$key = "resources:$server->uuid";
|
2021-04-03 17:53:41 +00:00
|
|
|
$stats = $this->cache->remember($key, Carbon::now()->addSeconds(20), function () use ($server) {
|
|
|
|
return $this->repository->setServer($server)->getDetails();
|
|
|
|
});
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($stats)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(StatsTransformer::class)
|
2018-03-17 19:01:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
}
|