From 7358eefed88f56806fcba5e37218c6bd9add2295 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 11 Jun 2015 15:29:36 +0200 Subject: [PATCH] CommenthistoryQuery: Use subqueries to fetch host and service comments refs #9009 --- .../Backend/Ido/Query/CommenthistoryQuery.php | 162 ++++++++++++++---- 1 file changed, 132 insertions(+), 30 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php index 6b2f821e9..b8fdd72a4 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php @@ -3,49 +3,151 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; +use Zend_Db_Expr; +use Zend_Db_Select; +use Icinga\Data\Filter\Filter; + +/** + * Query for host and service comment history records + */ class CommenthistoryQuery extends IdoQuery { + /** + * {@inheritdoc} + */ protected $columnMap = array( 'commenthistory' => array( - 'state_time' => 'h.comment_time', - 'timestamp' => 'UNIX_TIMESTAMP(h.comment_time)', - 'raw_timestamp' => 'h.comment_time', - 'object_id' => 'h.object_id', - 'type' => "(CASE h.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'dt_comment' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END)", - 'state' => '(NULL)', - 'state_type' => '(NULL)', - 'output' => "('[' || h.author_name || '] ' || h.comment_data)", - 'attempt' => '(NULL)', - 'max_attempts' => '(NULL)', - - 'host' => 'o.name1 COLLATE latin1_general_ci', - 'service' => 'o.name2 COLLATE latin1_general_ci', - 'host_name' => 'o.name1', - 'service_description' => 'o.name2', - 'object_type' => "CASE WHEN o.objecttype_id = 1 THEN 'host' ELSE 'service' END" + 'object_type' => 'ch.object_type' + ), + 'history' => array( + 'type' => 'ch.type', + 'timestamp' => 'ch.timestamp', + 'object_id' => 'ch.object_id', + 'state' => 'ch.state', + 'output' => 'ch.output' + ), + 'hosts' => array( + 'host_display_name' => 'ch.host_display_name', + 'host_name' => 'ch.host_name' + ), + 'services' => array( + 'service_description' => 'ch.service_description', + 'service_display_name' => 'ch.service_display_name', + 'service_host_name' => 'ch.service_host_name' ) ); - public function whereToSql($col, $sign, $expression) - { - if ($col === 'UNIX_TIMESTAMP(h.comment_time)') { - return 'h.comment_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression)); - } else { - return parent::whereToSql($col, $sign, $expression); - } - } + /** + * The union + * + * @var Zend_Db_Select + */ + protected $commentHistoryQuery; + /** + * Subqueries used for the comment history query + * + * @var IdoQuery[] + */ + protected $subQueries = array(); + + /** + * Whether to additionally select all history columns + * + * @var bool + */ + protected $fetchHistoryColumns = false; + + /** + * {@inheritdoc} + */ protected function joinBaseTables() { + $this->commentHistoryQuery = $this->db->select(); $this->select->from( - array('o' => $this->prefix . 'objects'), - array() - )->join( - array('h' => $this->prefix . 'commenthistory'), - 'o.' . $this->object_id . ' = h.' . $this->object_id . ' AND o.is_active = 1 AND h.entry_type <> 2', + array('ch' => $this->commentHistoryQuery), array() ); - $this->joinedVirtualTables = array('commenthistory' => true); + $this->joinedVirtualTables['commenthistory'] = true; } + /** + * Join history related columns and tables + */ + protected function joinHistory() + { + // TODO: Ensure that one is selecting the history columns first... + $this->fetchHistoryColumns = true; + $this->requireVirtualTable('hosts'); + $this->requireVirtualTable('services'); + } + + /** + * Join hosts + */ + protected function joinHosts() + { + $columns = array_keys( + $this->columnMap['commenthistory'] + $this->columnMap['hosts'] + ); + foreach ($this->columnMap['services'] as $column => $_) { + $columns[$column] = new Zend_Db_Expr('NULL'); + } + if ($this->fetchHistoryColumns) { + $columns = array_merge($columns, array_keys($this->columnMap['history'])); + } + $hosts = $this->createSubQuery('Hostcommenthistory', $columns); + $this->subQueries[] = $hosts; + $this->commentHistoryQuery->union(array($hosts), Zend_Db_Select::SQL_UNION_ALL); + } + + /** + * Join services + */ + protected function joinServices() + { + $columns = array_keys( + $this->columnMap['commenthistory'] + $this->columnMap['hosts'] + $this->columnMap['services'] + ); + if ($this->fetchHistoryColumns) { + $columns = array_merge($columns, array_keys($this->columnMap['history'])); + } + $services = $this->createSubQuery('Servicecommenthistory', $columns); + $this->subQueries[] = $services; + $this->commentHistoryQuery->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; + } + + /** + * {@inheritdoc} + */ + public function addFilter(Filter $filter) + { + foreach ($this->subQueries as $sub) { + $sub->applyFilter(clone $filter); + } + return $this; + } }