2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2018-01-05 04:49:50 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
|
|
|
|
2017-08-19 03:19:06 +00:00
|
|
|
class NodeRepository extends EloquentRepository implements NodeRepositoryInterface
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the model backing this repository.
|
|
|
|
*
|
|
|
|
* @return string
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Node::class;
|
|
|
|
}
|
2017-07-22 02:17:42 +00:00
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the usage stats for a single node.
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function getUsageStats(Node $node): array
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2019-11-24 23:08:54 +00:00
|
|
|
$stats = $this->getBuilder()
|
|
|
|
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
|
|
|
|
->join('servers', 'servers.node_id', '=', 'nodes.id')
|
|
|
|
->where('node_id', '=', $node->id)
|
|
|
|
->first();
|
2017-10-01 00:04:36 +00:00
|
|
|
|
2019-11-24 23:08:54 +00:00
|
|
|
return Collection::make(['disk' => $stats->sum_disk, 'memory' => $stats->sum_memory])
|
|
|
|
->mapWithKeys(function ($value, $key) use ($node) {
|
|
|
|
$maxUsage = $node->{$key};
|
|
|
|
if ($node->{$key . '_overallocate'} > 0) {
|
|
|
|
$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));
|
|
|
|
}
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2019-11-24 23:08:54 +00:00
|
|
|
$percent = ($value / $maxUsage) * 100;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2019-11-24 23:08:54 +00:00
|
|
|
return [
|
|
|
|
$key => [
|
|
|
|
'value' => number_format($value),
|
|
|
|
'max' => number_format($maxUsage),
|
|
|
|
'percent' => $percent,
|
|
|
|
'css' => ($percent <= self::THRESHOLD_PERCENTAGE_LOW) ? 'green' : (($percent > self::THRESHOLD_PERCENTAGE_MEDIUM) ? 'red' : 'yellow'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
})
|
|
|
|
->toArray();
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 22:16:13 +00:00
|
|
|
/**
|
|
|
|
* Return the usage stats for a single node.
|
|
|
|
*/
|
|
|
|
public function getUsageStatsRaw(Node $node): array
|
|
|
|
{
|
|
|
|
$stats = $this->getBuilder()->select(
|
|
|
|
$this->getBuilder()->raw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
|
|
|
|
)->join('servers', 'servers.node_id', '=', 'nodes.id')->where('node_id', $node->id)->first();
|
|
|
|
|
2021-10-30 20:41:38 +00:00
|
|
|
return collect([
|
|
|
|
'disk' => $stats->sum_disk,
|
|
|
|
'memory' => $stats->sum_memory,
|
|
|
|
])->mapWithKeys(function ($value, $key) use ($node) {
|
2018-05-26 22:16:13 +00:00
|
|
|
$maxUsage = $node->{$key};
|
|
|
|
if ($node->{$key . '_overallocate'} > 0) {
|
|
|
|
$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
$key => [
|
|
|
|
'value' => $value,
|
|
|
|
'max' => $maxUsage,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
})->toArray();
|
|
|
|
}
|
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return a single node with location and server information.
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function loadLocationAndServerCount(Node $node, bool $refresh = false): Node
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$node->relationLoaded('location') || $refresh) {
|
2018-01-05 04:49:50 +00:00
|
|
|
$node->load('location');
|
|
|
|
}
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
// This is quite ugly and can probably be improved down the road.
|
|
|
|
// And by probably, I mean it should.
|
2021-10-30 20:41:38 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2018-01-05 04:49:50 +00:00
|
|
|
if (is_null($node->servers_count) || $refresh) {
|
|
|
|
$node->load('servers');
|
|
|
|
$node->setRelation('servers_count', count($node->getRelation('servers')));
|
|
|
|
unset($node->servers);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
return $node;
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Attach a paginated set of allocations to a node mode including
|
|
|
|
* any servers that are also attached to those allocations.
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function loadNodeAllocations(Node $node, bool $refresh = false): Node
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
$node->setRelation(
|
|
|
|
'allocations',
|
2019-11-24 23:08:54 +00:00
|
|
|
$node->allocations()
|
|
|
|
->orderByRaw('server_id IS NOT NULL DESC, server_id IS NULL')
|
|
|
|
->orderByRaw('INET_ATON(ip) ASC')
|
|
|
|
->orderBy('port', 'asc')
|
|
|
|
->with('server:id,name')
|
|
|
|
->paginate(50)
|
2017-08-05 22:20:07 +00:00
|
|
|
);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
return $node;
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return a collection of nodes for all locations to use in server creation UI.
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function getNodesForServerCreation(): Collection
|
2017-07-22 02:17:42 +00:00
|
|
|
{
|
2021-10-30 20:41:38 +00:00
|
|
|
/** @phpstan-var \Illuminate\Database\Eloquent\Collection<\Pterodactyl\Models\Node> $collection */
|
|
|
|
$collection = $this->getBuilder()->with('allocations')->get();
|
|
|
|
|
|
|
|
return $collection->map(function (Node $item) {
|
|
|
|
/** @phpstan-var \Illuminate\Support\Collection<array{id: string, ip: string, port: string|int}> $filtered */
|
2018-01-05 04:49:50 +00:00
|
|
|
$filtered = $item->getRelation('allocations')->where('server_id', null)->map(function ($map) {
|
2017-07-22 02:17:42 +00:00
|
|
|
return collect($map)->only(['id', 'ip', 'port']);
|
|
|
|
});
|
|
|
|
|
2021-10-30 20:41:38 +00:00
|
|
|
$ports = $filtered->map(function ($map) {
|
2017-07-22 02:17:42 +00:00
|
|
|
return [
|
|
|
|
'id' => $map['id'],
|
|
|
|
'text' => sprintf('%s:%s', $map['ip'], $map['port']),
|
|
|
|
];
|
|
|
|
})->values();
|
|
|
|
|
2021-10-30 20:41:38 +00:00
|
|
|
$item->setAttribute('ports', $ports);
|
|
|
|
|
2017-07-22 02:17:42 +00:00
|
|
|
return [
|
|
|
|
'id' => $item->id,
|
|
|
|
'text' => $item->name,
|
2021-10-30 20:41:38 +00:00
|
|
|
'allocations' => $ports,
|
2017-07-22 02:17:42 +00:00
|
|
|
];
|
|
|
|
})->values();
|
|
|
|
}
|
2018-01-28 23:14:14 +00:00
|
|
|
|
2020-04-04 03:45:37 +00:00
|
|
|
/**
|
|
|
|
* Returns a node with the given id with the Node's resource usage.
|
|
|
|
*/
|
|
|
|
public function getNodeWithResourceUsage(int $node_id): Node
|
|
|
|
{
|
|
|
|
$instance = $this->getBuilder()
|
2021-10-30 20:41:38 +00:00
|
|
|
->select([
|
|
|
|
'nodes.id',
|
|
|
|
'nodes.fqdn',
|
|
|
|
'nodes.public_port_http',
|
|
|
|
'nodes.scheme',
|
|
|
|
'nodes.daemon_token',
|
|
|
|
'nodes.memory',
|
|
|
|
'nodes.disk',
|
|
|
|
'nodes.memory_overallocate',
|
|
|
|
'nodes.disk_overallocate',
|
|
|
|
])
|
2020-04-04 03:45:37 +00:00
|
|
|
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
|
|
|
|
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
|
|
|
|
->where('nodes.id', $node_id);
|
|
|
|
|
2021-10-30 20:41:38 +00:00
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */
|
2020-04-04 03:45:37 +00:00
|
|
|
return $instance->first();
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|