From 8a71cdf56875080527e725a0a0dc3d47136348d8 Mon Sep 17 00:00:00 2001 From: IceToast <> Date: Sun, 22 Jan 2023 15:25:03 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=E2=9A=A1=EF=B8=8F=20Less=20DB=20queries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/OverViewController.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Admin/OverViewController.php b/app/Http/Controllers/Admin/OverViewController.php index ee46989d..6d8d63a9 100644 --- a/app/Http/Controllers/Admin/OverViewController.php +++ b/app/Http/Controllers/Admin/OverViewController.php @@ -138,11 +138,16 @@ class OverViewController extends Controller array_push($pteroNodeIds, $pteroNode['attributes']['id']); } $nodes = collect(); - foreach ($DBnodes = Node::query()->get() as $DBnode) { //gets all node information and prepares the structure + + //gets all node information and prepares the structure + foreach ($DBnodes = Node::query()->get() as $DBnode) { $nodeId = $DBnode['id']; + + //Check if node exists on pterodactyl too, if not, skip if (!in_array($nodeId, $pteroNodeIds)) { continue; - } //Check if node exists on pterodactyl too, if not, skip + } + $nodes->put($nodeId, collect()); $nodes[$nodeId]->name = $DBnode['name']; $pteroNode = Pterodactyl::getNode($nodeId); @@ -156,11 +161,11 @@ class OverViewController extends Controller } $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 + // Calculate server and earnings information + foreach (Pterodactyl::getServers() as $server) { $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 = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->with('product')->first()) { + $price = $CPServer->product->price; if (!$CPServer->suspended) { $counters['earnings']->active += $price; $counters['servers']->active++;