monitoring: Use the host and service comment queries in the comment query

refs 
This commit is contained in:
Eric Lippmann 2015-05-29 15:35:39 +02:00
parent 82644b6122
commit 85c21b042d

@ -3,78 +3,126 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Zend_Db_Expr;
use Zend_Db_Select;
use Icinga\Data\Filter\Filter;
/**
* Query map for comments
* Query for host and service comments
*/
class CommentQuery extends IdoQuery
{
/**
* {@inheritdoc}
*/
protected $columnMap = array(
'comments' => array(
'comment_internal_id' => 'cm.internal_comment_id',
'comment_data' => 'cm.comment_data',
'comment_author' => 'cm.author_name COLLATE latin1_general_ci',
'comment_author_name' => 'cm.author_name',
'comment_timestamp' => 'UNIX_TIMESTAMP(cm.comment_time)',
'comment_type' => "CASE cm.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'downtime' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END",
'comment_is_persistent' => 'cm.is_persistent',
'comment_expiration' => 'CASE cm.expires WHEN 1 THEN UNIX_TIMESTAMP(cm.expiration_time) ELSE NULL END',
'comment_objecttype' => "CASE WHEN ho.object_id IS NOT NULL THEN 'host' ELSE CASE WHEN so.object_id IS NOT NULL THEN 'service' ELSE NULL END END",
'host' => 'CASE WHEN ho.name1 IS NULL THEN so.name1 ELSE ho.name1 END COLLATE latin1_general_ci',
'host_name' => 'CASE WHEN ho.name1 IS NULL THEN so.name1 ELSE ho.name1 END',
'service' => 'so.name2 COLLATE latin1_general_ci',
'service_description' => 'so.name2',
'service_host' => 'so.name1 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1'
'comment_author' => 'c.comment_author',
'comment_author_name' => 'c.comment_author_name',
'comment_data' => 'c.comment_data',
'comment_expiration' => 'c.comment_expiration',
'comment_internal_id' => 'c.comment_internal_id',
'comment_is_persistent' => 'c.comment_is_persistent',
'comment_timestamp' => 'c.comment_timestamp',
'comment_type' => 'c.comment_type',
'object_type' => 'c.object_type'
),
'hosts' => array(
'host_display_name' => 'CASE WHEN sh.display_name IS NOT NULL THEN sh.display_name ELSE h.display_name END'
'host_display_name' => 'c.host_display_name',
'host_name' => 'c.host_name',
'host_state' => 'c.host_state'
),
'services' => array(
'service_display_name' => 's.display_name'
'service_description' => 'c.service_description',
'service_display_name' => 'c.service_display_name',
'service_host_name' => 'c.service_host_name',
'service_state' => 'c.service_state'
)
);
protected function joinBaseTables()
{
$this->select->from(
array('cm' => $this->prefix . 'comments'),
array()
);
$this->select->joinLeft(
array('ho' => $this->prefix . 'objects'),
'cm.object_id = ho.object_id AND ho.is_active = 1 AND ho.objecttype_id = 1',
array()
);
$this->select->joinLeft(
array('so' => $this->prefix . 'objects'),
'cm.object_id = so.object_id AND so.is_active = 1 AND so.objecttype_id = 2',
array()
);
$this->joinedVirtualTables = array('comments' => true);
}
/**
* The union
*
* @var Zend_Db_Select
*/
protected $commentQuery;
protected function joinHosts()
/**
* Subqueries used for the comment query
*
* @var IdoQuery[]
*/
protected $subQueries = array();
/**
* {@inheritdoc}
*/
public function addFilter(Filter $filter)
{
$this->select->joinLeft(
array('h' => $this->prefix . 'hosts'),
'h.host_object_id = ho.object_id',
array()
);
foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter);
}
return $this;
}
/**
* {@inheritdoc}
*/
protected function joinBaseTables()
{
$this->commentQuery = $this->db->select();
$this->select->from(
array('c' => $this->commentQuery),
array()
);
$this->joinedVirtualTables['comments'] = true;
}
/**
* Join hosts
*/
protected function joinHosts()
{
$columns = array_keys($this->columnMap['comments'] + $this->columnMap['hosts']);
foreach (array_keys($this->columnMap['services']) as $column) {
$columns[$column] = new Zend_Db_Expr('NULL');
}
$hosts = $this->createSubQuery('hostcomment', $columns);
$this->subQueries[] = $hosts;
$this->commentQuery->union(array($hosts), Zend_Db_Select::SQL_UNION_ALL);
}
/**
* Join services
*/
protected function joinServices()
{
$this->select->joinLeft(
array('s' => $this->prefix . 'services'),
's.service_object_id = so.object_id',
array()
);
$this->select->joinLeft(
array('sh' => $this->prefix . 'hosts'),
'sh.host_object_id = s.host_object_id',
array()
);
$columns = array_keys($this->columnMap['comments'] + $this->columnMap['hosts'] + $this->columnMap['services']);
$services = $this->createSubQuery('servicecomment', $columns);
$this->subQueries[] = $services;
$this->commentQuery->union(array($services), Zend_Db_Select::SQL_UNION_ALL);
}
/**
* {@inheritdoc}
*/
public function order($columnOrAlias, $dir = null)
{
foreach ($this->subQueries as $sub) {
$sub->requireColumn($columnOrAlias);
}
return parent::order($columnOrAlias, $dir);
}
/**
* {@inheritdoc}
*/
public function where($condition, $value = null)
{
$this->requireColumn($condition);
foreach ($this->subQueries as $sub) {
$sub->where($condition, $value);
}
return $this;
}
}