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

View File

@ -36,6 +36,44 @@ class Zone extends Model
return $this->belongsTo(Domain::class); 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() public function system()
{ {
return $this->belongsTo(System::class); return $this->belongsTo(System::class);