diff --git a/app/Classes/Pterodactyl.php b/app/Classes/Pterodactyl.php index a7bc9bd5..b0b93373 100644 --- a/app/Classes/Pterodactyl.php +++ b/app/Classes/Pterodactyl.php @@ -14,11 +14,6 @@ use Illuminate\Support\Facades\Http; class Pterodactyl { - /** - * @description per_page option to pull more than the default 50 from pterodactyl - */ - public const PER_PAGE = 200; - //TODO: Extend error handling (maybe logger for more errors when debugging) /** @@ -73,7 +68,7 @@ class Pterodactyl public static function getEggs(Nest $nest) { try { - $response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . self::PER_PAGE); + $response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . config("SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT")); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -88,7 +83,7 @@ class Pterodactyl public static function getNodes() { try { - $response = self::client()->get('/application/nodes?per_page=' . self::PER_PAGE); + $response = self::client()->get('/application/nodes?per_page=' . config("SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT")); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -115,7 +110,7 @@ class Pterodactyl public static function getServers() { try { - $response = self::client()->get('/application/servers'); + $response = self::client()->get('/application/servers?per_page=' . config("SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT")); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -130,7 +125,7 @@ class Pterodactyl public static function getNests() { try { - $response = self::client()->get('/application/nests?per_page=' . self::PER_PAGE); + $response = self::client()->get('/application/nests?per_page=' . config("SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT")); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -145,7 +140,7 @@ class Pterodactyl public static function getLocations() { try { - $response = self::client()->get('/application/locations?per_page=' . self::PER_PAGE); + $response = self::client()->get('/application/locations?per_page=' . config("SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT")); } catch (Exception $e) { throw self::getException($e->getMessage()); } diff --git a/app/Classes/Settings/System.php b/app/Classes/Settings/System.php index 63266ad4..56e9d480 100644 --- a/app/Classes/Settings/System.php +++ b/app/Classes/Settings/System.php @@ -42,6 +42,7 @@ public function checkPteroClientkey(){ "server-limit-purchase" => "required|min:0|integer", "pterodactyl-api-key" => "required|string", "pterodactyl-url" => "required|string", + "per-page-limit" => "required|min:0|integer", "pterodactyl-admin-api-key" => "required|string", ]); @@ -70,6 +71,7 @@ public function checkPteroClientkey(){ "SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE" => "server-limit-purchase", "SETTINGS::MISC:PHPMYADMIN:URL" => "phpmyadmin-url", "SETTINGS::SYSTEM:PTERODACTYL:URL" => "pterodactyl-url", + 'SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT' => "per-page-limit", "SETTINGS::SYSTEM:PTERODACTYL:TOKEN" => "pterodactyl-api-key", "SETTINGS::SYSTEM:ENABLE_LOGIN_LOGO" => "enable-login-logo", "SETTINGS::SYSTEM:PTERODACTYL:ADMIN_USER_TOKEN" => "pterodactyl-admin-api-key", diff --git a/app/Http/Controllers/Admin/OverViewController.php b/app/Http/Controllers/Admin/OverViewController.php index 29049b8f..19608455 100644 --- a/app/Http/Controllers/Admin/OverViewController.php +++ b/app/Http/Controllers/Admin/OverViewController.php @@ -11,6 +11,8 @@ use App\Models\Payment; use App\Models\Server; use App\Models\User; use Illuminate\Support\Facades\Cache; +use App\Classes\Pterodactyl; +use App\Models\Product; class OverViewController extends Controller { @@ -18,38 +20,73 @@ class OverViewController extends Controller public function index() { - $userCount = Cache::remember('user:count', self::TTL, function () { - return User::query()->count(); - }); + $counters = Cache::remember('counters', self::TTL, function () { + $output = collect(); + $output->put('users', User::query()->count()); + $output->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', '')); + $output->put('payments', Payment::query()->count()); + $output->put('eggs', Egg::query()->count()); + $output->put('nests', Nest::query()->count()); + $output->put('locations', Location::query()->count()); - $creditCount = Cache::remember('credit:count', self::TTL, function () { - return User::query()->where("role","!=","admin")->sum('credits'); - }); - - $paymentCount = Cache::remember('payment:count', self::TTL, function () { - return Payment::query()->count(); - }); - - $serverCount = Cache::remember('server:count', self::TTL, function () { - return Server::query()->count(); + $output->put('servers', collect()); + $output['servers']->active = 0; + $output['servers']->total = 0; + $output->put('earnings', collect()); + $output['earnings']->active = 0; + $output['earnings']->total = 0; + $output->put('totalUsagePercent', 0); + + return $output; }); $lastEgg = Egg::query()->latest('updated_at')->first(); $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown'); + + $nodes = Cache::remember('nodes', self::TTL, function() use($counters){ + $output = collect(); + foreach($nodes = Node::query()->get() as $node){ //gets all node information and prepares the structure + $nodeId = $node['id']; + $output->put($nodeId, collect()); + $output[$nodeId]->name = $node['name']; + $node = Pterodactyl::getNode($nodeId); + $output[$nodeId]->usagePercent = round(max($node['allocated_resources']['memory']/($node['memory']*($node['memory_overallocate']+100)/100), $node['allocated_resources']['disk']/($node['disk']*($node['disk_overallocate']+100)/100))*100, 2); + $counters['totalUsagePercent'] += $output[$nodeId]->usagePercent; + + $output[$nodeId]->totalServers = 0; + $output[$nodeId]->activeServers = 0; + $output[$nodeId]->totalEarnings = 0; + $output[$nodeId]->activeEarnings = 0; + } + $counters['totalUsagePercent'] = round($counters['totalUsagePercent']/$nodes->count(), 2); + + foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total + $nodeId = $server['attributes']['node']; + + if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){ + $prize = Product::query()->where('id', $CPServer->product_id)->first()->price; + if (!$CPServer->suspended){ + $counters['earnings']->active += $prize; + $counters['servers']->active ++; + $output[$nodeId]->activeEarnings += $prize; + $output[$nodeId]->activeServers ++; + } + $counters['earnings']->total += $prize; + $counters['servers']->total ++; + $output[$nodeId]->totalEarnings += $prize; + $output[$nodeId]->totalServers ++; + } + } + return $output; + }); return view('admin.overview.index', [ - 'serverCount' => $serverCount, - 'userCount' => $userCount, - 'paymentCount' => $paymentCount, - 'creditCount' => number_format($creditCount, 2, '.', ''), - - 'locationCount' => Location::query()->count(), - 'nodeCount' => Node::query()->count(), - 'nestCount' => Nest::query()->count(), - 'eggCount' => Egg::query()->count(), - 'syncLastUpdate' => $syncLastUpdate + 'counters' => $counters, + 'nodes' => $nodes, + 'syncLastUpdate' => $syncLastUpdate, + 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false ]); - } + } /** * @description Sync locations,nodes,nests,eggs with the linked pterodactyl panel diff --git a/database/seeders/Seeds/SettingsSeeder.php b/database/seeders/Seeds/SettingsSeeder.php index a79501a9..95a110de 100644 --- a/database/seeders/Seeds/SettingsSeeder.php +++ b/database/seeders/Seeds/SettingsSeeder.php @@ -378,6 +378,13 @@ class SettingsSeeder extends Seeder 'type' => 'string', 'description' => 'The URL to your Pterodactyl Panel. Must not end with a / ' ]); + Settings::firstOrCreate([ + 'key' => 'SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT', + ], [ + 'value' => 200, + 'type' => 'integer', + 'description' => 'The Pterodactyl API perPage limit. It is necessary to set it higher than your server count.' + ]); Settings::firstOrCreate([ 'key' => 'SETTINGS::MISC:PHPMYADMIN:URL', ], [ diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php index 8da6292d..bfc81563 100644 --- a/resources/views/admin/overview/index.blade.php +++ b/resources/views/admin/overview/index.blade.php @@ -50,7 +50,7 @@
{{__('Servers')}} - {{$serverCount}} + {{$counters['servers']->total}}
@@ -63,7 +63,7 @@
{{__('Users')}} - {{$userCount}} + {{$counters['users']}}
@@ -77,7 +77,7 @@
{{__('Total')}} {{CREDITS_DISPLAY_NAME}} - {{$creditCount}} + {{$counters['credits']}}
@@ -90,7 +90,7 @@
{{__('Payments')}} - {{$paymentCount}} + {{$counters['payments']}}
@@ -121,19 +121,19 @@ {{__('Locations')}} - {{$locationCount}} + {{$counters['locations']}} {{__('Nodes')}} - {{$nodeCount}} + {{$nodes->count()}} {{__('Nests')}} - {{$nestCount}} + {{$counters['nests']}} {{__('Eggs')}} - {{$eggCount}} + {{$counters['eggs']}} @@ -143,6 +143,58 @@ +
+
+
+
+
+ {{__('Individual nodes')}} +
+
+
+
+ @if ($perPageLimit) +
+
{{ __('Error!') }}
+

+ {{ __('You reached the Pterodactyl perPage limit. Please make sure to set it higher than your server count.') }}
+ {{ __('You can do that in settings.') }} +

+
+ @endif + + + + + + + + + + + + @foreach($nodes as $nodeID => $node) + + + + + + + + @endforeach + + + + + + + + + +
{{__('ID')}}{{__('Node')}}{{__('Server count')}}{{__('Resource usage')}}{{CREDITS_DISPLAY_NAME . ' ' . __('Usage')}}
{{$nodeID}}{{$node->name}}{{$node->activeServers}}/{{$node->totalServers}}{{$node->usagePercent}}%{{$node->activeEarnings}}/{{$node->totalEarnings}}
{{__('Total')}} ({{__('active')}}/{{__('total')}}):{{$counters['servers']->active}}/{{$counters['servers']->total}}{{$counters['totalUsagePercent']}}%{{$counters['earnings']->active}}/{{$counters['earnings']->total}}
+
+
+
diff --git a/resources/views/admin/settings/tabs/system.blade.php b/resources/views/admin/settings/tabs/system.blade.php index f5f2df70..899dc133 100644 --- a/resources/views/admin/settings/tabs/system.blade.php +++ b/resources/views/admin/settings/tabs/system.blade.php @@ -70,6 +70,17 @@ value="{{ config('SETTINGS::SYSTEM:PTERODACTYL:URL') }}" class="form-control @error('pterodactyl-url') is-invalid @enderror" required> +
+
+ + +
+ +