From 5fbcede5bbe6fa5b7d5779792072ef23323ceb43 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Mon, 12 Oct 2026 01:13:53 -0400 Subject: [PATCH] query builders --- app/Models/ActivityLog.php | 19 ----- app/Models/Allocation.php | 69 +----------------- app/Models/Builders/ActivityLogBuilder.php | 15 ++++ app/Models/Builders/AllocationBuilder.php | 78 ++++++++++++++++++++ app/Models/Builders/NodeBuilder.php | 84 ++++++++++++++++++++++ app/Models/Node.php | 76 ++------------------ app/Models/Objects/DeploymentObject.php | 2 +- 7 files changed, 185 insertions(+), 158 deletions(-) create mode 100644 app/Models/Builders/AllocationBuilder.php create mode 100644 app/Models/Builders/NodeBuilder.php diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php index 0dd9863e9..6f20f5e9d 100644 --- a/app/Models/ActivityLog.php +++ b/app/Models/ActivityLog.php @@ -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 $builder - */ - #[Scope] - protected function forEvent(Builder $builder, string $action): void - { - $builder->where('event', $action); - } - - /** - * @param Builder $builder - */ - #[Scope] - protected function forActor(Builder $builder, IlluminateModel $actor): void - { - $builder->whereMorphedTo('actor', $actor); - } } diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index 6265f658a..10ca0c6bb 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -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 */ @@ -79,68 +80,4 @@ class Allocation extends Model { return Attribute::make(get: fn (): bool => $this->ip_alias !== null); } - - /** - * @param Builder $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 $query - * @param array $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 $query - * @param list $ports - * @param list $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 $query - * @param array $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); - } } diff --git a/app/Models/Builders/ActivityLogBuilder.php b/app/Models/Builders/ActivityLogBuilder.php index 554f61332..81073dcc9 100644 --- a/app/Models/Builders/ActivityLogBuilder.php +++ b/app/Models/Builders/ActivityLogBuilder.php @@ -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); diff --git a/app/Models/Builders/AllocationBuilder.php b/app/Models/Builders/AllocationBuilder.php new file mode 100644 index 000000000..a41256917 --- /dev/null +++ b/app/Models/Builders/AllocationBuilder.php @@ -0,0 +1,78 @@ + + */ +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 $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 $ports + * @param list $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 $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; + } +} diff --git a/app/Models/Builders/NodeBuilder.php b/app/Models/Builders/NodeBuilder.php new file mode 100644 index 000000000..3999813ec --- /dev/null +++ b/app/Models/Builders/NodeBuilder.php @@ -0,0 +1,84 @@ + + */ +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 $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 $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 $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; + } +} diff --git a/app/Models/Node.php b/app/Models/Node.php index 6af75f01b..8c4bc8389 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -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 */ @@ -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 */ @@ -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 $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 $query - * @param list $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 $query - * @param array $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 $query - * @param array $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 { diff --git a/app/Models/Objects/DeploymentObject.php b/app/Models/Objects/DeploymentObject.php index 87e6650ab..327368160 100644 --- a/app/Models/Objects/DeploymentObject.php +++ b/app/Models/Objects/DeploymentObject.php @@ -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 */