diff --git a/app/Classes/Pterodactyl.php b/app/Classes/Pterodactyl.php index a7bc9bd5..95893dca 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()); } @@ -292,7 +287,7 @@ class Pterodactyl * @param int $pterodactylId * @return mixed */ - public static function getServerAttributes(int $pterodactylId) + public static function getServerAttributes(int $pterodactylId, bool $deleteOn404 = false) { try { $response = self::client()->get("/application/servers/{$pterodactylId}?include=egg,node,nest,location"); @@ -304,7 +299,13 @@ class Pterodactyl - if ($response->failed()) throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status()); + if ($response->failed()){ + if($deleteOn404){ //Delete the server if it does not exist (server deleted on pterodactyl) + Server::where('pterodactyl_id', $pterodactylId)->first()->delete(); + return; + } + else throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status()); + } return $response->json()['attributes']; } @@ -368,8 +369,8 @@ class Pterodactyl throw self::getException($e->getMessage()); } $node = $response['attributes']; - $freeMemory = $node['memory'] - $node['allocated_resources']['memory']; - $freeDisk = $node['disk'] - $node['allocated_resources']['disk']; + $freeMemory = ($node['memory']*($node['memory_overallocate']+100)/100) - $node['allocated_resources']['memory']; + $freeDisk = ($node['disk']*($node['disk_overallocate']+100)/100) - $node['allocated_resources']['disk']; if ($freeMemory < $requireMemory) { return false; } diff --git a/app/Classes/Settings/System.php b/app/Classes/Settings/System.php index 22fed19e..3cfd8517 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", "enable-upgrades" => "string", @@ -79,6 +80,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..c0cb0c4c 100644 --- a/app/Http/Controllers/Admin/OverViewController.php +++ b/app/Http/Controllers/Admin/OverViewController.php @@ -11,6 +11,10 @@ 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; +use App\Models\Ticket; +use Carbon\Carbon; class OverViewController extends Controller { @@ -18,38 +22,139 @@ 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(); + //Set basic variables in the collection + $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'); - }); + //Prepare for counting + $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); - $paymentCount = Cache::remember('payment:count', self::TTL, function () { - return Payment::query()->count(); - }); - - $serverCount = Cache::remember('server:count', self::TTL, function () { - return Server::query()->count(); + //Prepare subCollection 'payments' + $output->put('payments', collect()); + //Get and save payments from last 2 months for later filtering and looping + $payments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->where('status', 'paid')->get(); + //Prepare collections and set a few variables + $output['payments']->put('thisMonth', collect()); + $output['payments']->put('lastMonth', collect()); + $output['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString(); + $output['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString(); + $output['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString(); + $output['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString(); + + //Fill out variables for each currency separately + foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){ + $paymentCurrency = $payment->currency_code; + if(!isset($output['payments']['thisMonth'][$paymentCurrency])){ + $output['payments']['thisMonth']->put($paymentCurrency, collect()); + $output['payments']['thisMonth'][$paymentCurrency]->total = 0; + $output['payments']['thisMonth'][$paymentCurrency]->count = 0; + } + $output['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price; + $output['payments']['thisMonth'][$paymentCurrency]->count ++; + } + foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){ + $paymentCurrency = $payment->currency_code; + if(!isset($output['payments']['lastMonth'][$paymentCurrency])){ + $output['payments']['lastMonth']->put($paymentCurrency, collect()); + $output['payments']['lastMonth'][$paymentCurrency]->total = 0; + $output['payments']['lastMonth'][$paymentCurrency]->count = 0; + } + $output['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price; + $output['payments']['lastMonth'][$paymentCurrency]->count ++; + } + $output['payments']->total = Payment::query()->count(); + + 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'] = ($nodes->count())?round($counters['totalUsagePercent']/$nodes->count(), 2):0; + + 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; + }); + + $tickets = Cache::remember('tickets', self::TTL, function(){ + $output = collect(); + foreach(Ticket::query()->latest()->take(3)->get() as $ticket){ + $output->put($ticket->ticket_id, collect()); + $output[$ticket->ticket_id]->title = $ticket->title; + $user = User::query()->where('id', $ticket->user_id)->first(); + $output[$ticket->ticket_id]->user_id = $user->id; + $output[$ticket->ticket_id]->user = $user->name; + $output[$ticket->ticket_id]->status = $ticket->status; + $output[$ticket->ticket_id]->last_updated = $ticket->updated_at->diffForHumans(); + switch ($ticket->status) { + case 'Open': + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-success'; + break; + case 'Closed': + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-danger'; + break; + case 'Answered': + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-info'; + break; + default: + $output[$ticket->ticket_id]->statusBadgeColor = 'badge-warning'; + break; + } + } + return $output; + }); + //dd($counters); 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, + 'tickets' => $tickets ]); - } + } /** * @description Sync locations,nodes,nests,eggs with the linked pterodactyl panel diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index a5e7bcbd..61e480f0 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Classes\Pterodactyl; use App\Models\Egg; use App\Models\Location; use App\Models\Node; @@ -55,6 +56,10 @@ class ProductController extends Controller public function getLocationsBasedOnEgg(Request $request, Egg $egg) { $nodes = $this->getNodesBasedOnEgg($request, $egg); + foreach($nodes as $key => $node){ + $pteroNode = Pterodactyl::getNode($node->id); + if($pteroNode['allocated_resources']['memory']>=($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)||$pteroNode['allocated_resources']['disk']>=($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)) $nodes->forget($key); + } $locations = collect(); //locations @@ -87,7 +92,7 @@ class ProductController extends Controller { if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400'); - return Product::query() + $products = Product::query() ->where('disabled', '=', false) ->whereHas('nodes', function (Builder $builder) use ($node) { $builder->where('id', '=', $node->id); @@ -96,5 +101,12 @@ class ProductController extends Controller $builder->where('id', '=', $egg->id); }) ->get(); + + $pteroNode = Pterodactyl::getNode($node->id); + foreach($products as $key => $product){ + if($product->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true; + } + + return $products; } } diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index cf731ddf..5ee16dd5 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -30,8 +30,8 @@ class ServerController extends Controller foreach ($servers as $server) { //Get server infos from ptero - $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id); - + $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id, true); + if(!$serverAttributes) continue; $serverRelationships = $serverAttributes['relationships']; $serverLocationAttributes = $serverRelationships['location']['attributes']; @@ -45,6 +45,13 @@ class ServerController extends Controller $server->node = $serverRelationships['node']['attributes']['name']; + //Check if a server got renamed on Pterodactyl + $savedServer = Server::query()->where('id', $server->id)->first(); + if($savedServer->name != $serverAttributes['name']){ + $savedServer->name = $serverAttributes['name']; + $server->name = $serverAttributes['name']; + $savedServer->save(); + } //get productname by product_id for server $product = Product::find($server->product_id); @@ -234,6 +241,9 @@ class ServerController extends Controller $serverRelationships = $serverAttributes['relationships']; $serverLocationAttributes = $serverRelationships['location']['attributes']; + //Get current product + $currentProduct = Product::where('id', $server->product_id)->first(); + //Set server infos $server->location = $serverLocationAttributes['long'] ? $serverLocationAttributes['long'] : @@ -242,11 +252,19 @@ class ServerController extends Controller $server->node = $serverRelationships['node']['attributes']['name']; $server->name = $serverAttributes['name']; $server->egg = $serverRelationships['egg']['attributes']['name']; - $products = Product::orderBy("created_at")->get(); + + $pteroNode = Pterodactyl::getNode($serverRelationships['node']['attributes']['id']); + + $products = Product::orderBy("created_at") + ->whereHas('nodes', function (Builder $builder) use ($serverRelationships) { //Only show products for that node + $builder->where('id', '=', $serverRelationships['node']['attributes']['id']); + }) + ->get(); // Set the each product eggs array to just contain the eggs name foreach ($products as $product) { $product->eggs = $product->eggs->pluck('name')->toArray(); + if($product->memory-$currentProduct->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk-$currentProduct->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true; } return view('servers.settings')->with([ diff --git a/config/app.php b/config/app.php index 90bdfdde..0778437d 100644 --- a/config/app.php +++ b/config/app.php @@ -4,7 +4,7 @@ use App\Models\Settings; return [ - 'version' => '0.8.2', + 'version' => '0.8.3', /* |-------------------------------------------------------------------------- diff --git a/database/seeders/Seeds/SettingsSeeder.php b/database/seeders/Seeds/SettingsSeeder.php index fe695a76..4e9dc12d 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 1b697104..957c72e1 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']->total}}
@@ -121,19 +121,19 @@ {{__('Locations')}} - {{$locationCount}} + {{$counters['locations']}} {{__('Nodes')}} - {{$nodeCount}} + {{$nodes->count()}} {{__('Nests')}} - {{$nestCount}} + {{$counters['nests']}} {{__('Eggs')}} - {{$eggCount}} + {{$counters['eggs']}} @@ -142,20 +142,173 @@ {{__('Last updated :date', ['date' => $syncLastUpdate])}} - -
+
+
+
+
+ {{__('Latest tickets')}} +
+
+
+
+ @if(!$tickets->count()){{__('There are no tickets')}}. + @else + + + + + + + + + + + + @foreach($tickets as $ticket_id => $ticket) + + + + + + + @endforeach + + +
{{__('Title')}}{{__('User')}}{{__('Status')}}{{__('Last updated')}}
#{{$ticket_id}} - {{$ticket->title}}{{$ticket->user}}{{$ticket->status}}{{$ticket->last_updated}}
+ @endif +
+
{{__('Controlpanel.gg')}}
+
+
+ +
+ +
+
+
+
+
+
+
+
+ {{__('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.') }}
+ {{ __('Note') }}: {{ __('If this error persists even after changing the limit, it might mean a server was deleted on Pterodactyl, but not on ControlPanel.') }} +

+
+ @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}}
- +
+
+
+
+ {{__('Latest payments')}} +
+
+
+
+
+
+ {{__('Last month')}}: + + + + + + + + + + + + @foreach($counters['payments']['lastMonth'] as $currency => $income) + + + + + + @endforeach + +
{{__('Currency')}}{{__('Number of payments')}}{{__('Total income')}}
{{$currency}}{{$income->count}}{{$income->total}}
+
+
+ {{__('This month')}}: + + + + + + + + + + + + @foreach($counters['payments']['thisMonth'] as $currency => $income) + + + + + + @endforeach + +
{{__('Currency')}}{{__('Number of payments')}}{{__('Total income')}}
{{$currency}}{{$income->count}}{{$income->total}}
+
+
+
+
diff --git a/resources/views/admin/settings/tabs/system.blade.php b/resources/views/admin/settings/tabs/system.blade.php index 9511bd7d..dee381c2 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>
+
+
+ + +
+ +
diff --git a/resources/views/moderator/ticket/show.blade.php b/resources/views/moderator/ticket/show.blade.php index 22bd2ea1..f6a309f2 100644 --- a/resources/views/moderator/ticket/show.blade.php +++ b/resources/views/moderator/ticket/show.blade.php @@ -51,11 +51,13 @@ @endif

Created on: {{ $ticket->created_at->diffForHumans() }}

+ @if($ticket->status!='Closed')
{{csrf_field()}} {{method_field("POST") }}
+ @endif
diff --git a/resources/views/servers/create.blade.php b/resources/views/servers/create.blade.php index 5454a6b6..4ac74f79 100644 --- a/resources/views/servers/create.blade.php +++ b/resources/views/servers/create.blade.php @@ -218,10 +218,10 @@ diff --git a/resources/views/servers/index.blade.php b/resources/views/servers/index.blade.php index 3c84086d..8dc81061 100644 --- a/resources/views/servers/index.blade.php +++ b/resources/views/servers/index.blade.php @@ -40,127 +40,128 @@
@foreach ($servers as $server) - -
-
-
-
{{ $server->name }} -
-
-