query builders

This commit is contained in:
robert dennis
2026-10-12 05:18:58 -04:00
parent e72b364d21
commit 5fbcede5bb
7 changed files with 185 additions and 158 deletions
-19
View File
@@ -6,7 +6,6 @@ namespace Pterodactyl\Models;
use Database\Factories\ActivityLogFactory;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;
use Illuminate\Database\Eloquent\Builder;
@@ -117,22 +116,4 @@ class ActivityLog extends Model implements Identifiable
Event::dispatch(new ActivityLogged($model));
});
}
/**
* @param Builder<static> $builder
*/
#[Scope]
protected function forEvent(Builder $builder, string $action): void
{
$builder->where('event', $action);
}
/**
* @param Builder<static> $builder
*/
#[Scope]
protected function forActor(Builder $builder, IlluminateModel $actor): void
{
$builder->whereMorphedTo('actor', $actor);
}
}
+3 -66
View File
@@ -6,17 +6,18 @@ namespace Pterodactyl\Models;
use Database\Factories\AllocationFactory;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Pterodactyl\Models\Builders\AllocationBuilder;
use Pterodactyl\Models\Traits\HasHashid;
/**
* @property-read Node $node
*/
#[Guarded(['id', 'created_at', 'updated_at'])]
#[UseEloquentBuilder(AllocationBuilder::class)]
class Allocation extends Model
{
/** @use HasFactory<AllocationFactory> */
@@ -79,68 +80,4 @@ class Allocation extends Model
{
return Attribute::make(get: fn (): bool => $this->ip_alias !== null);
}
/**
* @param Builder<self> $query
*/
#[Scope]
protected function unassigned(Builder $query): void
{
$query->whereNull('server_id');
}
/**
* Allocations on any of the given nodes; an empty list leaves the query unrestricted.
*
* @param Builder<self> $query
* @param array<int> $nodes
*/
#[Scope]
protected function onNodes(Builder $query, array $nodes): void
{
$query->when($nodes !== [], fn (Builder $q) => $q->whereIn('node_id', $nodes));
}
/**
* Allocations whose port is one of the given ports or falls inside one of the
* given [start, end] ranges; with neither given the query is unrestricted.
*
* @param Builder<self> $query
* @param list<int> $ports
* @param list<array{int, int}> $ranges
*/
#[Scope]
protected function onPorts(Builder $query, array $ports, array $ranges = []): void
{
if ($ports === [] && $ranges === []) {
return;
}
$query->where(function (Builder $inner) use ($ports, $ranges): void {
if ($ports !== []) {
$inner->orWhereIn('port', $ports);
}
foreach ($ranges as $range) {
$inner->orWhereBetween('port', $range);
}
});
}
/**
* Allocations on node/IP pairs that no server uses yet.
*
* @param Builder<self> $query
* @param array<int> $nodes restrict the occupied-IP lookup to these nodes
*/
#[Scope]
protected function onDedicatedIp(Builder $query, array $nodes = []): void
{
$occupied = self::query()
->selectRaw('CONCAT_WS("-", node_id, ip)')
->whereNotNull('server_id')
->onNodes($nodes);
$query->whereNotIn($query->raw('CONCAT_WS("-", node_id, ip)'), $occupied);
}
}
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Models\Builders;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Pterodactyl\Models\ActivityLog;
use Pterodactyl\Models\User;
@@ -15,6 +16,20 @@ use Pterodactyl\Models\User;
*/
class ActivityLogBuilder extends Builder
{
public function forEvent(string $action): static
{
$this->where('event', $action);
return $this;
}
public function forActor(Model $actor): static
{
$this->whereMorphedTo('actor', $actor);
return $this;
}
public function withoutDisabledEvents(): static
{
$this->whereNotIn('activity_logs.event', ActivityLog::DISABLED_EVENTS);
+78
View File
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Pterodactyl\Models\Builders;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Models\Allocation;
/**
* @template TModel of Allocation
*
* @extends Builder<TModel>
*/
class AllocationBuilder extends Builder
{
public function unassigned(): static
{
$this->whereNull('server_id');
return $this;
}
/**
* Allocations on any of the given nodes; an empty list leaves the query unrestricted.
*
* @param array<int> $nodes
*/
public function onNodes(array $nodes): static
{
$this->when($nodes !== [], fn (Builder $q) => $q->whereIn('node_id', $nodes));
return $this;
}
/**
* Allocations whose port is one of the given ports or falls inside one of the
* given [start, end] ranges; with neither given the query is unrestricted.
*
* @param list<int> $ports
* @param list<array{int, int}> $ranges
*/
public function onPorts(array $ports, array $ranges = []): static
{
if ($ports === [] && $ranges === []) {
return $this;
}
$this->where(function (Builder $inner) use ($ports, $ranges): void {
if ($ports !== []) {
$inner->orWhereIn('port', $ports);
}
foreach ($ranges as $range) {
$inner->orWhereBetween('port', $range);
}
});
return $this;
}
/**
* Allocations on node/IP pairs that no server uses yet.
*
* @param array<int> $nodes restrict the occupied-IP lookup to these nodes
*/
public function onDedicatedIp(array $nodes = []): static
{
$occupied = Allocation::query()
->selectRaw('CONCAT_WS("-", node_id, ip)')
->whereNotNull('server_id')
->onNodes($nodes);
$this->whereNotIn($this->raw('CONCAT_WS("-", node_id, ip)'), $occupied);
return $this;
}
}
+84
View File
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Pterodactyl\Models\Builders;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Models\Node;
/**
* @template TModel of Node
*
* @extends Builder<TModel>
*/
class NodeBuilder extends Builder
{
/**
* Public nodes with room for another server of this size, counting their overallocation.
* Selects the sum_memory and sum_disk that Node::allocatedResources() reads.
*/
public function viableFor(int $memory, int $disk): static
{
$this->select('nodes.*')
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory')
->selectRaw('IFNULL(SUM(servers.disk), 0) as sum_disk')
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.public', 1)
->groupBy('nodes.id')
->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [$memory])
->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$disk]);
return $this;
}
/**
* Nodes in any of the given locations; an empty list leaves the query unrestricted.
*
* @param list<int> $locations
*/
public function inLocations(array $locations): static
{
$this->when($locations !== [], fn (Builder $q) => $q->whereIn('nodes.location_id', $locations));
return $this;
}
/**
* Nodes that accept an egg with these tags: a node without egg tags accepts any egg,
* any other needs at least one tag in common with it.
*
* @param array<string> $eggTags
*/
public function forEggTags(array $eggTags): static
{
$this->where(function (Builder $outer) use ($eggTags): void {
$outer->whereDoesntHave('eggTags');
if ($eggTags !== []) {
$outer->orWhereHas('eggTags', fn (Builder $relation) => $relation->whereIn('tags.slug', $eggTags));
}
});
return $this;
}
/**
* Nodes whose deployment tags are exactly the deploy tags: a node reserved for a tag the
* deploy did not declare is left out, and every declared tag must be on the node. This
* is matched apart from the egg tags so that their OR never gets a deploy past a
* reservation.
*
* @param array<string> $deployTags
*/
public function forDeployTags(array $deployTags): static
{
$this->whereDoesntHave('deploymentTags', fn (Builder $relation) => $relation->whereNotIn('tags.slug', $deployTags));
foreach ($deployTags as $slug) {
$this->whereHas('deploymentTags', fn (Builder $relation) => $relation->where('tags.slug', $slug));
}
return $this;
}
}
+4 -72
View File
@@ -7,8 +7,7 @@ namespace Pterodactyl\Models;
use Database\Factories\NodeFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -19,6 +18,7 @@ use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;
use Pterodactyl\Contracts\Models\Identifiable;
use Pterodactyl\Models\Builders\NodeBuilder;
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
use Symfony\Component\Yaml\Yaml;
use UnexpectedValueException;
@@ -36,6 +36,7 @@ use UnexpectedValueException;
'maintenance_mode',
])]
#[Hidden(['daemon_token_id', 'daemon_token'])]
#[UseEloquentBuilder(NodeBuilder::class)]
class Node extends Model implements Identifiable
{
/** @use HasFactory<NodeFactory> */
@@ -228,7 +229,7 @@ class Node extends Model implements Identifiable
/**
* The reservations on this node: a deploy lands here only when its own deploy
* tags match these exactly (see forDeployTags()).
* tags match these exactly (see NodeBuilder::forDeployTags()).
*
* @return MorphToMany<Tag, $this>
*/
@@ -289,75 +290,6 @@ class Node extends Model implements Identifiable
return ['memory' => $memory, 'disk' => $disk];
}
/**
* Public nodes with room for another server of this size, counting their overallocation.
* Selects the sum_memory and sum_disk that allocatedResources() reads.
*
* @param Builder<self> $query
*/
#[Scope]
protected function viableFor(Builder $query, int $memory, int $disk): void
{
$query->select('nodes.*')
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory')
->selectRaw('IFNULL(SUM(servers.disk), 0) as sum_disk')
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.public', 1)
->groupBy('nodes.id')
->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [$memory])
->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$disk]);
}
/**
* Nodes in any of the given locations; an empty list leaves the query unrestricted.
*
* @param Builder<self> $query
* @param list<int> $locations
*/
#[Scope]
protected function inLocations(Builder $query, array $locations): void
{
$query->when($locations !== [], fn (Builder $q) => $q->whereIn('nodes.location_id', $locations));
}
/**
* Nodes that accept an egg with these tags: a node without egg tags accepts any egg,
* any other needs at least one tag in common with it.
*
* @param Builder<self> $query
* @param array<string> $eggTags
*/
#[Scope]
protected function forEggTags(Builder $query, array $eggTags): void
{
$query->where(function (Builder $outer) use ($eggTags): void {
$outer->whereDoesntHave('eggTags');
if ($eggTags !== []) {
$outer->orWhereHas('eggTags', fn (Builder $relation) => $relation->whereIn('tags.slug', $eggTags));
}
});
}
/**
* Nodes whose deployment tags are exactly the deploy tags: a node reserved for a tag the
* deploy did not declare is left out, and every declared tag must be on the node. This
* is matched apart from the egg tags so that their OR never gets a deploy past a
* reservation.
*
* @param Builder<self> $query
* @param array<string> $deployTags
*/
#[Scope]
protected function forDeployTags(Builder $query, array $deployTags): void
{
$query->whereDoesntHave('deploymentTags', fn (Builder $relation) => $relation->whereNotIn('tags.slug', $deployTags));
foreach ($deployTags as $slug) {
$query->whereHas('deploymentTags', fn (Builder $relation) => $relation->where('tags.slug', $slug));
}
}
/** @return array{value: string, max: string, percent: float|int, css: string} */
private function utilizationOf(int $used, int $capacity, int $overallocate): array
{
+1 -1
View File
@@ -20,7 +20,7 @@ class DeploymentObject
/**
* The deploy tags this deployment declares. A node's reservations are matched
* against these exactly (see Node::forDeployTags()).
* against these exactly (see NodeBuilder::forDeployTags()).
*
* @var array<string>
*/