Update server listing and associated logic to pull from the panel dynamiacally

This commit is contained in:
Dane Everitt 2019-08-17 16:03:10 -07:00
parent 952dff854e
commit fb9c106448
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
26 changed files with 384 additions and 239 deletions

View file

@ -2,28 +2,10 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
use Illuminate\Support\Arr;
class StatsTransformer extends BaseClientTransformer
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
private $repository;
/**
* Perform dependency injection.
*
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $repository
*/
public function handle(ServerRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* @return string
*/
@ -36,60 +18,21 @@ class StatsTransformer extends BaseClientTransformer
* Transform stats from the daemon into a result set that can be used in
* the client API.
*
* @param \Pterodactyl\Models\Server $model
* @param array $data
* @return array
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function transform(Server $model)
public function transform(array $data)
{
try {
$stats = $this->repository->setServer($model)->details();
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);
}
$object = json_decode($stats->getBody()->getContents());
return [
'state' => $this->transformState(object_get($object, 'status', 0)),
'memory' => [
'current' => round(object_get($object, 'proc.memory.total', 0) / 1024 / 1024),
'limit' => floatval($model->memory),
'current_state' => Arr::get($data, 'state', 'stopped'),
'is_suspended' => Arr::get($data, 'suspended', false),
'resources' => [
'memory_bytes' => Arr::get($data, 'resources.memory_bytes', 0),
'cpu_absolute' => Arr::get($data, 'resources.cpu_absolute', 0),
'disk_bytes' => Arr::get($data, 'resources.disk_bytes', 0),
'network_rx_bytes' => Arr::get($data, 'resources.network.rx_bytes', 0),
'network_tx_bytes' => Arr::get($data, 'resources.network.tx_bytes', 0),
],
'cpu' => [
'current' => object_get($object, 'proc.cpu.total', 0),
'cores' => object_get($object, 'proc.cpu.cores', []),
'limit' => floatval($model->cpu),
],
'disk' => [
'current' => round(object_get($object, 'proc.disk.used', 0)),
'limit' => floatval($model->disk),
'io' => $model->io,
],
'installed' => $model->installed === 1,
'suspended' => (bool) $model->suspended,
];
}
/**
* Transform the state returned by the daemon into a human readable string.
*
* @param int $state
* @return string
*/
private function transformState(int $state): string
{
switch ($state) {
case 1:
return 'on';
case 2:
return 'starting';
case 3:
return 'stopping';
case 0:
default:
return 'off';
}
}
}