From 3ad934bc86cdb33cedac7fa312c4babb363ca3c8 Mon Sep 17 00:00:00 2001 From: 1Day Date: Mon, 22 Aug 2022 11:14:40 +0200 Subject: [PATCH 1/5] beginning of Overview page --- app/Classes/Pterodactyl/PterodactylClient.php | 22 +++++ .../Controllers/Admin/OverviewController.php | 96 +++++++++++++++++++ .../views/admin/overview/index.blade.php | 96 +++++++++++++++++++ .../navigation/sidebar/301-overview.blade.php | 10 ++ routes/web.php | 2 + 5 files changed, 226 insertions(+) create mode 100644 app/Http/Controllers/Admin/OverviewController.php create mode 100644 resources/views/admin/overview/index.blade.php create mode 100644 resources/views/layouts/parts/navigation/sidebar/301-overview.blade.php diff --git a/app/Classes/Pterodactyl/PterodactylClient.php b/app/Classes/Pterodactyl/PterodactylClient.php index 8b9c3f7..95f6fae 100755 --- a/app/Classes/Pterodactyl/PterodactylClient.php +++ b/app/Classes/Pterodactyl/PterodactylClient.php @@ -71,6 +71,16 @@ public function getNests(): PromiseInterface|Response return $this->handleResponse($response); } + /** + * @return mixed + * @throws Exception + * @description Returns the infos of a single node + */ + public function getNode($id) { + $response = $this->client->get('/application/nodes/' . $id); + return $this->handleResponse($response); + } + /** * @throws PterodactylRequestException */ @@ -156,6 +166,18 @@ public function updateServerDetails(int $pterodactyl_id, array $data): PromiseIn return $this->handleResponse($response); } + /** + * Get Servers from Pterodactyl + * + * @return PromiseInterface|Response + * @throws PterodactylRequestException + */ + public function getServers(): PromiseInterface|Response + { + $response = $this->client->get('application/servers?per_page=' . self::PER_PAGE); + return $this->handleResponse($response); + } + /** * Delete server * diff --git a/app/Http/Controllers/Admin/OverviewController.php b/app/Http/Controllers/Admin/OverviewController.php new file mode 100644 index 0000000..b42419c --- /dev/null +++ b/app/Http/Controllers/Admin/OverviewController.php @@ -0,0 +1,96 @@ +put('users', User::query()->count()); + $output->put('credits', number_format(User::query()->sum('credits'), 2, '.', '')); + $output->put('eggs', Egg::query()->count()); + $output->put('nests', Nest::query()->count()); + $output->put('locations', Location::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($syncLastUpdate, $pterodactylClient, $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 = $pterodactylClient->getNode($nodeId)->json()["attributes"]; + + $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); + $response = ($pterodactylClient->getServers()->json()); + + foreach($response["data"] 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 = $CPServer->price; + if (!$CPServer->suspended){ + $counters['earnings']->active += $price; + $counters['servers']->active ++; + $output[$nodeId]->activeEarnings += $price; + $output[$nodeId]->activeServers ++; + } + $counters['earnings']->total += $price; + $counters['servers']->total ++; + $output[$nodeId]->totalEarnings += $price; + $output[$nodeId]->totalServers ++; + } + } + return $output; + }); + return view('admin.overview.index', [ + 'counters' => $counters, + 'nodes' => $nodes, + 'syncLastUpdate' => $syncLastUpdate, + 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false, + 'settings' => $settings + ]); + + +} +} diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php new file mode 100644 index 0000000..5263c3c --- /dev/null +++ b/resources/views/admin/overview/index.blade.php @@ -0,0 +1,96 @@ +@extends('layouts.dashboard') + +@section('content') +
+ +
+ +
+
+
+
+
+ Total servers + {{$counters['servers']->total}} +
+
+
+ +
+
+
+
+
+
+ + + +
+
+
+
+
+ {{__("Total users")}} + {{$counters['users']}} +
+
+
+ +
+
+
+
+
+
+ + + +
+
+
+
+
+ {{__('Total :credits ', ['credits' => $settings->credits_display_name])}} + + {{$counters['credits']}} +
+
+
+ +
+
+
+
+
+
+ + + +
+
+
+
+
+ {{__('Total :credits earnings', ['credits' => $settings->credits_display_name])}} + + {{$counters['earnings']->active}} +
+
+
+ +
+
+
+
+
+
+ +
+ + + + +
+@endsection + + diff --git a/resources/views/layouts/parts/navigation/sidebar/301-overview.blade.php b/resources/views/layouts/parts/navigation/sidebar/301-overview.blade.php new file mode 100644 index 0000000..4ba495c --- /dev/null +++ b/resources/views/layouts/parts/navigation/sidebar/301-overview.blade.php @@ -0,0 +1,10 @@ +@can('admin.overview.read') + +@endcan diff --git a/routes/web.php b/routes/web.php index 922fb62..d0936e0 100755 --- a/routes/web.php +++ b/routes/web.php @@ -2,6 +2,7 @@ use App\Http\Controllers\Admin\ConfigurationController; use App\Http\Controllers\Admin\NotificationTemplateController; +use App\Http\Controllers\Admin\OverviewController; use App\Http\Controllers\Admin\RoleController; use App\Http\Controllers\Admin\ServerController as AdminServerController; use App\Http\Controllers\Admin\UserController; @@ -54,6 +55,7 @@ Route::resource('roles', RoleController::class); Route::resource('users', UserController::class); Route::resource('servers', AdminServerController::class); + Route::get('overview', [OverviewController::class, 'index'])->name('overview.index'); Route::get('/configurations/sync', [ConfigurationController::class, 'syncPterodactyl'])->name('configurations.sync'); Route::get('/configurations/{configuration}/clone', [ConfigurationController::class, 'clone'])->name('configurations.clone'); Route::resource('configurations', ConfigurationController::class); From b67d8302c6da744fdd964bc9e8a82f16059f0627 Mon Sep 17 00:00:00 2001 From: 1day2die Date: Sun, 4 Sep 2022 22:42:29 +0200 Subject: [PATCH 2/5] formatting and stuff --- .../Controllers/Admin/OverviewController.php | 166 +++++++++++------- .../views/admin/overview/index.blade.php | 4 +- 2 files changed, 102 insertions(+), 68 deletions(-) diff --git a/app/Http/Controllers/Admin/OverviewController.php b/app/Http/Controllers/Admin/OverviewController.php index b42419c..9314b88 100644 --- a/app/Http/Controllers/Admin/OverviewController.php +++ b/app/Http/Controllers/Admin/OverviewController.php @@ -11,7 +11,6 @@ use App\Models\Server; use App\Models\User; use App\Settings\GeneralSettings; -use Illuminate\Support\Facades\Cache; class OverviewController extends Controller { @@ -22,75 +21,110 @@ public function index(PterodactylClient $pterodactylClient) /** @var GeneralSettings $settings */ $settings = app(GeneralSettings::class); - - - $counters = Cache::remember('counters', self::TTL, function () { - $output = collect(); - $output->put('users', User::query()->count()); - $output->put('credits', number_format(User::query()->sum('credits'), 2, '.', '')); - $output->put('eggs', Egg::query()->count()); - $output->put('nests', Nest::query()->count()); - $output->put('locations', Location::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; - }); - + //Get counters + $counters = collect(); + //Set basic variables in the collection + $counters->put('users', User::query()->count()); + $counters->put('credits', number_format(User::query()->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 + $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' + $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; + } + $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; + } + $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($syncLastUpdate, $pterodactylClient, $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 = $pterodactylClient->getNode($nodeId)->json()["attributes"]; - - $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); - $response = ($pterodactylClient->getServers()->json()); - - foreach($response["data"] 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 = $CPServer->price; - if (!$CPServer->suspended){ - $counters['earnings']->active += $price; - $counters['servers']->active ++; - $output[$nodeId]->activeEarnings += $price; - $output[$nodeId]->activeServers ++; - } - $counters['earnings']->total += $price; - $counters['servers']->total ++; - $output[$nodeId]->totalEarnings += $price; - $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 = $pterodactylClient->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; + + $response = ($pterodactylClient->getServers()->json()); + foreach ($response["data"] 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 = Server::query()->where('id', $CPServer->product_id)->first()->price; + if (!$CPServer->suspended) { + $counters['earnings']->active += $prize; + $counters['servers']->active++; + $nodes[$nodeId]->activeEarnings += $prize; + $nodes[$nodeId]->activeServers++; } + $counters['earnings']->total += $prize; + $counters['servers']->total++; + $nodes[$nodeId]->totalEarnings += $prize; + $nodes[$nodeId]->totalServers++; } - return $output; - }); - return view('admin.overview.index', [ - 'counters' => $counters, - 'nodes' => $nodes, - 'syncLastUpdate' => $syncLastUpdate, - 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false, - 'settings' => $settings - ]); + } -} + return view('admin.overview.index', [ + 'counters' => $counters, + 'nodes' => $nodes, + 'syncLastUpdate' => $syncLastUpdate, + 'perPageLimit' => ($counters['servers']->total != Server::query()->count()) ? true : false, + 'settings' => $settings + ]); + + + } } diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php index 5263c3c..b108650 100644 --- a/resources/views/admin/overview/index.blade.php +++ b/resources/views/admin/overview/index.blade.php @@ -30,7 +30,8 @@
- {{__("Total users")}} + {{__("Total users")}} {{$counters['users']}}
@@ -89,7 +90,6 @@ -
@endsection From 558c6d06cfead6c21be2730142c39d34244f96ee Mon Sep 17 00:00:00 2001 From: 1day2die Date: Sun, 4 Sep 2022 22:52:46 +0200 Subject: [PATCH 3/5] cleanup --- .../Controllers/Admin/OverviewController.php | 69 +++++-------------- .../views/admin/overview/index.blade.php | 20 ++++++ 2 files changed, 39 insertions(+), 50 deletions(-) diff --git a/app/Http/Controllers/Admin/OverviewController.php b/app/Http/Controllers/Admin/OverviewController.php index 9314b88..6562647 100644 --- a/app/Http/Controllers/Admin/OverviewController.php +++ b/app/Http/Controllers/Admin/OverviewController.php @@ -16,20 +16,21 @@ class OverviewController extends Controller { public const TTL = 86400; - public function index(PterodactylClient $pterodactylClient) + public function index(PterodactylClient $pterodactylClient, GeneralSettings $settings) { - /** @var GeneralSettings $settings */ - $settings = app(GeneralSettings::class); + $userCount = User::query()->count(); + $creditCount = User::query()->sum('credits'); + $creditCount = number_format($creditCount, 2, '.', ''); //Get counters $counters = collect(); //Set basic variables in the collection - $counters->put('users', User::query()->count()); - $counters->put('credits', number_format(User::query()->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()); + $counters->put('users', $userCount); + $counters->put('credits', $creditCount); + // $counters->put('payments', Payment::all()->count()); + $counters->put('eggs', Egg::all()->count()); + $counters->put('nests', Nest::all()->count()); + $counters->put('locations', Location::all()->count()); //Prepare for counting $counters->put('servers', collect()); @@ -40,49 +41,15 @@ public function index(PterodactylClient $pterodactylClient) $counters['earnings']->total = 0; $counters->put('totalUsagePercent', 0); - /* - //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; - } - $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; - } - $counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price; - $counters['payments']['lastMonth'][$paymentCurrency]->count++; - } - $counters['payments']->total = Payment::query()->count(); - */ + //ToDo add payments from v0.8 PR + $lastEgg = Egg::query()->latest('updated_at')->first(); $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown'); //Get node information $nodes = collect(); - foreach ($DBnodes = Node::query()->get() as $DBnode) { //gets all node information and prepares the structure + foreach ($DBnodes = Node::all() as $DBnode) { //gets all node information and prepares the structure $nodeId = $DBnode['id']; $nodes->put($nodeId, collect()); $nodes[$nodeId]->name = $DBnode['name']; @@ -99,9 +66,13 @@ public function index(PterodactylClient $pterodactylClient) $response = ($pterodactylClient->getServers()->json()); foreach ($response["data"] as $server) { //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total + + $serverId = $server['attributes']['id']; $nodeId = $server['attributes']['node']; - if ($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()) { + $CPServer = Server::query()->where('pterodactyl_id', $serverId)->first(); + + if ($CPServer) { $prize = Server::query()->where('id', $CPServer->product_id)->first()->price; if (!$CPServer->suspended) { $counters['earnings']->active += $prize; @@ -119,9 +90,7 @@ public function index(PterodactylClient $pterodactylClient) return view('admin.overview.index', [ 'counters' => $counters, - 'nodes' => $nodes, - 'syncLastUpdate' => $syncLastUpdate, - 'perPageLimit' => ($counters['servers']->total != Server::query()->count()) ? true : false, + 'lastPteroSync' => $syncLastUpdate, 'settings' => $settings ]); diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php index b108650..c9e574a 100644 --- a/resources/views/admin/overview/index.blade.php +++ b/resources/views/admin/overview/index.blade.php @@ -86,6 +86,26 @@ class="h6 font-semibold text-muted text-sm d-block mb-2">{{__("Total users")}}
+ +
+
+
+
+
+ {{__('Last Sync from Pterodactyl')}} + + {{$lastPteroSync}} +
+
+
+ +
+
+
+
+
+
+
From 509a8deb4ea688da001996fa2562bce54b538964 Mon Sep 17 00:00:00 2001 From: 1day2die Date: Wed, 19 Oct 2022 22:55:13 +0200 Subject: [PATCH 4/5] rewrite --- .../Controllers/Admin/OverviewController.php | 68 +++++++++++++------ ...rview.blade.php => 350-overview.blade.php} | 0 2 files changed, 48 insertions(+), 20 deletions(-) rename resources/views/layouts/parts/navigation/sidebar/{301-overview.blade.php => 350-overview.blade.php} (100%) diff --git a/app/Http/Controllers/Admin/OverviewController.php b/app/Http/Controllers/Admin/OverviewController.php index 6562647..d68e20a 100644 --- a/app/Http/Controllers/Admin/OverviewController.php +++ b/app/Http/Controllers/Admin/OverviewController.php @@ -16,18 +16,43 @@ class OverviewController extends Controller { public const TTL = 86400; - public function index(PterodactylClient $pterodactylClient, GeneralSettings $settings) + public function index(GeneralSettings $settings) { - $userCount = User::query()->count(); - $creditCount = User::query()->sum('credits'); - $creditCount = number_format($creditCount, 2, '.', ''); + //Check the last Sync Status + $lastEgg = Egg::query()->latest('updated_at')->first(); + $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown'); + + //Generate the Counters used on the Overview + $counters = $this->constructNodeInfo()["counters"]; + $nodes = $this->constructNodeInfo()["nodes"]; + + return view('admin.overview.index', [ + 'counters' => $counters, + 'nodeCounters' => $nodes, + 'lastPteroSync' => $syncLastUpdate, + 'settings' => $settings + ]); + + + } + + + /** + * @return \Illuminate\Support\Collection + */ + private function constructCounters() + { + + $userCount = User::query()->count(); + $creditCount = User::query()->sum('credits'); + $creditCount = number_format($creditCount, 2, '.', ''); //Get counters $counters = collect(); //Set basic variables in the collection $counters->put('users', $userCount); $counters->put('credits', $creditCount); - // $counters->put('payments', Payment::all()->count()); + // $counters->put('payments', Payment::all()->count()); $counters->put('eggs', Egg::all()->count()); $counters->put('nests', Nest::all()->count()); $counters->put('locations', Location::all()->count()); @@ -41,11 +66,17 @@ public function index(PterodactylClient $pterodactylClient, GeneralSettings $set $counters['earnings']->total = 0; $counters->put('totalUsagePercent', 0); - //ToDo add payments from v0.8 PR + return $counters; + } - $lastEgg = Egg::query()->latest('updated_at')->first(); - $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown'); + /** + * @return array + */ + private function constructNodeInfo() + { + $pterodactylClient = app(PterodactylClient::class); + $counters = $this->constructCounters(); //Get node information $nodes = collect(); @@ -73,27 +104,24 @@ public function index(PterodactylClient $pterodactylClient, GeneralSettings $set $CPServer = Server::query()->where('pterodactyl_id', $serverId)->first(); if ($CPServer) { - $prize = Server::query()->where('id', $CPServer->product_id)->first()->price; + $price = Server::query()->where('id', $CPServer->product_id)->first()->price; if (!$CPServer->suspended) { - $counters['earnings']->active += $prize; + $counters['earnings']->active += $price; $counters['servers']->active++; - $nodes[$nodeId]->activeEarnings += $prize; + $nodes[$nodeId]->activeEarnings += $price; $nodes[$nodeId]->activeServers++; } - $counters['earnings']->total += $prize; + $counters['earnings']->total += $price; $counters['servers']->total++; - $nodes[$nodeId]->totalEarnings += $prize; + $nodes[$nodeId]->totalEarnings += $price; $nodes[$nodeId]->totalServers++; } } - - - return view('admin.overview.index', [ + return [ 'counters' => $counters, - 'lastPteroSync' => $syncLastUpdate, - 'settings' => $settings - ]); + 'nodes' => $nodes + ]; + } - } } diff --git a/resources/views/layouts/parts/navigation/sidebar/301-overview.blade.php b/resources/views/layouts/parts/navigation/sidebar/350-overview.blade.php similarity index 100% rename from resources/views/layouts/parts/navigation/sidebar/301-overview.blade.php rename to resources/views/layouts/parts/navigation/sidebar/350-overview.blade.php From 79b74b6986fb98aafd8f1c1d523b913f4740e5e2 Mon Sep 17 00:00:00 2001 From: 1day2die Date: Wed, 26 Oct 2022 09:55:24 +0200 Subject: [PATCH 5/5] fixed overview page. needs to be made prettier --- app/Classes/Pterodactyl/PterodactylClient.php | 2 +- .../Controllers/Admin/OverviewController.php | 7 +++- .../views/admin/overview/index.blade.php | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/Classes/Pterodactyl/PterodactylClient.php b/app/Classes/Pterodactyl/PterodactylClient.php index 95f6fae..6e2faca 100755 --- a/app/Classes/Pterodactyl/PterodactylClient.php +++ b/app/Classes/Pterodactyl/PterodactylClient.php @@ -77,7 +77,7 @@ public function getNests(): PromiseInterface|Response * @description Returns the infos of a single node */ public function getNode($id) { - $response = $this->client->get('/application/nodes/' . $id); + $response = $this->client->get('application/nodes/' . $id); return $this->handleResponse($response); } diff --git a/app/Http/Controllers/Admin/OverviewController.php b/app/Http/Controllers/Admin/OverviewController.php index d68e20a..b9060c8 100644 --- a/app/Http/Controllers/Admin/OverviewController.php +++ b/app/Http/Controllers/Admin/OverviewController.php @@ -11,6 +11,7 @@ use App\Models\Server; use App\Models\User; use App\Settings\GeneralSettings; +use App\Settings\PterodactylSettings; class OverviewController extends Controller { @@ -75,7 +76,8 @@ private function constructCounters() */ private function constructNodeInfo() { - $pterodactylClient = app(PterodactylClient::class); + $pterosettings = new PterodactylSettings(); + $pterodactylClient = new PterodactylClient($pterosettings); $counters = $this->constructCounters(); //Get node information @@ -83,8 +85,9 @@ private function constructNodeInfo() foreach ($DBnodes = Node::all() as $DBnode) { //gets all node information and prepares the structure $nodeId = $DBnode['id']; $nodes->put($nodeId, collect()); + $nodes[$nodeId]->id = $nodeId; $nodes[$nodeId]->name = $DBnode['name']; - $pteroNode = $pterodactylClient->getNode($nodeId); + $pteroNode = $pterodactylClient->getNode($nodeId)->json()["attributes"]; $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; diff --git a/resources/views/admin/overview/index.blade.php b/resources/views/admin/overview/index.blade.php index c9e574a..68b990e 100644 --- a/resources/views/admin/overview/index.blade.php +++ b/resources/views/admin/overview/index.blade.php @@ -106,6 +106,46 @@ class="h6 font-semibold text-muted text-sm d-block mb-2">{{__("Total users")}} + + +
+
+
+ {{__("Nodes Info")}} +
+ + + + + + + + + + + + + + @foreach($nodeCounters as $node) + + + + + + + + + + @endforeach + + + +
#NameUsage-%Total ServersActive ServersActive Earnings
{{$node->id}}{{$node->name}}{{$node->usagePercent}}{{$node->totalServers}}{{$node->activeServers}}{{$node->activeEarnings}}
+
+
+
+
+