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:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user