Minor optimisations when editing and viewing domains

This commit is contained in:
Deon George 2024-11-05 00:28:34 +11:00
parent 05b7c7904a
commit 9fb7611335
6 changed files with 91 additions and 73 deletions

View File

@ -186,7 +186,7 @@ class AddressIdle implements ShouldQueue
}
if ($result->count())
Notification::route('echomail',$this->do->nodestatusarea)->notify(new AbsentNodes($result));
Notification::route('echomail',$this->do->nodestatus_echoarea)->notify(new AbsentNodes($result));
}
private function old(Domain $do,int $days,int $flags=0,Address $ao=NULL): Collection

View File

@ -11,9 +11,16 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Casts\CompressedStringOrNull;
use App\Models\Casts\CompressedStringOrNull;
use App\Traits\{QueryCacheableConfig,ScopeActive};
/**
* This represents an FTN Domain.
*
* We are either a member of the domain (because we have an AKA in it) or NOT.
*
* The assumption is, if we are a member of the domain, we receive/send mail to an uplink and possibly downlinks
*/
class Domain extends Model
{
use HasFactory,QueryCacheableConfig,ScopeActive;
@ -28,14 +35,16 @@ class Domain extends Model
/* SCOPES */
/**
* Only query active records
* A domain is public (visible), if the user is an admin or, the domain is marked public)
*/
public function scopePublic($query)
{
$user = Auth::user();
return $query
->when(((! $user) || (! $user->isAdmin())),function($query) { return $query->where('public',TRUE)->active(); });
->active()
->when((! $user) || (! $user->isAdmin()),
fn($query)=>$query->where('public',TRUE));
}
/* RELATIONS */
@ -43,13 +52,13 @@ class Domain extends Model
public function echoareas()
{
return $this->hasMany(Echoarea::class)
->orderBY('name');
->orderBy('name');
}
public function fileareas()
{
return $this->hasMany(Filearea::class)
->orderBY('name');
->orderBy('name');
}
public function nodelist_filearea()
@ -57,7 +66,7 @@ class Domain extends Model
return $this->belongsTo(Filearea::class);
}
public function nodestatusarea()
public function nodestatus_echoarea()
{
return $this->belongsTo(Echoarea::class,'nodestatus_id');
}
@ -65,29 +74,37 @@ class Domain extends Model
public function zones()
{
return $this->hasMany(Zone::class)
->select(['id','zone_id','domain_id','active']);
->select(['id','zone_id','domain_id','active'])
->orderBy('zone_id');
}
/* ATTRIBUTES */
/**
* We can accept applications if we have an address in the domain
*
* @return bool
* @throws \Exception
*/
public function getCanAcceptAppAttribute(): bool
{
return ($x=our_address($this))
&& $x->count()
&& $this->active
return $this->isManaged()
&& $this->accept_app
&& Auth::id()
&& $this->userHasSystemsNotInNet(Auth::user())->count();
&& Auth::id();
}
public function getHomePageAttribute(?string $value): string
{
//0xFD2FB528
return $this->castAttribute('homepage',$value) ?: 'No available information at the moment.';
}
/* METHODS */
/**
* Show count of messages by day/week/month/all stats for each echoarea in this domain
*
* @return Collection
*/
public function echoarea_stats(): Collection
{
return Cache::remember(md5(sprintf('%d-%s',$this->id,__METHOD__)),self::CACHE_TIME,function() {
@ -110,6 +127,12 @@ class Domain extends Model
});
}
/**
* Show daily total of messages by echoarea in this domain
*
* @param Collection|NULL $systems
* @return Collection
*/
public function echoarea_total_daily(Collection $systems=NULL): Collection
{
return Cache::remember(md5(sprintf('%d-%s',$this->id,$systems?->pluck('id')->join(','))),self::CACHE_TIME,function() use ($systems) {
@ -120,7 +143,7 @@ class Domain extends Model
->join('echomails',['echomails.echoarea_id'=>'echoareas.id'])
->where('domains.id',$this->id)
->where('echomails.datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('echomails.fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')); })
->when($systems?->count(),fn($query)=>$query->whereIn('echomails.fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')))
->groupBy(['echoareas.name','echoareas.show','date'])
->orderBy('echoareas.name')
->orderBy('date')
@ -128,6 +151,9 @@ class Domain extends Model
});
}
/**
* Show count of files by week/month/all status for each filearea in this domain
*/
public function filearea_stats()
{
return Cache::remember(md5(sprintf('%d-%s',$this->id,__METHOD__)),self::CACHE_TIME,function() {
@ -157,28 +183,6 @@ class Domain extends Model
*/
public function isManaged(): bool
{
return ($x=our_address())
&& $x->pluck('zone.domain')
->pluck('id')
->contains($this->id);
}
/**
* Work out which of the users systems are not in this domain
*
* @param User $o
* @return Collection
*/
public function userHasSystemsNotInNet(User $o): Collection
{
$o->load('systems.akas.zone');
$result = collect();
foreach ($o->systems->filter(function($item) { return $item->active; }) as $so) {
if (! $so->akas->pluck('zone')->unique('domain_id')->pluck('domain_id')->contains($this->id))
$result->push($so);
}
return $result;
return our_address($this)->count() > 0;
}
}

View File

@ -115,9 +115,10 @@ class System extends Model
public function sessions()
{
return $this->belongsToMany(Zone::class)
->select(['id','zones.zone_id','domain_id','active'])
->select(['zones.id','zones.zone_id','domain_id','zones.active'])
->join('domains',['domains.id'=>'zones.domain_id'])
->withPivot(['sespass','pktpass','ticpass','fixpass','zt_ipv4','zt_ipv6','default'])
->dontCache();
->orderBy('domains.name');
}
/**
@ -301,6 +302,13 @@ class System extends Model
}
}
public function inDomain(Domain $o): bool
{
return $this->addresses
->filter(fn($item)=>$item->zone->domain_id === $o->id)
->count() > 0;
}
/**
* Return the system's address in the same zone
* This function can filter based on the address type needed.

View File

@ -1,14 +1,12 @@
@use(App\Models\Echoarea)
<!-- $o=Domain::class -->
@extends('layouts.app')
@section('htmlheader_title')
@if($o->exists) Update @else Add @endif Domain
@endsection
@php
use App\Models\Echoarea;
@endphp
@section('content')
<form class="needs-validation" method="post" novalidate>
@csrf
@ -107,6 +105,7 @@ use App\Models\Echoarea;
</div>
<div class="col-2">
@if ($o->isManaged())
<label class="form-label">Applications</label>
<div class="input-group">
<div class="btn-group" role="group">
@ -117,6 +116,7 @@ use App\Models\Echoarea;
<label class="btn btn-outline-danger" for="accept_app_no">No</label>
</div>
</div>
@endif
</div>
@if ($o->nodelist_filename)
@ -136,6 +136,7 @@ use App\Models\Echoarea;
<div class="col-8">
</div>
<div class="col-4">
@if ($o->isManaged())
<label for="nodestatus_id" class="form-label">Echoarea Node Status</label>
<div class="input-group has-validation">
<span class="input-group-text"><i class="bi bi-journal-text"></i></span>
@ -152,6 +153,7 @@ use App\Models\Echoarea;
</span>
<span class="input-helper">Add a <a href="{{ url('echoarea/addedit') }}">NEW Echoarea</a>. This echoarea is used to send node status messages.</span>
</div>
@endif
</div>
</div>

View File

@ -1,3 +1,4 @@
@use(App\Models\Address)
@use(Illuminate\Mail\Markdown)
<!-- $o=Domain::class -->
@ -176,7 +177,7 @@
</div>
<!-- Sign up -->
@if ($o->can_accept_app)
@if ($o->can_accept_app && ($x=Auth::user()->systems->filter(fn($item)=>$item->address && (! $item->inDomain($o))))->count())
<div class="accordion-item">
<h3 class="accordion-header">
<span class="accordion-button collapsed" id="signup" data-bs-toggle="collapse" data-bs-target="#collapse_signup" aria-expanded="false" aria-controls="collapse_signup">Join Network</span>
@ -184,7 +185,10 @@
<div id="collapse_signup" class="accordion-collapse collapse" aria-labelledby="signup" data-bs-parent="#accordion_homepage">
<div class="accordion-body">
<p>Your system(s) <strong class="highlight">{!! $o->userHasSystemsNotInNet(Auth::user())->pluck('name')->join('</strong>, <strong class="highlight">') !!}</strong> can join this network.</p>
<p>Your system(s) <strong class="highlight">{!! $x->pluck('name')->sort()->join('</strong>, <strong class="highlight">') !!}</strong> can join this network.</p>
@if (($x=Auth::user()->systems->filter(fn($item)=>(! $item->address) && (! $item->inDomain($o))))->count())
<p><small>Your other systems(s) <strong class="highlight">{!! $x->pluck('name')->sort()->join('</strong>, <strong class="highlight">') !!}</strong> cant use this process, because they are missing network details.</small></p>
@endif
<p>
If you want to join it/them to this network, make sure:
</p>
@ -206,7 +210,7 @@
<li>Enjoy!</li>
</ul>
<button class="btn btn-success">Lets Do It</button>
<button class="btn btn-success">Lets Do It</button> <small><-- NOT READY YET</small>
</div>
</div>
</div>

View File

@ -1,8 +1,8 @@
<!-- $o=System::class -->
@use(App\Models\Address)
@use(App\Models\Echomail)
@use(App\Models\File)
@use(App\Models\Netmail)
<!-- $o=System::class -->
@extends('layouts.app')
@ -212,7 +212,7 @@
</thead>
<tbody>
@foreach ($o->sessions->sortBy('zone_id') as $oo)
@foreach ($o->sessions as $oo)
<tr>
<td>{{ $oo->zone_id }}<span>@</span>{{ $oo->domain->name }}</td>
<td style="text-align: center;">