Optimise Zone::class to identify region/hosts/hubs

This commit is contained in:
Deon George 2024-05-27 10:47:42 +10:00
parent 65b2a2d519
commit 800593d034
2 changed files with 67 additions and 33 deletions

View File

@ -57,18 +57,16 @@ class DomainController extends Controller
*/
public function api_hosts(Zone $o,int $region): Collection
{
$oo = Address::where('role',Address::NODE_NC)
->where('zone_id',$o->id)
->when($region,function($query,$region) { return $query->where('region_id',$region); })
->when((! $region),function($query) use ($region) { return $query->where('region_id',0); })
->where('point_id',0)
->FTNorder()
->with(['system'])
->get();
return $oo->map(function($item) {
return ['id'=>$item->host_id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)];
});
return $o->hosts
->filter(fn($item)=>$item->role_id === Address::NODE_NC)
->filter(fn($item)=>$region ? ($item->region_id === $region) : TRUE)
->map(function($item) {
return [
'id'=>$item->host_id,
'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)
];
})
->values();
}
/**
@ -80,15 +78,16 @@ class DomainController extends Controller
*/
public function api_hubs(Zone $o,int $host): Collection
{
$oo = Address::where('role',Address::NODE_HC)
->where('zone_id',$o->id)
->when($host,function($query,$host) { return $query->where('host_id',$host)->where('node_id','<>',0); })
->with(['system'])
->get();
return $oo->map(function($item) {
return ['id'=>$item->id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)];
});
return $o->hubs
->filter(fn($item)=>$item->role_id === Address::NODE_HC)
->filter(fn($item)=>$host ? ($item->host_id === $host) : TRUE)
->map(function($item) {
return [
'id'=>$item->id,
'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)
];
})
->values();
}
/**
@ -99,18 +98,15 @@ class DomainController extends Controller
*/
public function api_regions(Zone $o): Collection
{
$oo = Address::where('role',Address::NODE_RC)
->where('zone_id',$o->id)
->where('node_id',0)
->where('point_id',0)
->orderBy('region_id')
->active()
->with(['system'])
->get();
return $oo->map(function($item) {
return ['id'=>$item->region_id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->location)];
});
return $o->regions
->filter(fn($item)=>$item->role_id === Address::NODE_RC)
->map(function($item) {
return [
'id'=>$item->region_id,
'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->location)
];
})
->values();
}
public function view(Domain $o)

View File

@ -36,6 +36,44 @@ class Zone extends Model
return $this->belongsTo(Domain::class);
}
public function hosts()
{
return $this->hasMany(Address::class)
->where(function($query) {
return $query
->where(fn($q)=>$q->where('node_id',0)->where('point_id',0))
->orWhere('role',Address::NODE_HC);
})
->FTNorder()
->with(['system','zone.domain']);
}
public function hubs()
{
// @todo we should be able to add to this query, to count children of an address by using a group by?
return $this->hasMany(Address::class)
->where(function($query) {
return $query
->where(fn($q)=>$q->where('point_id',0))
->orWhere('role',Address::NODE_HC);
})
->FTNorder()
->with(['system','zone.domain']);
}
public function regions()
{
return $this->hasMany(Address::class)
->where(function($query) {
return $query
->where(fn($q)=>$q->where('node_id',0)->where('point_id',0))
->orWhere('role',Address::NODE_RC);
})
->orderBy('region_id')
->active()
->with(['system','zone.domain']);
}
public function system()
{
return $this->belongsTo(System::class);