Path: blob/1.0-develop/app/Repositories/Eloquent/NodeRepository.php
7460 views
<?php12namespace Pterodactyl\Repositories\Eloquent;34use Pterodactyl\Models\Node;5use Illuminate\Support\Collection;6use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;78class NodeRepository extends EloquentRepository implements NodeRepositoryInterface9{10/**11* Return the model backing this repository.12*/13public function model(): string14{15return Node::class;16}1718/**19* Return the usage stats for a single node.20*/21public function getUsageStats(Node $node): array22{23$stats = $this->getBuilder()24->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')25->join('servers', 'servers.node_id', '=', 'nodes.id')26->where('node_id', '=', $node->id)27->first();2829return Collection::make(['disk' => $stats->sum_disk, 'memory' => $stats->sum_memory])30->mapWithKeys(function ($value, $key) use ($node) {31$maxUsage = $node->{$key};32if ($node->{$key . '_overallocate'} > 0) {33$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));34}3536$percent = ($value / $maxUsage) * 100;3738return [39$key => [40'value' => number_format($value),41'max' => number_format($maxUsage),42'percent' => $percent,43'css' => ($percent <= self::THRESHOLD_PERCENTAGE_LOW) ? 'green' : (($percent > self::THRESHOLD_PERCENTAGE_MEDIUM) ? 'red' : 'yellow'),44],45];46})47->toArray();48}4950/**51* Return the usage stats for a single node.52*/53public function getUsageStatsRaw(Node $node): array54{55$stats = $this->getBuilder()->select(56$this->getBuilder()->raw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')57)->join('servers', 'servers.node_id', '=', 'nodes.id')->where('node_id', $node->id)->first();5859return collect(['disk' => $stats->sum_disk, 'memory' => $stats->sum_memory])->mapWithKeys(function ($value, $key) use ($node) {60$maxUsage = $node->{$key};61if ($node->{$key . '_overallocate'} > 0) {62$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));63}6465return [66$key => [67'value' => $value,68'max' => $maxUsage,69],70];71})->toArray();72}7374/**75* Return a single node with location and server information.76*/77public function loadLocationAndServerCount(Node $node, bool $refresh = false): Node78{79if (!$node->relationLoaded('location') || $refresh) {80$node->load('location');81}8283// This is quite ugly and can probably be improved down the road.84// And by probably, I mean it should.85if (is_null($node->servers_count) || $refresh) {86$node->load('servers');87$node->setRelation('servers_count', count($node->getRelation('servers')));88unset($node->servers);89}9091return $node;92}9394/**95* Attach a paginated set of allocations to a node mode including96* any servers that are also attached to those allocations.97*/98public function loadNodeAllocations(Node $node, bool $refresh = false): Node99{100$node->setRelation(101'allocations',102$node->allocations()103->orderByRaw('server_id IS NOT NULL DESC, server_id IS NULL')104->orderByRaw('INET_ATON(ip) ASC')105->orderBy('port')106->with('server:id,name')107->paginate(50)108);109110return $node;111}112113/**114* Return a collection of nodes for all locations to use in server creation UI.115*/116public function getNodesForServerCreation(): Collection117{118return $this->getBuilder()->with('allocations')->get()->map(function (Node $item) {119$filtered = $item->getRelation('allocations')->where('server_id', null)->map(function ($map) {120return collect($map)->only(['id', 'ip', 'port']);121});122123$item->ports = $filtered->map(function ($map) {124return [125'id' => $map['id'],126'text' => sprintf('%s:%s', $map['ip'], $map['port']),127];128})->values();129130return [131'id' => $item->id,132'text' => $item->name,133'allocations' => $item->ports,134];135})->values();136}137138/**139* Returns a node with the given id with the Node's resource usage.140*/141public function getNodeWithResourceUsage(int $node_id): Node142{143$instance = $this->getBuilder()144->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemonListen', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate'])145->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')146->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')147->where('nodes.id', $node_id);148149return $instance->first();150}151}152153154