Optimize the SQL queries that finds unsent echomail,netmail and files

This commit is contained in:
2025-04-16 23:36:55 +10:00
parent 128e333deb
commit 3f0e17e20b
8 changed files with 257 additions and 284 deletions

View File

@@ -450,115 +450,6 @@ class Address extends Model
->orderBy('point_id');
}
/**
* Return a list of addresses and the amount of uncollected echomail
*
* @param $query
* @return mixed
*/
public function scopeUncollectedEchomail($query)
{
return $query
->join('echomail_seenby',['echomail_seenby.address_id'=>'addresses.id'])
->join('echomails',['echomails.id'=>'echomail_seenby.echomail_id'])
->whereNotNull('export_at')
->whereNull('sent_at')
->whereNull('echomails.deleted_at')
->groupBy('addresses.id')
->dontCache();
}
public function scopeUncollectedEchomailTotal($query)
{
return $query
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('count(addresses.id) as uncollected_echomail'),
DB::raw('0 as uncollected_netmail'),
DB::raw('0 as uncollected_files'),
])
->UncollectedEchomail();
}
/**
* Return a list of addresses and the amount of uncollected files
*
* @param $query
* @return mixed
*/
public function scopeUncollectedFiles($query)
{
return $query
->join('file_seenby',['file_seenby.address_id'=>'addresses.id'])
->join('files',['files.id'=>'file_seenby.file_id'])
->whereNotNull('export_at')
->whereNull('sent_at')
->whereNull('files.deleted_at')
->groupBy('addresses.id')
->dontCache();
}
public function scopeUncollectedFilesTotal($query)
{
return $query
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('0 as uncollected_echomail'),
DB::raw('0 as uncollected_netmail'),
DB::raw('count(addresses.id) as uncollected_files')
])
->UncollectedFiles();
}
public function scopeUncollectedNetmail($query)
{
return $query
->join('netmails',['netmails.tftn_id'=>'addresses.id'])
->where(function($query) {
return $query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL));
})
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_pkt')
->whereNull('sent_at')
->whereNull('netmails.deleted_at')
->groupBy('addresses.id')
->dontCache();
}
/**
* Return a list of addresses and the amount of uncollected netmail
*
* @param $query
* @return mixed
*/
public function scopeUncollectedNetmailTotal($query)
{
return $query
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('0 as uncollected_echomail'),
DB::raw('count(addresses.id) as uncollected_netmail'),
DB::raw('0 as uncollected_files')
])
->UncollectedNetmail();
}
/* RELATIONS */
public function domain()
@@ -1077,55 +968,55 @@ class Address extends Model
}
/**
* Echomail waiting to be sent to this system
* Echomail waiting to be sent to this address
*
* @return Builder
* @return Collection
*/
public function echomailWaiting(): Builder
public function echomailWaiting(): Collection
{
return Echomail::select('echomails.*')
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
return Echomail::Uncollected()
->where('address_id',$this->id)
->whereNull('echomails.deleted_at')
->whereNotNull('export_at')
->whereNull('sent_at')
->orderby('id')
->with([
'tagline:id,value',
'tearline:id,value',
'origin:id,value',
'echoarea:id,name,domain_id',
'echoarea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
]);
->get();
}
/**
* Files waiting to be sent to this system
* Count of echomail waiting to be sent to this address
*
* @return int
*/
public function echomailWaitingCount(): int
{
return Echomail::Uncollected()
->where('address_id',$this->id)
->count();
}
/**
* Files waiting to be sent to this address
*
* @return Collection
*/
public function filesWaiting(): Collection
{
return File::select('files.*')
->join('file_seenby',['file_seenby.file_id'=>'files.id'])
return File::Uncollected()
->where('address_id',$this->id)
->whereNull('files.deleted_at')
->whereNotNull('export_at')
->whereNull('sent_at')
->orderby('id')
->with([
'filearea:id,name,domain_id',
'filearea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
])
->get();
}
/**
* Count of files waiting to be sent to this address
*
* @return int
*/
public function filesWaitingCount(): int
{
return File::Uncollected()
->where('address_id',$this->id)
->count();
}
/**
* Work out what role this FTN should have
*
@@ -1265,43 +1156,40 @@ class Address extends Model
*/
public function netmailWaiting(): Builder
{
// Addresses that our downstream of this address, except anybody that has session details with us
$ours = our_nodes($this->zone->domain)->pluck('id');
$addresses = $this->downlinks()
->filter(fn($item)=>(! $ours->contains($item->id)))
->merge($this->system->match($this->zone,Address::NODE_ALL));
$netmails = $this
->UncollectedNetmail()
->select('netmails.id')
->whereIn('addresses.id',$addresses->pluck('id'))
->groupBy(['netmails.id'])
->get();
return Netmail::whereIn('id',$netmails->pluck('id'));
return Netmail::Uncollected()
->whereIn('tftn_id',$this->downlinks()
->add($this)
->pluck('id'));
}
/**
* Netmail alerts waiting to be sent to this system
* Count of echomail waiting to be sent to this address
*
* @return int
*/
public function netmailWaitingCount(): int
{
return Netmail::Uncollected()
->whereIn('tftn_id',$this->downlinks()
->add($this)
->pluck('id'))
->count();
}
/**
* Netmail alerts waiting to be sent to this address
*
* @return Builder
* @throws \Exception
* @note The packet password to use is on the subject line for these alerts
* @todo To Test
*/
public function netmailAlertWaiting(): Builder
{
$netmails = $this
->UncollectedNetmail()
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL))
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_PKTPASSWD))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->select('netmails.id')
->whereIn('addresses.id',$this->downlinks()->add($this)->pluck('id'))
->groupBy(['netmails.id'])
->get();
return Netmail::whereIn('id',$netmails->pluck('id'));
return Netmail::UncollectedAlerts()
->whereIn('tftn_id',$this->downlinks()
->add($this)
->pluck('id'));
}
public function nodes(): Collection

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
@@ -279,6 +280,33 @@ final class Echomail extends Model implements Packet
});
}
/* SCOPES */
/**
* Base query to find uncollected echomails
*
* @param Builder $query
* @return Builder
*/
public function scopeUncollected(Builder $query): Builder
{
return $this
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
->whereNull('echomails.deleted_at')
->whereNotNull('export_at')
->whereNull('sent_at')
->with([
'tagline:id,value',
'tearline:id,value',
'origin:id,value',
'echoarea:id,name,domain_id',
'echoarea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
]);
}
/* RELATIONS */
public function echoarea()

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
@@ -174,6 +175,30 @@ class File extends Model
});
}
/* SCOPES */
/**
* Base query to find uncollected files
*
* @param Builder $query
* @return Builder
*/
public function scopeUncollected(Builder $query): Builder
{
return $query
->join('file_seenby',['file_seenby.file_id'=>'files.id'])
->whereNotNull('export_at')
->whereNull('sent_at')
->whereNull('files.deleted_at')
->with([
'filearea:id,name,domain_id',
'filearea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
]);
}
/* RELATIONS */
public function filearea()

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
@@ -10,6 +11,7 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Models\Casts\{CompressedStringOrNull,CollectionOrNull,UTF8StringOrNull};
use App\Models\Pivots\ViaPivot;
@@ -237,6 +239,37 @@ final class Netmail extends Model implements Packet
});
}
/* SCOPES */
/**
* Base query to find uncollected netmails
*
* @param Builder $query
* @return Builder
*/
public function scopeUncollected(Builder $query): Builder
{
return $query
->where(fn($query)=>
$query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL)))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_pkt')
->whereNull('sent_at')
->whereNull('netmails.deleted_at');
}
public function scopeUncollectedAlerts(Builder $query): Builder
{
return $query
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL))
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_PKTPASSWD))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_pkt')
->whereNull('sent_at')
->whereNull('netmails.deleted_at');
}
/* RELATIONS */
public function path()