diff --git a/app/Http/Controllers/Admin/OverViewController.php b/app/Http/Controllers/Admin/OverViewController.php index c0cb0c4c..2d1784a1 100644 --- a/app/Http/Controllers/Admin/OverViewController.php +++ b/app/Http/Controllers/Admin/OverViewController.php @@ -22,103 +22,103 @@ class OverViewController extends Controller public function index() { - $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()); + //Get counters + $counters = collect(); + //Set basic variables in the collection + $counters->put('users', User::query()->count()); + $counters->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', '')); + $counters->put('payments', Payment::query()->count()); + $counters->put('eggs', Egg::query()->count()); + $counters->put('nests', Nest::query()->count()); + $counters->put('locations', Location::query()->count()); - //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); + //Prepare for counting + $counters->put('servers', collect()); + $counters['servers']->active = 0; + $counters['servers']->total = 0; + $counters->put('earnings', collect()); + $counters['earnings']->active = 0; + $counters['earnings']->total = 0; + $counters->put('totalUsagePercent', 0); - //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 ++; + //Prepare subCollection 'payments' + $counters->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 + $counters['payments']->put('thisMonth', collect()); + $counters['payments']->put('lastMonth', collect()); + $counters['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString(); + $counters['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString(); + $counters['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString(); + $counters['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($counters['payments']['thisMonth'][$paymentCurrency])){ + $counters['payments']['thisMonth']->put($paymentCurrency, collect()); + $counters['payments']['thisMonth'][$paymentCurrency]->total = 0; + $counters['payments']['thisMonth'][$paymentCurrency]->count = 0; } - 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 ++; + $counters['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price; + $counters['payments']['thisMonth'][$paymentCurrency]->count ++; + } + foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){ + $paymentCurrency = $payment->currency_code; + if(!isset($counters['payments']['lastMonth'][$paymentCurrency])){ + $counters['payments']['lastMonth']->put($paymentCurrency, collect()); + $counters['payments']['lastMonth'][$paymentCurrency]->total = 0; + $counters['payments']['lastMonth'][$paymentCurrency]->count = 0; } - $output['payments']->total = Payment::query()->count(); - - return $output; - }); + $counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price; + $counters['payments']['lastMonth'][$paymentCurrency]->count ++; + } + $counters['payments']->total = Payment::query()->count(); $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 ++; + //Get node information + $nodes = collect(); + foreach($DBnodes = Node::query()->get() as $DBnode){ //gets all node information and prepares the structure + $nodeId = $DBnode['id']; + $nodes->put($nodeId, collect()); + $nodes[$nodeId]->name = $DBnode['name']; + $pteroNode = Pterodactyl::getNode($nodeId); + $nodes[$nodeId]->usagePercent = round(max($pteroNode['allocated_resources']['memory']/($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100), $pteroNode['allocated_resources']['disk']/($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100))*100, 2); + $counters['totalUsagePercent'] += $nodes[$nodeId]->usagePercent; + + $nodes[$nodeId]->totalServers = 0; + $nodes[$nodeId]->activeServers = 0; + $nodes[$nodeId]->totalEarnings = 0; + $nodes[$nodeId]->activeEarnings = 0; + } + $counters['totalUsagePercent'] = ($DBnodes->count())?round($counters['totalUsagePercent']/$DBnodes->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()){ + $price = Product::query()->where('id', $CPServer->product_id)->first()->price; + if (!$CPServer->suspended){ + $counters['earnings']->active += $price; + $counters['servers']->active ++; + $nodes[$nodeId]->activeEarnings += $price; + $nodes[$nodeId]->activeServers ++; } + $counters['earnings']->total += $price; + $counters['servers']->total ++; + $nodes[$nodeId]->totalEarnings += $price; + $nodes[$nodeId]->totalServers ++; } - return $output; - }); + } + + + //Get latest tickets $tickets = Cache::remember('tickets', self::TTL, function(){ $output = collect(); foreach(Ticket::query()->latest()->take(3)->get() as $ticket){ @@ -146,7 +146,7 @@ class OverViewController extends Controller } return $output; }); - //dd($counters); + return view('admin.overview.index', [ 'counters' => $counters, 'nodes' => $nodes, diff --git a/app/Http/Controllers/Admin/ServerController.php b/app/Http/Controllers/Admin/ServerController.php index 5ebc29bc..a34b097b 100644 --- a/app/Http/Controllers/Admin/ServerController.php +++ b/app/Http/Controllers/Admin/ServerController.php @@ -123,6 +123,44 @@ class ServerController extends Controller return redirect()->back()->with('success', __('Server has been updated!')); } + public function syncServers() + { + $pteroServers = Pterodactyl::getServers(); + $CPServers = Server::get(); + + $CPIDArray = []; + $renameCount = 0; + foreach($CPServers as $CPServer)//go thru all CP servers and make array with IDs as keys. All values are false. + { + if($CPServer->pterodactyl_id) $CPIDArray[$CPServer->pterodactyl_id] = false; + } + + foreach($pteroServers as $server)//go thru all ptero servers, if server exists, change value to true in array. + { + if(isset($CPIDArray[$server['attributes']['id']])){ + $CPIDArray[$server['attributes']['id']]=true; + + if(isset($server['attributes']['name'])){//failsafe + //Check if a server got renamed + $savedServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first(); + if($savedServer->name != $server['attributes']['name']){ + $savedServer->name = $server['attributes']['name']; + $savedServer->save(); + $renameCount++; + } + } + } + } + $filteredArray = array_filter($CPIDArray, function($v, $k) { return $v == false; }, ARRAY_FILTER_USE_BOTH); //Array of servers, that dont exist on ptero (value == false) + $deleteCount = 0; + foreach($filteredArray as $key => $CPID)//delete servers that dont exist on ptero anymore + { + if(!Pterodactyl::getServerAttributes($key, true)) $deleteCount++; + } + + return redirect()->back()->with('success', __('Servers synced successfully' . (($renameCount)?(',\n' . __('renamed') . ' ' . $renameCount . ' ' . __('servers')):'') . ((count($filteredArray))?(',\n' . __('deleted') . ' ' . $deleteCount . '/' . count($filteredArray) . ' ' . __('old servers')):''))) . '.'; + } + /** * @return JsonResponse|mixed * @throws Exception diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php index 957c72e1..facf060a 100644 --- a/resources/views/admin/overview/index.blade.php +++ b/resources/views/admin/overview/index.blade.php @@ -207,11 +207,13 @@ @if ($perPageLimit)
+
{{ __('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.') }}
+ {{ __('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. Try clicking the button below.') }}