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') DB::raw('sum(a.uncollected_files) as uncollected_files')
]) ])
->from( ->from(
Address::UncollectedEchomail() Address::UncollectedEchomailTotal()
->where('echomails.created_at','<',$date) ->where('echomails.created_at','<',$date)
->union(Address::UncollectedNetmail() ->union(Address::UncollectedNetmailTotal()
->where('netmails.created_at','<',$date) ->where('netmails.created_at','<',$date)
) )
->union(Address::UncollectedFiles() ->union(Address::UncollectedFilesTotal()
->where('files.created_at','<',$date) ->where('files.created_at','<',$date)
),'a') ),'a')
->where('systems.active',true) ->where('systems.active',true)

View File

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

View File

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

View File

@ -100,7 +100,6 @@ class Address extends Model
public function scopeUncollectedEchomail($query) public function scopeUncollectedEchomail($query)
{ {
return $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('echomail_seenby',['echomail_seenby.address_id'=>'addresses.id'])
->join('echomails',['echomails.id'=>'echomail_seenby.echomail_id']) ->join('echomails',['echomails.id'=>'echomail_seenby.echomail_id'])
->whereNotNull('export_at') ->whereNotNull('export_at')
@ -109,6 +108,13 @@ class Address extends Model
->groupBy('addresses.id'); ->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')])
->UncollectedEchomail();
}
/** /**
* Return a list of addresses and the amount of uncollected files * Return a list of addresses and the amount of uncollected files
* *
@ -118,7 +124,6 @@ class Address extends Model
public function scopeUncollectedFiles($query) public function scopeUncollectedFiles($query)
{ {
return $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('file_seenby',['file_seenby.address_id'=>'addresses.id'])
->join('files',['files.id'=>'file_seenby.file_id']) ->join('files',['files.id'=>'file_seenby.file_id'])
->whereNotNull('export_at') ->whereNotNull('export_at')
@ -127,24 +132,39 @@ class Address extends Model
->groupBy('addresses.id'); ->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 * Return a list of addresses and the amount of uncollected netmail
* *
* @param $query * @param $query
* @return mixed * @return mixed
*/ */
public function scopeUncollectedNetmail($query) public function scopeUncollectedNetmailTotal($query)
{ {
return $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')]) ->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']) ->UncollectedNetmail();
->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');
} }
/* RELATIONS */ /* RELATIONS */
@ -634,12 +654,17 @@ class Address extends Model
* *
* @return Collection * @return Collection
*/ */
public function echomailWaiting(): Collection public function echomailWaiting(int $max=NULL): Collection
{ {
return $this->echomails() $echomails = $this
->whereNull('sent_at') ->UncollectedEchomail()
->whereNotNull('export_at') ->select('echomails.id')
->where('addresses.id',$this->id)
->when($max,function($query) use ($max) { return $query->limit($max); })
->groupBy(['echomails.id'])
->get(); ->get();
return Echomail::whereIn('id',$echomails->pluck('id'))->get();
} }
/** /**
@ -672,17 +697,20 @@ class Address extends Model
$s = Setup::findOrFail(config('app.id')); $s = Setup::findOrFail(config('app.id'));
if (($x=$this->echomailWaiting()) $num = self::UncollectedEchomail()
->count()) ->select('echomails.id')
{ ->where('addresses.id',$this->id)
->groupBy(['echomails.id'])
->get();
if ($num->count()) {
// Limit to max messages // 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) { if ($num->count() > $s->msgs_pkt)
$x = $x->take($s->msgs_pkt); Log::notice(sprintf('%s:= Only sending [%d] echomails for [%s]',self::LOGKEY,$s->msgs_pkt,$this->ftn));
Log::alert(sprintf('%s:= Only sending [%d] echomails for [%s]',self::LOGKEY,$x->count(),$this->ftn));
}
$x = $this->echomailWaiting($s->msgs_pkt);
$pkt = $this->getPacket($x); $pkt = $this->getPacket($x);
if ($pkt && $pkt->count() && $update) if ($pkt && $pkt->count() && $update)
@ -804,14 +832,14 @@ class Address extends Model
*/ */
public function netmailWaiting(): Collection public function netmailWaiting(): Collection
{ {
return Netmail::whereIn('tftn_id',(($x=$this->children()) ? $x->pluck('id') : collect())->push($this->id)) $netmails = $this
->where(function($query) { ->UncollectedNetmail()
return $query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT)) ->select('netmails.id')
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL)); ->where('addresses.id',$this->id)
}) ->groupBy(['netmails.id'])
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_at')
->get(); ->get();
return Netmail::whereIn('id',$netmails->pluck('id'))->get();
} }
/** /**
@ -821,12 +849,17 @@ class Address extends Model
*/ */
public function netmailAlertWaiting(): Collection 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_LOCAL))
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_PKTPASSWD)) ->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_PKTPASSWD))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT)) ->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_at') ->select('netmails.id')
->where('addresses.id',$this->id)
->groupBy(['netmails.id'])
->get(); ->get();
return Netmail::whereIn('id',$netmails->pluck('id'))->get();
} }
/** /**