Optimise our use of items waiting and queries used. We are now using a single consistent query for each resource.

This commit is contained in:
Deon George 2023-12-16 23:22:23 +11:00
parent 7af67de2a8
commit 7b9ab388d8
5 changed files with 80 additions and 44 deletions

View File

@ -47,12 +47,12 @@ class HubStats extends Dynamic
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(
Address::UncollectedEchomail()
Address::UncollectedEchomailTotal()
->where('echomails.created_at','<',$date)
->union(Address::UncollectedNetmail()
->union(Address::UncollectedNetmailTotal()
->where('netmails.created_at','<',$date)
)
->union(Address::UncollectedFiles()
->union(Address::UncollectedFilesTotal()
->where('files.created_at','<',$date)
),'a')
->where('systems.active',true)

View File

@ -71,6 +71,9 @@ class Tic extends FTNBase
case 'file':
return $this->{$key};
case 'name':
return $this->file->name;
default:
return parent::__get($key);
}

View File

@ -263,12 +263,12 @@ class HomeController extends Controller
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(
Address::UncollectedEchomail()
Address::UncollectedEchomailTotal()
->where('echomails.created_at','<',$this->yesterdayEOD())
->union(Address::UncollectedNetmail()
->union(Address::UncollectedNetmailTotal()
->where('netmails.created_at','<',$this->yesterdayEOD())
)
->union(Address::UncollectedFiles()
->union(Address::UncollectedFilesTotal()
->where('files.created_at','<',$this->yesterdayEOD())
),'a')
->where('systems.active',true)

View File

@ -39,7 +39,7 @@ class MailSend #implements ShouldQueue
DB::raw('sum(a.uncollected_netmail) as uncollected_netmail'),
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(Address::UncollectedEchomail()->union(Address::UncollectedNetmail())->union(Address::UncollectedFiles()),'a')
->from(Address::UncollectedEchomailTotal()->union(Address::UncollectedNetmailTotal())->union(Address::UncollectedFilesTotal()),'a')
->where('systems.active',true)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)

View File

@ -98,15 +98,21 @@ class Address extends Model
* @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');
}
public function scopeUncollectedEchomailTotal($query)
{
return $query
->select(['addresses.id','zone_id','host_id','node_id','point_id','system_id',DB::raw('count(*) as uncollected_echomail'),DB::raw('0 as uncollected_netmail'),DB::raw('0 as uncollected_files')])
->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');
->UncollectedEchomail();
}
/**
@ -118,7 +124,6 @@ class Address extends Model
public function scopeUncollectedFiles($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(*) as uncollected_files')])
->join('file_seenby',['file_seenby.address_id'=>'addresses.id'])
->join('files',['files.id'=>'file_seenby.file_id'])
->whereNotNull('export_at')
@ -127,24 +132,39 @@ class Address extends Model
->groupBy('addresses.id');
}
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(*) 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');
}
/**
* Return a list of addresses and the amount of uncollected netmail
*
* @param $query
* @return mixed
*/
public function scopeUncollectedNetmail($query)
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(*) as uncollected_netmail'),DB::raw('0 as uncollected_files')])
->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('netmails.deleted_at')
->groupBy('addresses.id');
->UncollectedNetmail();
}
/* RELATIONS */
@ -634,12 +654,17 @@ class Address extends Model
*
* @return Collection
*/
public function echomailWaiting(): Collection
public function echomailWaiting(int $max=NULL): Collection
{
return $this->echomails()
->whereNull('sent_at')
->whereNotNull('export_at')
$echomails = $this
->UncollectedEchomail()
->select('echomails.id')
->where('addresses.id',$this->id)
->when($max,function($query) use ($max) { return $query->limit($max); })
->groupBy(['echomails.id'])
->get();
return Echomail::whereIn('id',$echomails->pluck('id'))->get();
}
/**
@ -672,17 +697,20 @@ class Address extends Model
$s = Setup::findOrFail(config('app.id'));
if (($x=$this->echomailWaiting())
->count())
{
$num = self::UncollectedEchomail()
->select('echomails.id')
->where('addresses.id',$this->id)
->groupBy(['echomails.id'])
->get();
if ($num->count()) {
// Limit to max messages
Log::info(sprintf('%s:= Got [%d] echomails for [%s] for sending',self::LOGKEY,$x->count(),$this->ftn));
Log::info(sprintf('%s:= Got [%d] echomails for [%s] for sending',self::LOGKEY,$num->count(),$this->ftn));
if ($x->count() > $s->msgs_pkt) {
$x = $x->take($s->msgs_pkt);
Log::alert(sprintf('%s:= Only sending [%d] echomails for [%s]',self::LOGKEY,$x->count(),$this->ftn));
}
if ($num->count() > $s->msgs_pkt)
Log::notice(sprintf('%s:= Only sending [%d] echomails for [%s]',self::LOGKEY,$s->msgs_pkt,$this->ftn));
$x = $this->echomailWaiting($s->msgs_pkt);
$pkt = $this->getPacket($x);
if ($pkt && $pkt->count() && $update)
@ -804,14 +832,14 @@ class Address extends Model
*/
public function netmailWaiting(): Collection
{
return Netmail::whereIn('tftn_id',(($x=$this->children()) ? $x->pluck('id') : collect())->push($this->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_at')
$netmails = $this
->UncollectedNetmail()
->select('netmails.id')
->where('addresses.id',$this->id)
->groupBy(['netmails.id'])
->get();
return Netmail::whereIn('id',$netmails->pluck('id'))->get();
}
/**
@ -821,12 +849,17 @@ class Address extends Model
*/
public function netmailAlertWaiting(): Collection
{
return Netmail::where('tftn_id',$this->id)
$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))
->whereNull('sent_at')
->select('netmails.id')
->where('addresses.id',$this->id)
->groupBy(['netmails.id'])
->get();
return Netmail::whereIn('id',$netmails->pluck('id'))->get();
}
/**