Minor optimisations when editing and viewing domains

This commit is contained in:
2024-11-05 00:28:34 +11:00
parent dc2e84386f
commit 60ea0afc98
6 changed files with 91 additions and 73 deletions

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.