From 908c408d3da99bcea56d417b9292d13d5c35d99a Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 26 Sep 2019 12:49:13 +0200 Subject: [PATCH] ServicegroupsummaryQuery: Fetch empty groups with a union query refs #3928 --- .../Ido/Query/EmptyservicegroupQuery.php | 36 ++++++++++++++++ .../Ido/Query/ServicegroupsummaryQuery.php | 41 +++++++++++++++---- 2 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Backend/Ido/Query/EmptyservicegroupQuery.php diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/EmptyservicegroupQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/EmptyservicegroupQuery.php new file mode 100644 index 000000000..98b21b36d --- /dev/null +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/EmptyservicegroupQuery.php @@ -0,0 +1,36 @@ + [ + 'servicegroup' => 'sgo.name1 COLLATE latin1_general_ci', + 'servicegroup_alias' => 'sg.alias COLLATE latin1_general_ci', + 'servicegroup_name' => 'sgo.name1', + 'host_name' => '(NULL)', + 'hostgroup_name' => '(NULL)', + 'service_description' => '(NULL)' + ], + 'instances' => [ + 'instance_name' => 'i.instance_name' + ] + ]; + + protected function joinBaseTables() + { + parent::joinBaseTables(); + + $this->select->joinLeft( + ['esgm' => $this->prefix . 'servicegroup_members'], + 'esgm.servicegroup_id = sg.servicegroup_id', + [] + ); + $this->select->group(['sgo.object_id', 'sg.servicegroup_id']); + $this->select->having('COUNT(esgm.servicegroup_member_id) = ?', 0); + } +} diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicegroupsummaryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicegroupsummaryQuery.php index 68cf1898b..11b62d026 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicegroupsummaryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicegroupsummaryQuery.php @@ -4,6 +4,8 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; use Icinga\Data\Filter\Filter; +use Zend_Db_Expr; +use Zend_Db_Select; /** * Query for service group summary @@ -34,11 +36,18 @@ class ServicegroupsummaryQuery extends IdoQuery ); /** - * Subquery used for the summary query + * The union * - * @var IdoQuery + * @var Zend_Db_Select */ - protected $subQuery; + protected $summaryQuery; + + /** + * Subqueries used for the summary query + * + * @var IdoQuery[] + */ + protected $subQueries = []; /** * Count query @@ -49,7 +58,9 @@ class ServicegroupsummaryQuery extends IdoQuery public function addFilter(Filter $filter) { - $this->subQuery->applyFilter(clone $filter); + foreach ($this->subQueries as $sub) { + $sub->applyFilter(clone $filter); + } $this->countQuery->applyFilter(clone $filter); return $this; } @@ -70,10 +81,24 @@ class ServicegroupsummaryQuery extends IdoQuery 'service_state' ) ); - $subQuery->setIsSubQuery(); - $this->subQuery = $subQuery; - $this->select->from(array('servicesgroupsummary' => $this->subQuery), array()); - $this->group(array('servicegroup_name', 'servicegroup_alias')); + $this->subQueries[] = $subQuery; + $emptyGroups = $this->createSubQuery( + 'Emptyservicegroup', + [ + 'servicegroup_alias', + 'servicegroup_name', + 'service_handled' => new Zend_Db_Expr('NULL'), + 'service_severity' => new Zend_Db_Expr('0'), + 'service_state' => new Zend_Db_Expr('NULL'), + ] + ); + $this->subQueries[] = $emptyGroups; + $this->summaryQuery = $this->db->select()->union( + [$subQuery, $emptyGroups], + Zend_Db_Select::SQL_UNION_ALL + ); + $this->select->from(['servicesgroupsummary' => $this->summaryQuery], []); + $this->group(['servicegroup_name', 'servicegroup_alias']); $this->joinedVirtualTables['servicegroupsummary'] = true; }