Add support for node management actions using new services
This commit is contained in:
parent
4391defb9f
commit
c1a078bdcf
33 changed files with 1375 additions and 745 deletions
|
@ -105,6 +105,14 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
|
|||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}.
|
||||
*/
|
||||
public function findCountWhere(array $fields)
|
||||
{
|
||||
return $this->getBuilder()->where($fields)->count($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -28,8 +28,9 @@ use Pterodactyl\Models\Location;
|
|||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
||||
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
|
||||
|
||||
class LocationRepository extends EloquentRepository implements LocationRepositoryInterface
|
||||
class LocationRepository extends SearchableRepository implements LocationRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -44,21 +45,6 @@ class LocationRepository extends EloquentRepository implements LocationRepositor
|
|||
return Location::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function search($term)
|
||||
{
|
||||
if (empty($term)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$clone = clone $this;
|
||||
$clone->searchTerm = $term;
|
||||
|
||||
return $clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
namespace Pterodactyl\Repositories\Eloquent;
|
||||
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
|
||||
|
||||
|
@ -38,6 +39,104 @@ class NodeRepository extends SearchableRepository implements NodeRepositoryInter
|
|||
return Node::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUsageStats($id)
|
||||
{
|
||||
$node = $this->getBuilder()->select(
|
||||
'nodes.disk_overallocate', 'nodes.memory_overallocate', 'nodes.disk', 'nodes.memory',
|
||||
$this->getBuilder()->raw('SUM(servers.memory) as sum_memory, SUM(servers.disk) as sum_disk')
|
||||
)->join('servers', 'servers.node_id', '=', 'nodes.id')
|
||||
->where('nodes.id', $id)
|
||||
->first();
|
||||
|
||||
return collect(['disk' => $node->sum_disk, 'memory' => $node->sum_memory])
|
||||
->mapWithKeys(function ($value, $key) use ($node) {
|
||||
$maxUsage = $node->{$key};
|
||||
if ($node->{$key . '_overallocate'} > 0) {
|
||||
$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));
|
||||
}
|
||||
|
||||
$percent = ($value / $maxUsage) * 100;
|
||||
|
||||
return [
|
||||
$key => [
|
||||
'value' => number_format($value),
|
||||
'max' => number_format($maxUsage),
|
||||
'percent' => $percent,
|
||||
'css' => ($percent <= 75) ? 'green' : (($percent > 90) ? 'red' : 'yellow'),
|
||||
],
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeListingData($count = 25)
|
||||
{
|
||||
$instance = $this->getBuilder()->with('location')->withCount('servers');
|
||||
|
||||
if ($this->searchTerm) {
|
||||
$instance->search($this->searchTerm);
|
||||
}
|
||||
|
||||
return $instance->paginate($count, $this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSingleNode($id)
|
||||
{
|
||||
$instance = $this->getBuilder()->with('location')->withCount('servers')->find($id, $this->getColumns());
|
||||
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeAllocations($id)
|
||||
{
|
||||
$instance = $this->getBuilder()->find($id, $this->getColumns());
|
||||
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
$instance->setRelation(
|
||||
'allocations',
|
||||
$this->getModel()->allocations()->orderBy('ip', 'asc')
|
||||
->orderBy('port', 'asc')
|
||||
->with('server')
|
||||
->paginate(50)
|
||||
);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeServers($id)
|
||||
{
|
||||
$instance = $this->getBuilder()->with('servers.user', 'servers.service', 'servers.option')
|
||||
->find($id, $this->getColumns());
|
||||
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
Reference in a new issue