2018-03-17 19:01:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2018-06-03 02:08:53 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2018-03-17 19:01:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return 'stats';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform stats from the daemon into a result set that can be used in
|
|
|
|
* the client API.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $model
|
|
|
|
* @return array
|
2018-06-03 02:08:53 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2018-03-17 19:01:53 +00:00
|
|
|
*/
|
|
|
|
public function transform(Server $model)
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
],
|
|
|
|
'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),
|
2018-06-03 02:08:53 +00:00
|
|
|
'io' => $model->io,
|
2018-03-17 19:01:53 +00:00
|
|
|
],
|
2018-06-03 02:08:53 +00:00
|
|
|
'installed' => $model->installed === 1,
|
|
|
|
'suspended' => (bool) $model->suspended,
|
2018-03-17 19:01:53 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|