HoststatusQuery: Rename baseQuery to select

This commit is contained in:
Eric Lippmann 2014-04-15 18:06:37 +02:00
parent 8b471e29e5
commit cb812b59a8
1 changed files with 79 additions and 85 deletions

View File

@ -29,12 +29,10 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query; namespace Icinga\Module\Monitoring\Backend\Ido\Query;
/**
*
*/
class HoststatusQuery extends IdoQuery class HoststatusQuery extends IdoQuery
{ {
protected $allowCustomVars = true; protected $allowCustomVars = true;
protected $columnMap = array( protected $columnMap = array(
'hosts' => array( 'hosts' => array(
'host' => 'ho.name1 COLLATE latin1_general_ci', 'host' => 'ho.name1 COLLATE latin1_general_ci',
@ -135,12 +133,14 @@ class HoststatusQuery extends IdoQuery
'services_unknown_unhandled' => 'SUM(CASE WHEN ss.current_state = 3 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) = 0 THEN 1 ELSE 0 END)', 'services_unknown_unhandled' => 'SUM(CASE WHEN ss.current_state = 3 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) = 0 THEN 1 ELSE 0 END)',
), ),
); );
protected $aggregateColumnIdx = array( protected $aggregateColumnIdx = array(
'services_cnt' => true, 'services_cnt' => true,
'services_problem' => true, 'services_problem' => true,
'services_problem_handled' => true, 'services_problem_handled' => true,
'services_problem_unhandled' => true, 'services_problem_unhandled' => true,
); );
protected $hcgSub; protected $hcgSub;
public function getDefaultColumns() public function getDefaultColumns()
@ -151,18 +151,18 @@ class HoststatusQuery extends IdoQuery
protected function joinBaseTables() protected function joinBaseTables()
{ {
// TODO: Shall we always add hostobject? // TODO: Shall we always add hostobject?
$this->baseQuery = $this->db->select()->from( $this->select->from(
array('ho' => $this->prefix . 'objects'), array('ho' => $this->prefix . 'objects'),
array() array()
)->join( )->join(
array('hs' => $this->prefix . 'hoststatus'), array('hs' => $this->prefix . 'hoststatus'),
'ho.' . $this->object_id . ' = hs.host_object_id AND ho.is_active = 1 AND ho.objecttype_id = 1', 'ho.' . $this->object_id . ' = hs.host_object_id AND ho.is_active = 1 AND ho.objecttype_id = 1',
array() array()
)->join( )->join(
array('h' => $this->prefix . 'hosts'), array('h' => $this->prefix . 'hosts'),
'hs.host_object_id = h.host_object_id', 'hs.host_object_id = h.host_object_id',
array() array()
); );
$this->joinedVirtualTables = array( $this->joinedVirtualTables = array(
'hosts' => true, 'hosts' => true,
'hoststatus' => true, 'hoststatus' => true,
@ -181,25 +181,25 @@ class HoststatusQuery extends IdoQuery
protected function joinServices() protected function joinServices()
{ {
$this->baseQuery->join( $this->select->join(
array('s' => $this->prefix . 'services'), array('s' => $this->prefix . 'services'),
's.host_object_id = h.host_object_id', 's.host_object_id = h.host_object_id',
array() array()
)->join( )->join(
array('so' => $this->prefix . 'objects'), array('so' => $this->prefix . 'objects'),
"so.$this->object_id = s.service_object_id AND so.is_active = 1", 'so.' . $this->object_id . ' = s.service_object_id AND so.is_active = 1',
array() array()
)->joinLeft( )->joinLeft(
array('ss' => $this->prefix . 'servicestatus'), array('ss' => $this->prefix . 'servicestatus'),
"so.$this->object_id = ss.service_object_id", 'so.' . $this->object_id . ' = ss.service_object_id',
array() array()
); );
foreach ($this->getColumns() as $col) { foreach ($this->getColumns() as $col) {
$real = $this->aliasToColumnName($col); $real = $this->aliasToColumnName($col);
if (substr($real, 0, 4) === 'SUM(') { if (substr($real, 0, 4) === 'SUM(') {
continue; continue;
} }
$this->baseQuery->group($real); $this->select->group($real);
} }
$this->useSubqueryCount = true; $this->useSubqueryCount = true;
} }
@ -215,41 +215,38 @@ class HoststatusQuery extends IdoQuery
protected function joinServiceHostgroups() protected function joinServiceHostgroups()
{ {
$this->baseQuery->join( $this->select->join(
array('hgm' => $this->prefix . 'hostgroup_members'), array('hgm' => $this->prefix . 'hostgroup_members'),
'hgm.host_object_id = s.host_object_id', 'hgm.host_object_id = s.host_object_id',
array() array()
)->join( )->join(
array('hg' => $this->prefix . 'hostgroups'), array('hg' => $this->prefix . 'hostgroups'),
'hgm.hostgroup_id = hg.' . $this->hostgroup_id, 'hgm.hostgroup_id = hg.' . $this->hostgroup_id,
array() array()
)->join( )->join(
array('hgo' => $this->prefix . 'objects'), array('hgo' => $this->prefix . 'objects'),
'hgo.' . $this->object_id . ' = hg.hostgroup_object_id' 'hgo.' . $this->object_id . ' = hg.hostgroup_object_id'
. ' AND hgo.is_active = 1', . ' AND hgo.is_active = 1',
array() array()
); );
return $this; return $this;
} }
protected function joinHostHostgroups() protected function joinHostHostgroups()
{ {
$this->baseQuery->join( $this->select->join(
array('hgm' => $this->prefix . 'hostgroup_members'), array('hgm' => $this->prefix . 'hostgroup_members'),
'hgm.host_object_id = h.host_object_id', 'hgm.host_object_id = h.host_object_id',
array() array()
)->join( )->join(
array('hg' => $this->prefix . 'hostgroups'), array('hg' => $this->prefix . 'hostgroups'),
"hgm.hostgroup_id = hg.$this->hostgroup_id", 'hgm.hostgroup_id = hg.' . $this->hostgroup_id,
array() array()
)->join( )->join(
array('hgo' => $this->prefix . 'objects'), array('hgo' => $this->prefix . 'objects'),
'hgo.' . $this->object_id . ' = hg.hostgroup_object_id' 'hgo.' . $this->object_id . ' = hg.hostgroup_object_id' . ' AND hgo.is_active = 1',
. ' AND hgo.is_active = 1', array()
array() );
);
return $this; return $this;
} }
@ -259,20 +256,19 @@ class HoststatusQuery extends IdoQuery
array('hcgc' => $this->prefix . 'host_contactgroups'), array('hcgc' => $this->prefix . 'host_contactgroups'),
array('host_name' => 'ho.name1') array('host_name' => 'ho.name1')
)->join( )->join(
array('cgo' => $this->prefix . 'objects'), array('cgo' => $this->prefix . 'objects'),
'hcg.contactgroup_object_id = cgo.' . $this->object_id 'hcg.contactgroup_object_id = cgo.' . $this->object_id . ' AND cgo.is_active = 1',
. ' AND cgo.is_active = 1', array()
array() )->join(
)->join( array('h' => $this->prefix . 'hosts'),
array('h' => $this->prefix . 'hosts'), 'hcg.host_id = h.host_id',
'hcg.host_id = h.host_id', array()
array() )->join(
)->join( array('ho' => $this->prefix . 'objects'),
array('ho' => $this->prefix . 'objects'), 'h.host_object_id = ho.' . $this->object_id . ' AND ho.is_active = 1',
'h.host_object_id = ho.' . $this->object_id . ' AND ho.is_active = 1', array()
array() );
); $this->select->join(
$this->baseQuery->join(
array('hcg' => $this->hcgSub), array('hcg' => $this->hcgSub),
'hcg.host_name = ho.name1', 'hcg.host_name = ho.name1',
array() array()
@ -295,55 +291,53 @@ class HoststatusQuery extends IdoQuery
protected function joinContactgroups() protected function joinContactgroups()
{ {
$this->hcgSub = $this->createContactgroupFilterSubselect(); $this->hcgSub = $this->createContactgroupFilterSubselect();
$this->baseQuery->join( $this->select->join(
array('hcg' => $this->hcgSub), array('hcg' => $this->hcgSub),
'hcg.object_id = ho.object_id', 'hcg.object_id = ho.object_id',
array() array()
); );
return $this; return $this;
} }
protected function createContactgroupFilterSubselect() protected function createContactgroupFilterSubselect()
{ {
die((string)$this->db->select()->distinct()->from( die((string) $this->db->select()->distinct()->from(
array('hcg' => $this->prefix . 'host_contactgroups'), array('hcg' => $this->prefix . 'host_contactgroups'),
array('object_id' => 'ho.object_id') array('object_id' => 'ho.object_id')
)->join( )->join(
array('cgo' => $this->prefix . 'objects'), array('cgo' => $this->prefix . 'objects'),
'hcg.contactgroup_object_id = cgo.' . $this->object_id 'hcg.contactgroup_object_id = cgo.' . $this->object_id
. ' AND cgo.is_active = 1', . ' AND cgo.is_active = 1',
array() array()
)->join( )->join(
array('h' => $this->prefix . 'hosts'), array('h' => $this->prefix . 'hosts'),
'hcg.host_id = h.host_id', 'hcg.host_id = h.host_id',
array() array()
)->join( )->join(
array('ho' => $this->prefix . 'objects'), array('ho' => $this->prefix . 'objects'),
'h.host_object_id = ho.' . $this->object_id . ' AND ho.is_active = 1', 'h.host_object_id = ho.' . $this->object_id . ' AND ho.is_active = 1',
array() array()
)); ));
} }
protected function joinServicegroups() protected function joinServicegroups()
{ {
// TODO: Only hosts with services having such servicegroups // TODO: Only hosts with services having such servicegroups
$this->requireVirtualTable('services'); $this->requireVirtualTable('services');
$this->baseQuery->join( $this->select->join(
array('sgm' => $this->prefix . 'servicegroup_members'), array('sgm' => $this->prefix . 'servicegroup_members'),
'sgm.service_object_id = s.service_object_id', 'sgm.service_object_id = s.service_object_id',
array() array()
)->join( )->join(
array('sg' => $this->prefix . 'servicegroups'), array('sg' => $this->prefix . 'servicegroups'),
'sgm.servicegroup_id = sg.' . $this->servicegroup_id, 'sgm.servicegroup_id = sg.' . $this->servicegroup_id,
array() array()
)->join( )->join(
array('sgo' => $this->prefix . 'objects'), array('sgo' => $this->prefix . 'objects'),
'sgo.' . $this->object_id . ' = sg.servicegroup_object_id' 'sgo.' . $this->object_id . ' = sg.servicegroup_object_id'
. ' AND sgo.is_active = 1', . ' AND sgo.is_active = 1',
array() array()
); );
return $this; return $this;
} }
} }