Optimisations to uncollected Items, which fix mail:send, which was only being triggered for mail unsent from yesterday

This commit is contained in:
Deon George 2023-12-08 15:16:49 +11:00
parent e56ecaa999
commit 5a74386f5a
3 changed files with 33 additions and 28 deletions

View File

@ -250,34 +250,47 @@ class HomeController extends Controller
$r = Address::select([
'a.id',
'addresses.system_id',
'addresses.zone_id',
'a.system_id',
'a.zone_id',
'addresses.region_id',
'addresses.host_id',
'addresses.node_id',
'addresses.point_id',
'a.host_id',
'a.node_id',
'a.point_id',
'addresses.hub_id',
'addresses.role',
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
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::UncollectedEchomail()
->where('echomails.created_at','<',$this->yesterdayEOD())
->union(Address::UncollectedNetmail()
->where('netmails.created_at','<',$this->yesterdayEOD())
)
->union(Address::UncollectedFiles()
->where('files.created_at','<',$this->yesterdayEOD())
),'a')
->where('systems.active',true)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->when(! ($x=Auth::user()) || (! $x->isAdmin()),function($query) { return $query->where('domains.public',TRUE); })
->join('addresses',['addresses.id'=>'a.id'])
->join('systems',['systems.id'=>'addresses.system_id'])
->join('systems',['systems.id'=>'a.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->ftnOrder()
->groupBy('addresses.system_id','a.id','addresses.zone_id','addresses.region_id','addresses.host_id','addresses.node_id','addresses.point_id','addresses.hub_id','addresses.role')
->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain']);
return view('status')
->with('date',$date)
->with('uncollected',$r->get());
}
private function yesterdayEOD(): Carbon
{
return Carbon::now()->yesterday()->endOfday();
}
}

View File

@ -27,12 +27,12 @@ class MailSend #implements ShouldQueue
{
$u = Address::select([
'a.id',
'addresses.system_id',
'addresses.zone_id',
'a.system_id',
'a.zone_id',
'addresses.region_id',
'addresses.host_id',
'addresses.node_id',
'addresses.point_id',
'a.host_id',
'a.node_id',
'a.point_id',
'addresses.hub_id',
'addresses.role',
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
@ -45,8 +45,8 @@ class MailSend #implements ShouldQueue
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->join('addresses',['addresses.id'=>'a.id'])
->join('systems',['systems.id'=>'addresses.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('systems',['systems.id'=>'a.system_id'])
->join('zones',['zones.id'=>'a.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->where(function($query) {
return $query->whereNull('autohold')
@ -63,7 +63,7 @@ class MailSend #implements ShouldQueue
}
);
})
->groupBy('addresses.system_id','a.id','addresses.zone_id','addresses.region_id','addresses.host_id','addresses.node_id','addresses.point_id','addresses.hub_id','addresses.role')
->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain'])
->get();

View File

@ -99,13 +99,12 @@ class Address extends Model
public function scopeUncollectedEchomail($query)
{
return $query
->select(['addresses.id',DB::raw('count(*) as uncollected_echomail'),DB::raw('0 as uncollected_netmail'),DB::raw('0 as uncollected_files')])
->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')
->where('echomails.created_at','<',$this->yesterdayEOD())
->groupBy('addresses.id');
}
@ -118,13 +117,12 @@ class Address extends Model
public function scopeUncollectedFiles($query)
{
return $query
->select(['addresses.id',DB::raw('0 as uncollected_echomail'),DB::raw('0 as uncollected_netmail'),DB::raw('count(*) as uncollected_files')])
->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')
->whereNull('sent_at')
->whereNull('files.deleted_at')
->where('files.created_at','<',$this->yesterdayEOD())
->groupBy('addresses.id');
}
@ -137,7 +135,7 @@ class Address extends Model
public function scopeUncollectedNetmail($query)
{
return $query
->select(['addresses.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'])
->where(function($query) {
return $query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
@ -145,7 +143,6 @@ class Address extends Model
})
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('netmails.deleted_at')
->where('netmails.created_at','<',$this->yesterdayEOD())
->groupBy('addresses.id');
}
@ -947,9 +944,4 @@ class Address extends Model
{
return ($this->exists && ($x=$this->system->sessions->where('id',$this->zone_id)->first())) ? ($x->pivot->{$type} ?: '') : NULL;
}
private function yesterdayEOD(): Carbon
{
return Carbon::now()->yesterday()->endOfday();
}
}