From 7b57d65edff41566e95af5c7339f99ed93a03e60 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 13 Sep 2020 12:47:05 -0700 Subject: [PATCH] Cleanup allocation repository --- .../AllocationRepositoryInterface.php | 29 -------- .../Admin/Nodes/NodeViewController.php | 6 +- .../Eloquent/AllocationRepository.php | 69 ++++++------------- .../Servers/BuildModificationService.php | 2 +- 4 files changed, 27 insertions(+), 79 deletions(-) diff --git a/app/Contracts/Repository/AllocationRepositoryInterface.php b/app/Contracts/Repository/AllocationRepositoryInterface.php index b02ef66a4..400279733 100644 --- a/app/Contracts/Repository/AllocationRepositoryInterface.php +++ b/app/Contracts/Repository/AllocationRepositoryInterface.php @@ -6,14 +6,6 @@ use Illuminate\Support\Collection; interface AllocationRepositoryInterface extends RepositoryInterface { - /** - * Return all of the unique IPs that exist for a given node. - * - * @param int $node - * @return \Illuminate\Support\Collection - */ - public function getUniqueAllocationIpsForNode(int $node): Collection; - /** * Return all of the allocations that exist for a node that are not currently * allocated. @@ -23,27 +15,6 @@ interface AllocationRepositoryInterface extends RepositoryInterface */ public function getUnassignedAllocationIds(int $node): array; - /** - * Get an array of all allocations that are currently assigned to a given server. - * - * @param int $server - * @return array - */ - public function getAssignedAllocationIds(int $server): array; - - /** - * Return a concatenated result set of node ips that already have at least one - * server assigned to that IP. This allows for filtering out sets for - * dedicated allocation IPs. - * - * If an array of nodes is passed the results will be limited to allocations - * in those nodes. - * - * @param array $nodes - * @return array - */ - public function getDiscardableDedicatedAllocations(array $nodes = []): array; - /** * Return a single allocation from those meeting the requirements. * diff --git a/app/Http/Controllers/Admin/Nodes/NodeViewController.php b/app/Http/Controllers/Admin/Nodes/NodeViewController.php index ba9e2e947..2121985c9 100644 --- a/app/Http/Controllers/Admin/Nodes/NodeViewController.php +++ b/app/Http/Controllers/Admin/Nodes/NodeViewController.php @@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Admin\Nodes; use Illuminate\Http\Request; use Pterodactyl\Models\Node; use Illuminate\Support\Collection; +use Pterodactyl\Models\Allocation; use Illuminate\Contracts\View\Factory; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Repositories\Eloquent\NodeRepository; @@ -134,7 +135,10 @@ class NodeViewController extends Controller return $this->view->make('admin.nodes.view.allocation', [ 'node' => $node, - 'allocations' => $this->allocationRepository->setColumns(['ip'])->getUniqueAllocationIpsForNode($node->id), + 'allocations' => Allocation::query()->where('node_id', $node->id) + ->groupBy('ip') + ->orderByRaw('INET_ATON(ip) ASC') + ->get(['ip']), ]); } diff --git a/app/Repositories/Eloquent/AllocationRepository.php b/app/Repositories/Eloquent/AllocationRepository.php index cc7efb23b..15a5db81b 100644 --- a/app/Repositories/Eloquent/AllocationRepository.php +++ b/app/Repositories/Eloquent/AllocationRepository.php @@ -2,7 +2,6 @@ namespace Pterodactyl\Repositories\Eloquent; -use Illuminate\Support\Collection; use Pterodactyl\Models\Allocation; use Illuminate\Database\Eloquent\Builder; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; @@ -19,20 +18,6 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos return Allocation::class; } - /** - * Return all of the unique IPs that exist for a given node. - * - * @param int $node - * @return \Illuminate\Support\Collection - */ - public function getUniqueAllocationIpsForNode(int $node): Collection - { - return $this->getBuilder()->where('node_id', $node) - ->groupBy('ip') - ->orderByRaw('INET_ATON(ip) ASC') - ->get($this->getColumns()); - } - /** * Return all of the allocations that exist for a node that are not currently * allocated. @@ -42,22 +27,12 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos */ public function getUnassignedAllocationIds(int $node): array { - $results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get(); - - return $results->pluck('id')->toArray(); - } - - /** - * Get an array of all allocations that are currently assigned to a given server. - * - * @param int $server - * @return array - */ - public function getAssignedAllocationIds(int $server): array - { - $results = $this->getBuilder()->select('id')->where('server_id', $server)->get(); - - return $results->pluck('id')->toArray(); + return Allocation::query()->select('id') + ->whereNull('server_id') + ->where('node_id', $node) + ->get() + ->pluck('id') + ->toArray(); } /** @@ -71,21 +46,19 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos * @param array $nodes * @return array */ - public function getDiscardableDedicatedAllocations(array $nodes = []): array + protected function getDiscardableDedicatedAllocations(array $nodes = []): array { - $instance = $this->getBuilder()->select( - $this->getBuilder()->raw('CONCAT_WS("-", node_id, ip) as result') - ); + $query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result'); if (! empty($nodes)) { - $instance->whereIn('node_id', $nodes); + $query->whereIn('node_id', $nodes); } - $results = $instance->whereNotNull('server_id') - ->groupBy($this->getBuilder()->raw('CONCAT(node_id, ip)')) - ->get(); - - return $results->pluck('result')->toArray(); + return $query->whereNotNull('server_id') + ->groupByRaw('CONCAT(node_id, ip)') + ->get() + ->pluck('result') + ->toArray(); } /** @@ -98,18 +71,18 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos */ public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false) { - $instance = $this->getBuilder()->whereNull('server_id'); + $query = Allocation::query()->whereNull('server_id'); if (! empty($nodes)) { - $instance->whereIn('node_id', $nodes); + $query->whereIn('node_id', $nodes); } if (! empty($ports)) { - $instance->where(function (Builder $query) use ($ports) { + $query->where(function (Builder $inner) use ($ports) { $whereIn = []; foreach ($ports as $port) { if (is_array($port)) { - $query->orWhereBetween('port', $port); + $inner->orWhereBetween('port', $port); continue; } @@ -117,7 +90,7 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos } if (! empty($whereIn)) { - $query->orWhereIn('port', $whereIn); + $inner->orWhereIn('port', $whereIn); } }); } @@ -128,12 +101,12 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos $discard = $this->getDiscardableDedicatedAllocations($nodes); if (! empty($discard)) { - $instance->whereNotIn( + $query->whereNotIn( $this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), $discard ); } } - return $instance->inRandomOrder()->first(); + return $query->inRandomOrder()->first(); } } diff --git a/app/Services/Servers/BuildModificationService.php b/app/Services/Servers/BuildModificationService.php index b6768fdb1..86ff5bb69 100644 --- a/app/Services/Servers/BuildModificationService.php +++ b/app/Services/Servers/BuildModificationService.php @@ -159,7 +159,7 @@ class BuildModificationService // Handle removal of allocations from this server. if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) { - $assigned = $this->allocationRepository->getAssignedAllocationIds($server->id); + $assigned = $server->allocations->pluck('id')->toArray(); $updateIds = []; foreach ($data['remove_allocations'] as $allocation) {