From 027ef6ede36c44970dd14fe0ea62d0de8a444d50 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 17 Jun 2014 12:36:32 +0000 Subject: [PATCH] Ido\Query\*historyQuery: subqueries join objects Each of our history subqueries should be able to join main object tables on their own for performance reasons. --- .../Ido/Query/CommentdeletionhistoryQuery.php | 42 +++++++++++++++++++ .../Backend/Ido/Query/CommenthistoryQuery.php | 33 ++++++++++++--- .../Ido/Query/DowntimeendhistoryQuery.php | 30 ++++++++++--- .../Ido/Query/DowntimestarthistoryQuery.php | 30 ++++++++++--- .../Ido/Query/NotificationhistoryQuery.php | 16 ++++++- .../Backend/Ido/Query/StatehistoryQuery.php | 40 +++++++++++++----- 6 files changed, 163 insertions(+), 28 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentdeletionhistoryQuery.php diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentdeletionhistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentdeletionhistoryQuery.php new file mode 100644 index 000000000..f82136764 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentdeletionhistoryQuery.php @@ -0,0 +1,42 @@ + array( + 'state_time' => 'h.deletion_time', + 'timestamp' => 'UNIX_TIMESTAMP(h.deletion_time)', + 'raw_timestamp' => 'h.deletion_time', + 'object_id' => 'h.object_id', + 'type' => "(CASE h.entry_type WHEN 1 THEN 'comment_deleted' WHEN 2 THEN 'dt_comment_deleted' WHEN 3 THEN 'flapping_deleted' WHEN 4 THEN 'ack_deleted' 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 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'o.name1 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci' + ) + ); + + protected function joinBaseTables() + { + $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.deletion_time > '1970-01-01 00:00:00' AND h.entry_type <> 2", + array() + ); + $this->joinedVirtualTables = array('commenthistory' => true); + } + +} diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php index bac78a7cd..cf42fb6ce 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommenthistoryQuery.php @@ -6,16 +6,37 @@ class CommenthistoryQuery extends IdoQuery { protected $columnMap = array( 'commenthistory' => array( - 'state_time' => 'comment_time', - 'timestamp' => 'UNIX_TIMESTAMP(comment_time)', - 'raw_timestamp' => 'comment_time', - 'object_id' => 'object_id', - 'type' => "(CASE entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'dt_comment' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END)", + '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' => "('[' || author_name || '] ' || comment_data)", + '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 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'o.name1 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci' ) ); + + protected function joinBaseTables() + { + $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() + ); + $this->joinedVirtualTables = array('commenthistory' => true); + } + } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeendhistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeendhistoryQuery.php index 75fecd848..5924209d1 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeendhistoryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeendhistoryQuery.php @@ -6,17 +6,37 @@ class DowntimeendhistoryQuery extends IdoQuery { protected $columnMap = array( 'downtimehistory' => array( - 'state_time' => 'actual_end_time', - 'timestamp' => 'UNIX_TIMESTAMP(actual_end_time)', - 'raw_timestamp' => 'actual_end_time', - 'object_id' => 'object_id', + 'state_time' => 'h.actual_end_time', + 'timestamp' => 'UNIX_TIMESTAMP(h.actual_end_time)', + 'raw_timestamp' => 'h.actual_end_time', + 'object_id' => 'h.object_id', 'type' => "('dt_end')", 'state' => '(NULL)', 'state_type' => '(NULL)', - 'output' => "('[' || author_name || '] ' || comment_data)", + '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 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'o.name1 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci' ) ); + + protected function joinBaseTables() + { + $this->select->from( + array('o' => $this->prefix . 'objects'), + array() + )->join( + array('h' => $this->prefix . 'downtimehistory'), + 'o.' . $this->object_id . ' = h.' . $this->object_id . ' AND o.is_active = 1', + array() + )->where('h.actual_end_time > ?', '1970-01-01 00:00:00'); + $this->joinedVirtualTables = array('downtimehistory' => true); + } } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimestarthistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimestarthistoryQuery.php index af68d66b0..f4cbe95bf 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimestarthistoryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimestarthistoryQuery.php @@ -6,17 +6,37 @@ class DowntimestarthistoryQuery extends IdoQuery { protected $columnMap = array( 'downtimehistory' => array( - 'state_time' => 'actual_start_time', - 'timestamp' => 'UNIX_TIMESTAMP(actual_start_time)', - 'raw_timestamp' => 'actual_start_time', - 'object_id' => 'object_id', + 'state_time' => 'h.actual_start_time', + 'timestamp' => 'UNIX_TIMESTAMP(h.actual_start_time)', + 'raw_timestamp' => 'h.actual_start_time', + 'object_id' => 'h.object_id', 'type' => "('dt_start')", 'state' => '(NULL)', 'state_type' => '(NULL)', - 'output' => "('[' || author_name || '] ' || comment_data)", + '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 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'o.name1 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci' ) ); + + protected function joinBaseTables() + { + $this->select->from( + array('o' => $this->prefix . 'objects'), + array() + )->join( + array('h' => $this->prefix . 'downtimehistory'), + 'o.' . $this->object_id . ' = h.' . $this->object_id . ' AND o.is_active = 1', + array() + )->where('h.actual_start_time > ?', '1970-01-01 00:00:00'); + $this->joinedVirtualTables = array('downtimehistory' => true); + } } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/NotificationhistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/NotificationhistoryQuery.php index 4dabd6ef2..6272f99d9 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/NotificationhistoryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/NotificationhistoryQuery.php @@ -9,13 +9,20 @@ class NotificationhistoryQuery extends IdoQuery 'timestamp' => 'UNIX_TIMESTAMP(n.start_time)', 'raw_timestamp' => 'n.start_time', 'state_time' => 'n.start_time', - 'object_id' => 'object_id', + 'object_id' => 'n.object_id', 'type' => "('notify')", - 'state' => 'state', + 'state' => 'n.state', 'state_type' => '(NULL)', 'output' => null, 'attempt' => '(NULL)', 'max_attempts' => '(NULL)', + + 'host' => 'o.name1 COLLATE latin1_general_ci', + 'service' => 'o.name2 COLLATE latin1_general_ci', + 'host_name' => 'o.name1 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'o.name1 COLLATE latin1_general_ci', + 'service_description' => 'o.name2 COLLATE latin1_general_ci' ) ); @@ -45,7 +52,11 @@ class NotificationhistoryQuery extends IdoQuery $this->columnMap['history']['output'] = "('[' || $concattedContacts || '] ' || n.output)"; $this->select->from( + array('o' => $this->prefix . 'objects'), + array() + )->join( array('n' => $this->prefix . 'notifications'), + 'o.' . $this->object_id . ' = n.' . $this->object_id . ' AND o.is_active = 1', array() )->join( array('cn' => $this->prefix . 'contactnotifications'), @@ -57,6 +68,7 @@ class NotificationhistoryQuery extends IdoQuery array() )->group('cn.notification_id'); + // TODO: hmmmm... if ($this->ds->getDbType() === 'pgsql') { $this->select->group('n.object_id') ->group('n.start_time') diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatehistoryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatehistoryQuery.php index bfe8ae5c4..66944bd5c 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatehistoryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatehistoryQuery.php @@ -6,17 +6,37 @@ class StatehistoryQuery extends IdoQuery { protected $columnMap = array( 'statehistory' => array( - 'raw_timestamp' => 'state_time', - 'timestamp' => 'UNIX_TIMESTAMP(state_time)', - 'state_time' => 'state_time', - 'object_id' => 'object_id', - 'type' => "(CASE WHEN state_type = 1 THEN 'hard_state' ELSE 'soft_state' END)", - 'state' => 'state', - 'state_type' => 'state_type', - 'output' => 'output', - 'attempt' => 'current_check_attempt', - 'max_attempts' => 'max_check_attempts', + 'raw_timestamp' => 'sh.state_time', + 'timestamp' => 'UNIX_TIMESTAMP(sh.state_time)', + 'state_time' => 'sh.state_time', + 'object_id' => 'sho.object_id', + 'type' => "(CASE WHEN sh.state_type = 1 THEN 'hard_state' ELSE 'soft_state' END)", + 'state' => 'sh.state', + 'state_type' => 'sh.state_type', + 'output' => 'sh.output', + 'attempt' => 'sh.current_check_attempt', + 'max_attempts' => 'sh.max_check_attempts', + + 'host' => 'sho.name1 COLLATE latin1_general_ci', + 'service' => 'sho.name2 COLLATE latin1_general_ci', + 'host_name' => 'sho.name1 COLLATE latin1_general_ci', + 'service_description' => 'sho.name2 COLLATE latin1_general_ci', + 'service_host_name' => 'sho.name1 COLLATE latin1_general_ci', + 'service_description' => 'sho.name2 COLLATE latin1_general_ci' ) ); + + protected function joinBaseTables() + { + $this->select->from( + array('sho' => $this->prefix . 'objects'), + array() + )->join( + array('sh' => $this->prefix . 'statehistory'), + 'sho.' . $this->object_id . ' = sh.' . $this->object_id . ' AND sho.is_active = 1', + array() + ); + $this->joinedVirtualTables = array('statehistory' => true); + } }