Skip to content

Commit

Permalink
Merge branch 'bugfix/unnecessary-joins-8614'
Browse files Browse the repository at this point in the history
fixes #8614
  • Loading branch information
lippserd committed Mar 12, 2015
2 parents 31d0641 + 53626cc commit be04143
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 189 deletions.
4 changes: 2 additions & 2 deletions modules/monitoring/application/controllers/ListController.php
Expand Up @@ -282,8 +282,8 @@ public function downtimesAction()
'is_fixed' => 'downtime_is_fixed',
'is_in_effect' => 'downtime_is_in_effect',
'entry_time' => 'downtime_entry_time',
'host' => 'downtime_host',
'service' => 'downtime_service',
'host' => 'host_name',
'service' => 'service_description',
'host_state' => 'downtime_host_state',
'service_state' => 'downtime_service_state',
'host_display_name',
Expand Down
Expand Up @@ -22,6 +22,9 @@ class CommentQuery extends IdoQuery
'comment_service' => 'so.name2 COLLATE latin1_general_ci',
'service' => 'so.name2 COLLATE latin1_general_ci', // #7278, #7279
'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",
'service_description' => 'so.name2',
'host_name' => 'CASE WHEN ho.name1 IS NULL THEN so.name1 ELSE ho.name1 END',
'service_host_name' => 'so.name1'
),
'hosts' => array(
'host_display_name' => 'CASE WHEN sh.display_name IS NOT NULL THEN sh.display_name ELSE h.display_name END'
Expand Down
Expand Up @@ -7,41 +7,39 @@ class ContactQuery extends IdoQuery
{
protected $columnMap = array(
'contacts' => array(
'contact_id' => 'c.contact_id',
'contact_name' => 'co.name1 COLLATE latin1_general_ci',
'contact_alias' => 'c.alias COLLATE latin1_general_ci',
'contact_email' => 'c.email_address COLLATE latin1_general_ci',
'contact_pager' => 'c.pager_address',
'contact_object_id' => 'c.contact_object_id',
'contact_has_host_notfications' => 'c.host_notifications_enabled',
'contact_has_service_notfications' => 'c.service_notifications_enabled',
'contact_can_submit_commands' => 'c.can_submit_commands',
'contact_notify_service_recovery' => 'c.notify_service_recovery',
'contact_notify_service_warning' => 'c.notify_service_warning',
'contact_notify_service_critical' => 'c.notify_service_critical',
'contact_notify_service_unknown' => 'c.notify_service_unknown',
'contact_notify_service_flapping' => 'c.notify_service_flapping',
'contact_notify_service_downtime' => 'c.notify_service_recovery',
'contact_notify_host_recovery' => 'c.notify_host_recovery',
'contact_notify_host_down' => 'c.notify_host_down',
'contact_notify_host_unreachable' => 'c.notify_host_unreachable',
'contact_notify_host_flapping' => 'c.notify_host_flapping',
'contact_notify_host_downtime' => 'c.notify_host_downtime',
'contact_id' => 'c.contact_id',
'contact_name' => 'co.name1 COLLATE latin1_general_ci',
'contact_alias' => 'c.alias COLLATE latin1_general_ci',
'contact_email' => 'c.email_address COLLATE latin1_general_ci',
'contact_pager' => 'c.pager_address',
'contact_object_id' => 'c.contact_object_id',
'contact_has_host_notfications' => 'c.host_notifications_enabled',
'contact_has_service_notfications' => 'c.service_notifications_enabled',
'contact_can_submit_commands' => 'c.can_submit_commands',
'contact_notify_service_recovery' => 'c.notify_service_recovery',
'contact_notify_service_warning' => 'c.notify_service_warning',
'contact_notify_service_critical' => 'c.notify_service_critical',
'contact_notify_service_unknown' => 'c.notify_service_unknown',
'contact_notify_service_flapping' => 'c.notify_service_flapping',
'contact_notify_service_downtime' => 'c.notify_service_recovery',
'contact_notify_host_recovery' => 'c.notify_host_recovery',
'contact_notify_host_down' => 'c.notify_host_down',
'contact_notify_host_unreachable' => 'c.notify_host_unreachable',
'contact_notify_host_flapping' => 'c.notify_host_flapping',
'contact_notify_host_downtime' => 'c.notify_host_downtime'
),
'timeperiods' => array(
'contact_notify_host_timeperiod' => 'ht.alias COLLATE latin1_general_ci',
'contact_notify_host_timeperiod' => 'ht.alias COLLATE latin1_general_ci',
'contact_notify_service_timeperiod' => 'st.alias COLLATE latin1_general_ci'
),
'hosts' => array(
'host_object_id' => 'ho.object_id',
'host_name' => 'ho.name1 COLLATE latin1_general_ci',
'host' => 'ho.name1 COLLATE latin1_general_ci',
'host' => 'ho.name1 COLLATE latin1_general_ci',
'host_name' => 'ho.name1',
),
'services' => array(
'service_object_id' => 'so.object_id',
'service_host_name' => 'so.name1 COLLATE latin1_general_ci',
'service' => 'so.name1 COLLATE latin1_general_ci',
'service_description' => 'so.name2 COLLATE latin1_general_ci',
'service' => 'so.name1 COLLATE latin1_general_ci',
'service_description' => 'so.name2',
'service_host_name' => 'so.name1',
)
);

Expand Down
Expand Up @@ -33,13 +33,13 @@ class ContactgroupQuery extends IdoQuery
'contact_notify_host_downtime' => 'c.notify_host_downtime',
),
'hosts' => array(
'host' => 'ho.name1',
'host' => 'ho.name1 COLLATE latin1_general_ci',
'host_name' => 'ho.name1'
),
'services' => array(
'service' => 'so.name2 COLLATE latin1_general_ci',
'service_description' => 'so.name2 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1 COLLATE latin1_general_ci'
'service_description' => 'so.name2',
'service_host_name' => 'so.name1'
)
);

Expand Down Expand Up @@ -95,10 +95,10 @@ protected function joinHosts()

protected function joinServices()
{
$scgSub = $this->db->select()->distinct()->from(
$this->prefix . 'service_contactgroups',
array('contactgroup_object_id', 'service_id')
);
// $scgSub = $this->db->select()->distinct()->from(
// $this->prefix . 'service_contactgroups',
// array('contactgroup_object_id', 'service_id')
// );

/*
This subselect is a workaround for a fucking stupid bug. Other tables
Expand Down
Expand Up @@ -3,8 +3,14 @@

namespace Icinga\Module\Monitoring\Backend\Ido\Query;

/**
* Query for host and service downtimes
*/
class DowntimeQuery extends IdoQuery
{
/**
* {@inheritdoc}
*/
protected $columnMap = array(
'downtime' => array(
'downtime_author' => 'sd.author_name',
Expand All @@ -21,22 +27,32 @@ class DowntimeQuery extends IdoQuery
'downtime_duration' => 'sd.duration',
'downtime_is_in_effect' => 'sd.is_in_effect',
'downtime_internal_id' => 'sd.internal_downtime_id',
'downtime_objecttype' => "CASE WHEN ho.object_id IS NULL THEN 'service' ELSE 'host' END",
'downtime_host' => 'CASE WHEN ho.name1 IS NULL THEN so.name1 ELSE ho.name1 END COLLATE latin1_general_ci', // #7278, #7279
'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',
'downtime_service' => 'so.name2 COLLATE latin1_general_ci',
'service' => 'so.name2 COLLATE latin1_general_ci', // #7278, #7279
'downtime_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",
'downtime_host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END',
'downtime_service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE ss.current_state END'
'service_description' => 'so.name2',
'service_host_name' => 'so.name1'
),
'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' => 'CASE WHEN h.display_name IS NULL THEN sh.display_name ELSE h.display_name END'
),
'hoststatus' => array(
'downtime_host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END'
),
'services' => array(
'service_display_name' => 's.display_name'
),
'servicestatus' => array(
'downtime_service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE ss.current_state END'
)
);

/**
* {@inheritdoc}
*/
protected function joinBaseTables()
{
$this->select->from(
Expand All @@ -53,19 +69,14 @@ protected function joinBaseTables()
'sd.object_id = so.object_id AND so.is_active = 1 AND so.objecttype_id = 2',
array()
);
$this->select->joinLeft(
array('hs' => $this->prefix . 'hoststatus'),
'ho.object_id = hs.host_object_id',
array()
);
$this->select->joinLeft(
array('ss' => $this->prefix . 'servicestatus'),
'so.object_id = ss.service_object_id',
array()
);
$this->joinedVirtualTables = array('downtime' => true);
}

/**
* Join downtimes' hosts
*
* @return $this
*/
protected function joinHosts()
{
$this->select->joinLeft(
Expand All @@ -76,6 +87,26 @@ protected function joinHosts()
return $this;
}

/**
* Join downtimes' hosts' status
*
* @return $this
*/
protected function joinHoststatus()
{
$this->select->joinLeft(
array('hs' => $this->prefix . 'hoststatus'),
'ho.object_id = hs.host_object_id',
array()
);
return $this;
}

/**
* Join downtimes' services
*
* @return $this
*/
protected function joinServices()
{
$this->select->joinLeft(
Expand All @@ -90,4 +121,19 @@ protected function joinServices()
);
return $this;
}

/**
* Join downtimes' services' status
*
* @return $this
*/
protected function joinServicestatus()
{
$this->select->joinLeft(
array('ss' => $this->prefix . 'servicestatus'),
'so.object_id = ss.service_object_id',
array()
);
return $this;
}
}
Expand Up @@ -6,10 +6,23 @@
use Zend_Db_Select;
use Icinga\Data\Filter\Filter;

/**
* Query for event history
*/
class EventHistoryQuery extends IdoQuery
{
/**
* Subqueries used for the event history query
*
* @type IdoQuery[]
*
* @see EventHistoryQuery::joinBaseTables() For the used subqueries.
*/
protected $subQueries = array();

/**
* {@inheritdoc}
*/
protected $columnMap = array(
'eventhistory' => array(
'cnt_notification' => "SUM(CASE eh.type WHEN 'notify' THEN 1 ELSE 0 END)",
Expand Down Expand Up @@ -41,8 +54,14 @@ class EventHistoryQuery extends IdoQuery
)
);

/**
* {@inheritdoc}
*/
protected $useSubqueryCount = true;

/**
* {@inheritdoc}
*/
protected function joinBaseTables()
{
$columns = array(
Expand Down Expand Up @@ -77,15 +96,20 @@ protected function joinBaseTables()
$this->joinedVirtualTables = array('eventhistory' => true);
}

/**
* {@inheritdoc}
*/
public function order($columnOrAlias, $dir = null)
{
foreach ($this->subQueries as $sub) {
$sub->requireColumn($columnOrAlias);
}

return parent::order($columnOrAlias, $dir);
}

/**
* {@inheritdoc}
*/
public function addFilter(Filter $filter)
{
foreach ($this->subQueries as $sub) {
Expand All @@ -94,6 +118,9 @@ public function addFilter(Filter $filter)
return $this;
}

/**
* {@inheritdoc}
*/
public function where($condition, $value = null)
{
$this->requireColumn($condition);
Expand All @@ -103,6 +130,11 @@ public function where($condition, $value = null)
return $this;
}

/**
* Join host groups
*
* @return $this
*/
protected function joinHostgroups()
{
$this->select->join(
Expand All @@ -121,6 +153,11 @@ protected function joinHostgroups()
return $this;
}

/**
* Join hosts
*
* @return $this
*/
protected function joinHosts()
{
$this->select->joinLeft(
Expand All @@ -131,6 +168,11 @@ protected function joinHosts()
return $this;
}

/**
* Join services
*
* @return $this
*/
protected function joinServices()
{
$this->select->joinLeft(
Expand Down
8 changes: 2 additions & 6 deletions modules/monitoring/library/Monitoring/DataView/Comment.php
Expand Up @@ -9,9 +9,7 @@
class Comment extends DataView
{
/**
* Retrieve columns provided by this view
*
* @return array
* {@inheritdoc}
*/
public function getColumns()
{
Expand All @@ -34,9 +32,7 @@ public function getColumns()
}

/**
* Retrieve default sorting rules for particular columns. These involve sort order and potential additional to sort
*
* @return array
* {@inheritdoc}
*/
public function getSortRules()
{
Expand Down
5 changes: 4 additions & 1 deletion modules/monitoring/library/Monitoring/DataView/Downtime.php
Expand Up @@ -33,7 +33,10 @@ public function getColumns()
'downtime_host_state',
'downtime_service_state',
'host_display_name',
'service_display_name'
'service_display_name',
'host_name',
'service_host_name',
'service_description'
);
}

Expand Down

0 comments on commit be04143

Please sign in to comment.