Some interface SQL performance improvements

This commit is contained in:
Deon George 2024-05-11 00:00:58 +10:00
parent cd2efbd1d4
commit 4d13199848
8 changed files with 65 additions and 53 deletions

View File

@ -59,7 +59,7 @@ class SystemController extends Controller
return redirect()->to('system'); 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') return view('system.addedit')
->with('action',$o->exists ? 'update' : 'create') ->with('action',$o->exists ? 'update' : 'create')

View File

@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -39,6 +40,7 @@ use App\Traits\ScopeActive;
class Address extends Model class Address extends Model
{ {
use ScopeActive,SoftDeletes; use ScopeActive,SoftDeletes;
const CACHE_KEY = 15;
private const LOGKEY = 'MA-'; private const LOGKEY = 'MA-';
@ -796,37 +798,39 @@ class Address extends Model
*/ */
public function children(): Collection public function children(): Collection
{ {
// If we are a point, our parent is the boss return Cache::remember(sprintf('children-%d',$this->id),self::CACHE_KEY,function() {
switch ($this->role_id) { // If we are a point, our parent is the boss
case self::NODE_NN: // Normal Nodes -> Points switch ($this->role_id) {
return $this->nodes_point; case self::NODE_NN: // Normal Nodes -> Points
return $this->nodes_point;
case self::NODE_HC: // Hubs -> Normal Nodes case self::NODE_HC: // Hubs -> Normal Nodes
return $this->nodes_hub; return $this->nodes_hub;
case self::NODE_NC: // Nets -> Normal Nodes, excluding Hub's Nodes case self::NODE_NC: // Nets -> Normal Nodes, excluding Hub's Nodes
return $this->nodes_net->diff($this return $this->nodes_net->diff($this
->nodes_net ->nodes_net
->filter(function($item) { return $item->role_id === Address::NODE_HC; }) ->filter(function($item) { return $item->role_id === Address::NODE_HC; })
->transform(function($item) { return $item->children(); }) ->transform(function($item) { return $item->children(); })
->flatten()); ->flatten());
case self::NODE_RC: // Regions, excluding NC's Nodes case self::NODE_RC: // Regions, excluding NC's Nodes
return $this->nodes_region->diff($this return $this->nodes_region->diff($this
->nodes_region ->nodes_region
->filter(function($item) { return $item->role_id === Address::NODE_NC; }) ->filter(function($item) { return $item->role_id === Address::NODE_NC; })
->transform(function($item) { return $item->nodes_net; }) ->transform(function($item) { return $item->nodes_net; })
->flatten()); ->flatten());
case self::NODE_ZC: // Zones, excluding RC's Nodes case self::NODE_ZC: // Zones, excluding RC's Nodes
return $this->nodes_zone->diff($this return $this->nodes_zone->diff($this
->nodes_zone ->nodes_zone
->filter(function($item) { return $item->role_id === Address::NODE_RC; }) ->filter(function($item) { return $item->role_id === Address::NODE_RC; })
->transform(function($item) { return $item->nodes_region; }) ->transform(function($item) { return $item->nodes_region; })
->flatten()); ->flatten());
} }
return new Collection; return new Collection;
});
} }
/** /**
@ -851,6 +855,7 @@ class Address extends Model
->join('zones',['zones.id'=>'addresses.zone_id']) ->join('zones',['zones.id'=>'addresses.zone_id'])
->where('addresses.id','<>',$this->id) ->where('addresses.id','<>',$this->id)
->where('domain_id',$this->zone->domain_id) ->where('domain_id',$this->zone->domain_id)
->with(['zone.domain'])
->active() ->active()
->FTNorder() ->FTNorder()
->get(); ->get();
@ -938,36 +943,38 @@ class Address extends Model
*/ */
private function ftn_role(): ?int 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 we have a point address, we're a point
if ($this->point_id) if ($this->point_id)
$role = self::NODE_POINT; $role = self::NODE_POINT;
// If we have a node_id, we're either a Node or a Hub // If we have a node_id, we're either a Node or a Hub
elseif ($this->node_id) { elseif ($this->node_id) {
$role = ($this->nodes_hub->count()) $role = ($this->nodes_hub->count())
? self::NODE_HC ? self::NODE_HC
: ((($this->role & Address::NODE_ALL) === self::NODE_HC) ? self::NODE_HC : self::NODE_NN); : ((($this->role & Address::NODE_ALL) === self::NODE_HC) ? self::NODE_HC : self::NODE_NN);
// point_id and node_id are zero // 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 // 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) { } elseif (($this->region_id !== $this->host_id) && $this->host_id) {
$role = self::NODE_NC; $role = self::NODE_NC;
// point_id and node_id are zero // point_id and node_id are zero
} elseif (($this->region_id === $this->host_id) && $this->host_id) { } elseif (($this->region_id === $this->host_id) && $this->host_id) {
$role = self::NODE_RC; $role = self::NODE_RC;
// point_id and node_id are zero // point_id and node_id are zero
} elseif (($this->region_id === $this->host_id) && (! $this->host_id)) { } elseif (($this->region_id === $this->host_id) && (! $this->host_id)) {
$role = self::NODE_ZC; $role = self::NODE_ZC;
} }
if (is_null($role)) 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)); 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;
});
} }
/** /**

View File

@ -73,6 +73,7 @@ class System extends Model
->where('zones.active',TRUE) ->where('zones.active',TRUE)
->where('domains.active',TRUE) ->where('domains.active',TRUE)
->orderBy('domains.name') ->orderBy('domains.name')
->with(['zone.domain'])
->FTNorder() ->FTNorder()
->orderBy('role','ASC'); ->orderBy('role','ASC');
} }

View File

@ -28,7 +28,7 @@
</thead> </thead>
<tbody> <tbody>
@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)
<tr> <tr>
<td>{{ $oo->zone->domain->name }}</td> <td>{{ $oo->zone->domain->name }}</td>
<td><a href="{{ url('system/addedit',[$oo->system_id]) }}">{{ $oo->system_id }}</a>@if($oo->system->hold)<span class="float-end"><i class="text-danger bi bi-stop-circle"></i></span>@endif</td> <td><a href="{{ url('system/addedit',[$oo->system_id]) }}">{{ $oo->system_id }}</a>@if($oo->system->hold)<span class="float-end"><i class="text-danger bi bi-stop-circle"></i></span>@endif</td>

View File

@ -1,6 +1,7 @@
@if(($x=\App\Models\Zone::active() @if(($x=\App\Models\Zone::active()
->whereIn('id',$o->sessions->pluck('pivot.zone_id')) ->whereIn('id',$o->sessions->pluck('pivot.zone_id'))
->orderBy('zone_id') ->orderBy('zone_id')
->with(['domain'])
->get())->count()) ->get())->count())
<form class="needs-validation" method="post" action="{{ url('system/echoarea',$o->id) }}" novalidate> <form class="needs-validation" method="post" action="{{ url('system/echoarea',$o->id) }}" novalidate>

View File

@ -1,6 +1,7 @@
@if(($x=\App\Models\Zone::active() @if(($x=\App\Models\Zone::active()
->whereIn('id',$o->sessions->pluck('pivot.zone_id')) ->whereIn('id',$o->sessions->pluck('pivot.zone_id'))
->orderBy('zone_id') ->orderBy('zone_id')
->with(['domain'])
->get())->count()) ->get())->count())
<form class="needs-validation" method="post" action="{{ url('system/filearea',$o->id) }}" novalidate> <form class="needs-validation" method="post" action="{{ url('system/filearea',$o->id) }}" novalidate>

View File

@ -2,6 +2,7 @@
@if(($x=\App\Models\Zone::active() @if(($x=\App\Models\Zone::active()
->whereIn('id',$o->zones->pluck('id')) ->whereIn('id',$o->zones->pluck('id'))
->whereNotIn('id',$o->sessions->pluck('id')) ->whereNotIn('id',$o->sessions->pluck('id'))
->with(['domain'])
->get())->count()) ->get())->count())
<form class="needs-validation" method="post" action="{{ url('system/session/add',$o->id) }}" novalidate> <form class="needs-validation" method="post" action="{{ url('system/session/add',$o->id) }}" novalidate>

View File

@ -1,4 +1,5 @@
@php @php
use App\Classes\FTN\Packet;
use App\Models\{Mailer,User}; use App\Models\{Mailer,User};
@endphp @endphp
@ -177,7 +178,7 @@
<div class="input-group"> <div class="input-group">
<span class="input-group-text"><i class="bi bi-ui-radios"></i></span> <span class="input-group-text"><i class="bi bi-ui-radios"></i></span>
<select class="form-select @error('pkt_type') is-invalid @enderror" id="pkt_type" name="pkt_type" @cannot($action,$o)readonly @endcannot> <select class="form-select @error('pkt_type') is-invalid @enderror" id="pkt_type" name="pkt_type" @cannot($action,$o)readonly @endcannot>
@foreach (\App\Classes\FTN\Packet::PACKET_TYPES as $type => $class) @foreach (Packet::PACKET_TYPES as $type => $class)
<option value="{{ $type }}" @if(old('pkt_type',$o->pkt_type ?: config('fido.packet_default')) === $type)selected @endif>{{ $type }}</option> <option value="{{ $type }}" @if(old('pkt_type',$o->pkt_type ?: config('fido.packet_default')) === $type)selected @endif>{{ $type }}</option>
@endforeach @endforeach
</select> </select>
@ -349,7 +350,7 @@
Last Poll: Last Poll:
</div> </div>
<div class="col-8"> <div class="col-8">
<strong class="highlight">{{ ($x=$o->logs->where('originate',TRUE)->last())?->created_at ?: 'Never' }}</strong> <strong class="highlight">{{ ($x=$o->logs()->where('originate',TRUE)->orderBy('created_at','DESC')->limit(1)->single())?->created_at ?: 'Never' }}</strong>
</div> </div>
</div> </div>