From 4d1319984819d2928e781d289cbd936ff3f94e0a Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 11 May 2024 00:00:58 +1000 Subject: [PATCH] Some interface SQL performance improvements --- app/Http/Controllers/SystemController.php | 2 +- app/Models/Address.php | 105 ++++++++++-------- app/Models/System.php | 1 + resources/views/system/ours.blade.php | 2 +- .../system/widget/form-echoarea.blade.php | 1 + .../system/widget/form-filearea.blade.php | 1 + .../system/widget/form-session.blade.php | 1 + .../views/system/widget/system.blade.php | 5 +- 8 files changed, 65 insertions(+), 53 deletions(-) diff --git a/app/Http/Controllers/SystemController.php b/app/Http/Controllers/SystemController.php index f2c0fab..561c9c8 100644 --- a/app/Http/Controllers/SystemController.php +++ b/app/Http/Controllers/SystemController.php @@ -59,7 +59,7 @@ class SystemController extends Controller return redirect()->to('system'); } - $o->load(['addresses.zone.domain','addresses.system','sessions.domain','sessions.systems']); + $o->load(['addresses.zone.domain','addresses.nodes_hub','addresses.system','sessions.domain','sessions.systems']); return view('system.addedit') ->with('action',$o->exists ? 'update' : 'create') diff --git a/app/Models/Address.php b/app/Models/Address.php index 568ec00..3d1b50b 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\QueryException; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; @@ -39,6 +40,7 @@ use App\Traits\ScopeActive; class Address extends Model { use ScopeActive,SoftDeletes; + const CACHE_KEY = 15; private const LOGKEY = 'MA-'; @@ -796,37 +798,39 @@ class Address extends Model */ public function children(): Collection { - // If we are a point, our parent is the boss - switch ($this->role_id) { - case self::NODE_NN: // Normal Nodes -> Points - return $this->nodes_point; + return Cache::remember(sprintf('children-%d',$this->id),self::CACHE_KEY,function() { + // If we are a point, our parent is the boss + switch ($this->role_id) { + case self::NODE_NN: // Normal Nodes -> Points + return $this->nodes_point; - case self::NODE_HC: // Hubs -> Normal Nodes - return $this->nodes_hub; + case self::NODE_HC: // Hubs -> Normal Nodes + return $this->nodes_hub; - case self::NODE_NC: // Nets -> Normal Nodes, excluding Hub's Nodes - return $this->nodes_net->diff($this - ->nodes_net - ->filter(function($item) { return $item->role_id === Address::NODE_HC; }) - ->transform(function($item) { return $item->children(); }) - ->flatten()); + case self::NODE_NC: // Nets -> Normal Nodes, excluding Hub's Nodes + return $this->nodes_net->diff($this + ->nodes_net + ->filter(function($item) { return $item->role_id === Address::NODE_HC; }) + ->transform(function($item) { return $item->children(); }) + ->flatten()); - case self::NODE_RC: // Regions, excluding NC's Nodes - return $this->nodes_region->diff($this - ->nodes_region - ->filter(function($item) { return $item->role_id === Address::NODE_NC; }) - ->transform(function($item) { return $item->nodes_net; }) - ->flatten()); + case self::NODE_RC: // Regions, excluding NC's Nodes + return $this->nodes_region->diff($this + ->nodes_region + ->filter(function($item) { return $item->role_id === Address::NODE_NC; }) + ->transform(function($item) { return $item->nodes_net; }) + ->flatten()); - case self::NODE_ZC: // Zones, excluding RC's Nodes - return $this->nodes_zone->diff($this - ->nodes_zone - ->filter(function($item) { return $item->role_id === Address::NODE_RC; }) - ->transform(function($item) { return $item->nodes_region; }) - ->flatten()); - } + case self::NODE_ZC: // Zones, excluding RC's Nodes + return $this->nodes_zone->diff($this + ->nodes_zone + ->filter(function($item) { return $item->role_id === Address::NODE_RC; }) + ->transform(function($item) { return $item->nodes_region; }) + ->flatten()); + } - return new Collection; + return new Collection; + }); } /** @@ -851,6 +855,7 @@ class Address extends Model ->join('zones',['zones.id'=>'addresses.zone_id']) ->where('addresses.id','<>',$this->id) ->where('domain_id',$this->zone->domain_id) + ->with(['zone.domain']) ->active() ->FTNorder() ->get(); @@ -938,36 +943,38 @@ class Address extends Model */ private function ftn_role(): ?int { - $role = NULL; + return Cache::remember(sprintf('ftn_role-%d',$this->id),self::CACHE_KEY,function() { + $role = NULL; - // If we have a point address, we're a point - if ($this->point_id) - $role = self::NODE_POINT; + // If we have a point address, we're a point + if ($this->point_id) + $role = self::NODE_POINT; - // If we have a node_id, we're either a Node or a Hub - elseif ($this->node_id) { - $role = ($this->nodes_hub->count()) - ? self::NODE_HC - : ((($this->role & Address::NODE_ALL) === self::NODE_HC) ? self::NODE_HC : self::NODE_NN); + // If we have a node_id, we're either a Node or a Hub + elseif ($this->node_id) { + $role = ($this->nodes_hub->count()) + ? self::NODE_HC + : ((($this->role & Address::NODE_ALL) === self::NODE_HC) ? self::NODE_HC : self::NODE_NN); - // point_id and node_id are zero - // If our region_id !== host_id, and are not zero, and node_id/point_id === 0, we are an NC - } elseif (($this->region_id !== $this->host_id) && $this->host_id) { - $role = self::NODE_NC; + // point_id and node_id are zero + // If our region_id !== host_id, and are not zero, and node_id/point_id === 0, we are an NC + } elseif (($this->region_id !== $this->host_id) && $this->host_id) { + $role = self::NODE_NC; - // point_id and node_id are zero - } elseif (($this->region_id === $this->host_id) && $this->host_id) { - $role = self::NODE_RC; + // point_id and node_id are zero + } elseif (($this->region_id === $this->host_id) && $this->host_id) { + $role = self::NODE_RC; - // point_id and node_id are zero - } elseif (($this->region_id === $this->host_id) && (! $this->host_id)) { - $role = self::NODE_ZC; - } + // point_id and node_id are zero + } elseif (($this->region_id === $this->host_id) && (! $this->host_id)) { + $role = self::NODE_ZC; + } - if (is_null($role)) - Log::alert(sprintf('%s:! Address ROLE [%d] could not be determined for [%s]',self::LOGKEY,($this->role & Address::NODE_ALL),$this->ftn)); + if (is_null($role)) + Log::alert(sprintf('%s:! Address ROLE [%d] could not be determined for [%s]',self::LOGKEY,($this->role & Address::NODE_ALL),$this->ftn)); - return $role; + return $role; + }); } /** diff --git a/app/Models/System.php b/app/Models/System.php index 305dbb2..2338099 100644 --- a/app/Models/System.php +++ b/app/Models/System.php @@ -73,6 +73,7 @@ class System extends Model ->where('zones.active',TRUE) ->where('domains.active',TRUE) ->orderBy('domains.name') + ->with(['zone.domain']) ->FTNorder() ->orderBy('role','ASC'); } diff --git a/resources/views/system/ours.blade.php b/resources/views/system/ours.blade.php index 74fd27a..675b23b 100644 --- a/resources/views/system/ours.blade.php +++ b/resources/views/system/ours.blade.php @@ -28,7 +28,7 @@ - @foreach (\App\Models\SystemZone::select('*')->with(['system','zone.domain'])->get() as $oo) + @foreach (\App\Models\SystemZone::select('*')->with(['system.addresses.zone.domain','zone.domain'])->get() as $oo) {{ $oo->zone->domain->name }} {{ $oo->system_id }}@if($oo->system->hold)@endif diff --git a/resources/views/system/widget/form-echoarea.blade.php b/resources/views/system/widget/form-echoarea.blade.php index a673e71..cc584f8 100644 --- a/resources/views/system/widget/form-echoarea.blade.php +++ b/resources/views/system/widget/form-echoarea.blade.php @@ -1,6 +1,7 @@ @if(($x=\App\Models\Zone::active() ->whereIn('id',$o->sessions->pluck('pivot.zone_id')) ->orderBy('zone_id') + ->with(['domain']) ->get())->count())
diff --git a/resources/views/system/widget/form-filearea.blade.php b/resources/views/system/widget/form-filearea.blade.php index 5722850..eeeb70b 100644 --- a/resources/views/system/widget/form-filearea.blade.php +++ b/resources/views/system/widget/form-filearea.blade.php @@ -1,6 +1,7 @@ @if(($x=\App\Models\Zone::active() ->whereIn('id',$o->sessions->pluck('pivot.zone_id')) ->orderBy('zone_id') + ->with(['domain']) ->get())->count()) diff --git a/resources/views/system/widget/form-session.blade.php b/resources/views/system/widget/form-session.blade.php index 1b88cf7..bfc8bda 100644 --- a/resources/views/system/widget/form-session.blade.php +++ b/resources/views/system/widget/form-session.blade.php @@ -2,6 +2,7 @@ @if(($x=\App\Models\Zone::active() ->whereIn('id',$o->zones->pluck('id')) ->whereNotIn('id',$o->sessions->pluck('id')) + ->with(['domain']) ->get())->count()) diff --git a/resources/views/system/widget/system.blade.php b/resources/views/system/widget/system.blade.php index 47da7c7..4020be1 100644 --- a/resources/views/system/widget/system.blade.php +++ b/resources/views/system/widget/system.blade.php @@ -1,4 +1,5 @@ @php + use App\Classes\FTN\Packet; use App\Models\{Mailer,User}; @endphp @@ -177,7 +178,7 @@
@@ -349,7 +350,7 @@ Last Poll:
- {{ ($x=$o->logs->where('originate',TRUE)->last())?->created_at ?: 'Never' }} + {{ ($x=$o->logs()->where('originate',TRUE)->orderBy('created_at','DESC')->limit(1)->single())?->created_at ?: 'Never' }}