EventHistory: Add support for object restrictions

refs #9009
This commit is contained in:
Johannes Meyer 2015-06-12 12:52:07 +02:00
parent c35be4023b
commit 528df1b26a
2 changed files with 38 additions and 104 deletions

View File

@ -7,16 +7,19 @@ use Zend_Db_Select;
use Icinga\Data\Filter\Filter;
/**
* Query for event history
* Query for event history records
*/
class EventHistoryQuery extends IdoQuery
{
/**
* {@inheritdoc}
*/
protected $useSubqueryCount = true;
/**
* Subqueries used for the event history query
*
* @type IdoQuery[]
*
* @see EventHistoryQuery::joinBaseTables() For the used subqueries.
*/
protected $subQueries = array();
@ -30,33 +33,18 @@ class EventHistoryQuery extends IdoQuery
'cnt_soft_state' => "SUM(CASE eh.type WHEN 'hard_state' THEN 1 ELSE 0 END)",
'cnt_downtime_start' => "SUM(CASE eh.type WHEN 'dt_start' THEN 1 ELSE 0 END)",
'cnt_downtime_end' => "SUM(CASE eh.type WHEN 'dt_end' THEN 1 ELSE 0 END)",
'host' => 'eho.name1 COLLATE latin1_general_ci',
'service' => 'eho.name2 COLLATE latin1_general_ci',
'host_name' => 'eho.name1',
'service_description' => 'eho.name2',
'host_name' => 'eh.host_name',
'service_description' => 'eh.service_description',
'object_type' => 'eh.object_type',
'timestamp' => 'eh.timestamp',
'state' => 'eh.state',
'output' => 'eh.output',
'type' => 'eh.type'
),
'hostgroups' => array(
'hostgroup' => 'hgo.name1 COLLATE latin1_general_ci',
'hostgroup_name' => 'hgo.name1'
),
'hosts' => array(
'host_display_name' => 'CASE WHEN sh.display_name IS NOT NULL THEN sh.display_name ELSE h.display_name END'
),
'services' => array(
'service_display_name' => 's.display_name'
'type' => 'eh.type',
'host_display_name' => 'eh.host_display_name',
'service_display_name' => 'eh.service_display_name'
)
);
/**
* {@inheritdoc}
*/
protected $useSubqueryCount = true;
/**
* {@inheritdoc}
*/
@ -64,11 +52,15 @@ class EventHistoryQuery extends IdoQuery
{
$columns = array(
'timestamp',
'object_id',
'type',
'output',
'type',
'state',
'object_type'
'object_type',
'object_id',
'host_name',
'service_description',
'host_display_name',
'service_display_name'
);
$this->subQueries = array(
$this->createSubQuery('Statehistory', $columns),
@ -76,19 +68,11 @@ class EventHistoryQuery extends IdoQuery
$this->createSubQuery('Downtimeendhistory', $columns),
$this->createSubQuery('Commenthistory', $columns),
$this->createSubQuery('Commentdeletionhistory', $columns),
$this->createSubQuery('Notificationhistory', $columns)
$this->createSubQuery('Notification', $columns)
);
$sub = $this->db->select()->union($this->subQueries, Zend_Db_Select::SQL_UNION_ALL);
$this->select->from(
array('eho' => $this->prefix . 'objects'),
'*'
)->join(
array('eh' => $sub),
'eho.' . $this->object_id . ' = eh.' . $this->object_id . ' AND eho.is_active = 1',
array()
);
$this->joinedVirtualTables = array('eventhistory' => true);
$this->select->from(array('eh' => $sub), array());
$this->joinedVirtualTables['eventhistory'] = true;
}
/**
@ -102,17 +86,6 @@ class EventHistoryQuery extends IdoQuery
return parent::order($columnOrAlias, $dir);
}
/**
* {@inheritdoc}
*/
public function addFilter(Filter $filter)
{
foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter);
}
return $this;
}
/**
* {@inheritdoc}
*/
@ -126,60 +99,13 @@ class EventHistoryQuery extends IdoQuery
}
/**
* Join host groups
*
* @return $this
* {@inheritdoc}
*/
protected function joinHostgroups()
public function addFilter(Filter $filter)
{
$this->select->join(
array('hgm' => $this->prefix . 'hostgroup_members'),
'hgm.host_object_id = eho.object_id',
array()
)->join(
array('hg' => $this->prefix . 'hostgroups'),
'hgm.hostgroup_id = hg.' . $this->hostgroup_id,
array()
)->join(
array('hgo' => $this->prefix . 'objects'),
'hgo.' . $this->object_id. ' = hg.hostgroup_object_id' . ' AND hgo.is_active = 1',
array()
);
return $this;
}
/**
* Join hosts
*
* @return $this
*/
protected function joinHosts()
{
$this->select->joinLeft(
array('h' => $this->prefix . 'hosts'),
'h.host_object_id = eho.object_id',
array()
);
return $this;
}
/**
* Join services
*
* @return $this
*/
protected function joinServices()
{
$this->select->joinLeft(
array('s' => $this->prefix . 'services'),
's.service_object_id = eho.object_id',
array()
);
$this->select->joinLeft(
array('sh' => $this->prefix . 'hosts'),
'sh.host_object_id = s.host_object_id',
array()
);
foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter);
}
return $this;
}
}

View File

@ -22,17 +22,17 @@ class EventHistory extends DataView
'host_display_name',
'service_description',
'service_display_name',
'hostgroup_name',
'object_type',
'timestamp',
'state',
'attempt',
'max_attempts',
'output',
'type'
);
}
/**
* {@inheritdoc}
*/
public function getSortRules()
{
return array(
@ -43,8 +43,16 @@ class EventHistory extends DataView
);
}
/**
* {@inheritdoc}
*/
public function getFilterColumns()
{
return array('host', 'service', 'hostgroup');
return array(
'host', 'host_alias',
'hostgroup', 'hostgroup_alias', 'hostgroup_name',
'service',
'servicegroup', 'servicegroup_alias', 'servicegroup_name'
);
}
}