Optimize the SQL queries that finds unsent echomail,netmail and files
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user