Skip to content

Commit 7b57d65

Browse files
committed
Cleanup allocation repository
1 parent 9410a54 commit 7b57d65

File tree

4 files changed

+27
-79
lines changed

4 files changed

+27
-79
lines changed

app/Contracts/Repository/AllocationRepositoryInterface.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66

77
interface AllocationRepositoryInterface extends RepositoryInterface
88
{
9-
/**
10-
* Return all of the unique IPs that exist for a given node.
11-
*
12-
* @param int $node
13-
* @return \Illuminate\Support\Collection
14-
*/
15-
public function getUniqueAllocationIpsForNode(int $node): Collection;
16-
179
/**
1810
* Return all of the allocations that exist for a node that are not currently
1911
* allocated.
@@ -23,27 +15,6 @@ public function getUniqueAllocationIpsForNode(int $node): Collection;
2315
*/
2416
public function getUnassignedAllocationIds(int $node): array;
2517

26-
/**
27-
* Get an array of all allocations that are currently assigned to a given server.
28-
*
29-
* @param int $server
30-
* @return array
31-
*/
32-
public function getAssignedAllocationIds(int $server): array;
33-
34-
/**
35-
* Return a concatenated result set of node ips that already have at least one
36-
* server assigned to that IP. This allows for filtering out sets for
37-
* dedicated allocation IPs.
38-
*
39-
* If an array of nodes is passed the results will be limited to allocations
40-
* in those nodes.
41-
*
42-
* @param array $nodes
43-
* @return array
44-
*/
45-
public function getDiscardableDedicatedAllocations(array $nodes = []): array;
46-
4718
/**
4819
* Return a single allocation from those meeting the requirements.
4920
*

app/Http/Controllers/Admin/Nodes/NodeViewController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Pterodactyl\Models\Node;
77
use Illuminate\Support\Collection;
8+
use Pterodactyl\Models\Allocation;
89
use Illuminate\Contracts\View\Factory;
910
use Pterodactyl\Http\Controllers\Controller;
1011
use Pterodactyl\Repositories\Eloquent\NodeRepository;
@@ -134,7 +135,10 @@ public function allocations(Request $request, Node $node)
134135

135136
return $this->view->make('admin.nodes.view.allocation', [
136137
'node' => $node,
137-
'allocations' => $this->allocationRepository->setColumns(['ip'])->getUniqueAllocationIpsForNode($node->id),
138+
'allocations' => Allocation::query()->where('node_id', $node->id)
139+
->groupBy('ip')
140+
->orderByRaw('INET_ATON(ip) ASC')
141+
->get(['ip']),
138142
]);
139143
}
140144

app/Repositories/Eloquent/AllocationRepository.php

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Pterodactyl\Repositories\Eloquent;
44

5-
use Illuminate\Support\Collection;
65
use Pterodactyl\Models\Allocation;
76
use Illuminate\Database\Eloquent\Builder;
87
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
@@ -19,20 +18,6 @@ public function model()
1918
return Allocation::class;
2019
}
2120

22-
/**
23-
* Return all of the unique IPs that exist for a given node.
24-
*
25-
* @param int $node
26-
* @return \Illuminate\Support\Collection
27-
*/
28-
public function getUniqueAllocationIpsForNode(int $node): Collection
29-
{
30-
return $this->getBuilder()->where('node_id', $node)
31-
->groupBy('ip')
32-
->orderByRaw('INET_ATON(ip) ASC')
33-
->get($this->getColumns());
34-
}
35-
3621
/**
3722
* Return all of the allocations that exist for a node that are not currently
3823
* allocated.
@@ -42,22 +27,12 @@ public function getUniqueAllocationIpsForNode(int $node): Collection
4227
*/
4328
public function getUnassignedAllocationIds(int $node): array
4429
{
45-
$results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get();
46-
47-
return $results->pluck('id')->toArray();
48-
}
49-
50-
/**
51-
* Get an array of all allocations that are currently assigned to a given server.
52-
*
53-
* @param int $server
54-
* @return array
55-
*/
56-
public function getAssignedAllocationIds(int $server): array
57-
{
58-
$results = $this->getBuilder()->select('id')->where('server_id', $server)->get();
59-
60-
return $results->pluck('id')->toArray();
30+
return Allocation::query()->select('id')
31+
->whereNull('server_id')
32+
->where('node_id', $node)
33+
->get()
34+
->pluck('id')
35+
->toArray();
6136
}
6237

6338
/**
@@ -71,21 +46,19 @@ public function getAssignedAllocationIds(int $server): array
7146
* @param array $nodes
7247
* @return array
7348
*/
74-
public function getDiscardableDedicatedAllocations(array $nodes = []): array
49+
protected function getDiscardableDedicatedAllocations(array $nodes = []): array
7550
{
76-
$instance = $this->getBuilder()->select(
77-
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip) as result')
78-
);
51+
$query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result');
7952

8053
if (! empty($nodes)) {
81-
$instance->whereIn('node_id', $nodes);
54+
$query->whereIn('node_id', $nodes);
8255
}
8356

84-
$results = $instance->whereNotNull('server_id')
85-
->groupBy($this->getBuilder()->raw('CONCAT(node_id, ip)'))
86-
->get();
87-
88-
return $results->pluck('result')->toArray();
57+
return $query->whereNotNull('server_id')
58+
->groupByRaw('CONCAT(node_id, ip)')
59+
->get()
60+
->pluck('result')
61+
->toArray();
8962
}
9063

9164
/**
@@ -98,26 +71,26 @@ public function getDiscardableDedicatedAllocations(array $nodes = []): array
9871
*/
9972
public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false)
10073
{
101-
$instance = $this->getBuilder()->whereNull('server_id');
74+
$query = Allocation::query()->whereNull('server_id');
10275

10376
if (! empty($nodes)) {
104-
$instance->whereIn('node_id', $nodes);
77+
$query->whereIn('node_id', $nodes);
10578
}
10679

10780
if (! empty($ports)) {
108-
$instance->where(function (Builder $query) use ($ports) {
81+
$query->where(function (Builder $inner) use ($ports) {
10982
$whereIn = [];
11083
foreach ($ports as $port) {
11184
if (is_array($port)) {
112-
$query->orWhereBetween('port', $port);
85+
$inner->orWhereBetween('port', $port);
11386
continue;
11487
}
11588

11689
$whereIn[] = $port;
11790
}
11891

11992
if (! empty($whereIn)) {
120-
$query->orWhereIn('port', $whereIn);
93+
$inner->orWhereIn('port', $whereIn);
12194
}
12295
});
12396
}
@@ -128,12 +101,12 @@ public function getRandomAllocation(array $nodes, array $ports, bool $dedicated
128101
$discard = $this->getDiscardableDedicatedAllocations($nodes);
129102

130103
if (! empty($discard)) {
131-
$instance->whereNotIn(
104+
$query->whereNotIn(
132105
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), $discard
133106
);
134107
}
135108
}
136109

137-
return $instance->inRandomOrder()->first();
110+
return $query->inRandomOrder()->first();
138111
}
139112
}

app/Services/Servers/BuildModificationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private function processAllocations(Server $server, array &$data)
159159

160160
// Handle removal of allocations from this server.
161161
if (array_key_exists('remove_allocations', $data) && ! empty($data['remove_allocations'])) {
162-
$assigned = $this->allocationRepository->getAssignedAllocationIds($server->id);
162+
$assigned = $server->allocations->pluck('id')->toArray();
163163

164164
$updateIds = [];
165165
foreach ($data['remove_allocations'] as $allocation) {

0 commit comments

Comments
 (0)