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
|
|
|
/**
|
2019-09-06 03:33:27 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
2019-08-17 23:03:10 +00:00
|
|
|
*/
|
2021-03-21 19:29:18 +00:00
|
|
|
private DaemonServerRepository $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Cache\Repository
|
|
|
|
*/
|
|
|
|
private Repository $cache;
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceUtilizationController constructor.
|
|
|
|
*/
|
2021-03-21 19:29:18 +00:00
|
|
|
public function __construct(Repository $cache, DaemonServerRepository $repository)
|
2019-08-17 23:03:10 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2021-03-21 19:29:18 +00:00
|
|
|
$this->cache = $cache;
|
2019-08-17 23:03:10 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-03-21 19:29:18 +00:00
|
|
|
$stats = $this->cache
|
|
|
|
->tags(['resources'])
|
|
|
|
->remember($server->uuid, 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)
|
2018-03-17 19:01:53 +00:00
|
|
|
->transformWith($this->getTransformer(StatsTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
}
|