浏览代码

Show individual node status on admin overview

ok236449 2 年之前
父节点
当前提交
043edf9a9b

+ 5 - 10
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());
         }

+ 2 - 0
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",

+ 61 - 24
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();
-        });
-
-        $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();
-        });
+        $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());
 
-        $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;
 
-        return view('admin.overview.index', [
-            'serverCount'  => $serverCount,
-            'userCount'    => $userCount,
-            'paymentCount' => $paymentCount,
-            'creditCount'  => number_format($creditCount, 2, '.', ''),
+                $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;
+        });
 
-            'locationCount'  => Location::query()->count(),
-            'nodeCount'      => Node::query()->count(),
-            'nestCount'      => Nest::query()->count(),
-            'eggCount'       => Egg::query()->count(),
-            'syncLastUpdate' => $syncLastUpdate
+        return view('admin.overview.index', [
+            '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

+ 7 - 0
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',
         ], [

+ 60 - 8
resources/views/admin/overview/index.blade.php

@@ -50,7 +50,7 @@
 
                         <div class="info-box-content">
                             <span class="info-box-text">{{__('Servers')}}</span>
-                            <span class="info-box-number">{{$serverCount}}</span>
+                            <span class="info-box-number">{{$counters['servers']->total}}</span>
                         </div>
                         <!-- /.info-box-content -->
                     </div>
@@ -63,7 +63,7 @@
 
                         <div class="info-box-content">
                             <span class="info-box-text">{{__('Users')}}</span>
-                            <span class="info-box-number">{{$userCount}}</span>
+                            <span class="info-box-number">{{$counters['users']}}</span>
                         </div>
                         <!-- /.info-box-content -->
                     </div>
@@ -77,7 +77,7 @@
 
                         <div class="info-box-content">
                             <span class="info-box-text">{{__('Total')}} {{CREDITS_DISPLAY_NAME}}</span>
-                            <span class="info-box-number">{{$creditCount}}</span>
+                            <span class="info-box-number">{{$counters['credits']}}</span>
                         </div>
                         <!-- /.info-box-content -->
                     </div>
@@ -90,7 +90,7 @@
 
                         <div class="info-box-content">
                             <span class="info-box-text">{{__('Payments')}}</span>
-                            <span class="info-box-number">{{$paymentCount}}</span>
+                            <span class="info-box-number">{{$counters['payments']}}</span>
                         </div>
                         <!-- /.info-box-content -->
                     </div>
@@ -121,19 +121,19 @@
                                 <tbody>
                                 <tr>
                                     <td>{{__('Locations')}}</td>
-                                    <td>{{$locationCount}}</td>
+                                    <td>{{$counters['locations']}}</td>
                                 </tr>
                                 <tr>
                                     <td>{{__('Nodes')}}</td>
-                                    <td>{{$nodeCount}}</td>
+                                    <td>{{$nodes->count()}}</td>
                                 </tr>
                                 <tr>
                                     <td>{{__('Nests')}}</td>
-                                    <td>{{$nestCount}}</td>
+                                    <td>{{$counters['nests']}}</td>
                                 </tr>
                                 <tr>
                                     <td>{{__('Eggs')}}</td>
-                                    <td>{{$eggCount}}</td>
+                                    <td>{{$counters['eggs']}}</td>
                                 </tr>
                                 </tbody>
                             </table>
@@ -143,6 +143,58 @@
                         </div>
                     </div>
                 </div>
+                <div class="col-md-6">
+                    <div class="card">
+                        <div class="card-header">
+                            <div class="d-flex justify-content-between">
+                                <div class="card-title ">
+                                    <span><i class="fas fa-file-invoice-dollar mr-2"></i>{{__('Individual nodes')}}</span>
+                                </div>
+                            </div>
+                        </div>
+                        <div class="card-body py-1">
+                            @if ($perPageLimit)
+                                <div class="alert alert-danger m-2">
+                                    <h5><i class="icon fas fa-exclamation-circle"></i>{{ __('Error!') }}</h5>
+                                    <p class="">
+                                        {{ __('You reached the Pterodactyl perPage limit. Please make sure to set it higher than your server count.') }}<br>
+                                        {{ __('You can do that in settings.') }}
+                                    </p>
+                                </div>
+                            @endif
+                            <table class="table">
+                                <thead>
+                                <tr>
+                                    <th>{{__('ID')}}</th>
+                                    <th>{{__('Node')}}</th>
+                                    <th>{{__('Server count')}}</th>
+                                    <th>{{__('Resource usage')}}</th>
+                                    <th>{{CREDITS_DISPLAY_NAME . ' ' . __('Usage')}}</th>
+                                </tr>
+                                </thead>
+                                <tbody>
+                                    @foreach($nodes as $nodeID => $node)
+                                        <tr>
+                                            <td>{{$nodeID}}</td>
+                                            <td>{{$node->name}}</td>
+                                            <td>{{$node->activeServers}}/{{$node->totalServers}}</td>
+                                            <td>{{$node->usagePercent}}%</td>
+                                            <td>{{$node->activeEarnings}}/{{$node->totalEarnings}}</td>
+                                        </tr>
+                                    @endforeach
+                                </tbody>
+                                <tfoot>
+                                    <tr>
+                                        <td colspan="2"><span style="float: right; font-weight: 700">{{__('Total')}} ({{__('active')}}/{{__('total')}}):</span></td>
+                                        <td>{{$counters['servers']->active}}/{{$counters['servers']->total}}</td>
+                                        <td>{{$counters['totalUsagePercent']}}%</td>
+                                        <td>{{$counters['earnings']->active}}/{{$counters['earnings']->total}}</td>
+                                    </tr>
+                                </tfoot>
+                            </table>
+                        </div>
+                    </div>
+                </div>
             </div>
 
         </div>

+ 11 - 0
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>
                     </div>
+                    <div class="custom-control mb-3 p-0">
+                        <div class="col m-0 p-0 d-flex justify-content-between align-items-center">
+                            <label for="per-page-limit">{{ __('Pterodactyl API perPage limit') }}</label>
+                            <i data-toggle="popover" data-trigger="hover" data-html="true"
+                                data-content="{{ __('The Pterodactyl API perPage limit. It is necessary to set it higher than your server count.') }}"
+                                class="fas fa-info-circle"></i>
+                        </div>
+                        <input x-model="per-page-limit" id="per-page-limit" name="per-page-limit" type="number"
+                            value="{{ config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT') }}"
+                            class="form-control @error('per-page-limit') is-invalid @enderror" required>
+                    </div>
                     <div class="custom-control p-0 mb-3">
                         <div class="col m-0 p-0 d-flex justify-content-between align-items-center">
                             <label for="pterodactyl-api-key">{{ __('Pterodactyl API Key') }}</label>