ServicegroupsummaryQuery: Fetch empty groups with a union query

refs #3928
This commit is contained in:
Johannes Meyer 2019-09-26 12:49:13 +02:00
parent faca6d53c8
commit 908c408d3d
2 changed files with 69 additions and 8 deletions

View File

@ -0,0 +1,36 @@
<?php
/* Icinga Web 2 | (c) 2019 Icinga GmbH | GPLv2+ */
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
class EmptyservicegroupQuery extends ServicegroupQuery
{
protected $subQueryTargets = [];
protected $columnMap = [
'servicegroups' => [
'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);
}
}

View File

@ -4,6 +4,8 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query; namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Zend_Db_Expr;
use Zend_Db_Select;
/** /**
* Query for service group summary * 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 * Count query
@ -49,7 +58,9 @@ class ServicegroupsummaryQuery extends IdoQuery
public function addFilter(Filter $filter) public function addFilter(Filter $filter)
{ {
$this->subQuery->applyFilter(clone $filter); foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter);
}
$this->countQuery->applyFilter(clone $filter); $this->countQuery->applyFilter(clone $filter);
return $this; return $this;
} }
@ -70,10 +81,24 @@ class ServicegroupsummaryQuery extends IdoQuery
'service_state' 'service_state'
) )
); );
$subQuery->setIsSubQuery(); $this->subQueries[] = $subQuery;
$this->subQuery = $subQuery; $emptyGroups = $this->createSubQuery(
$this->select->from(array('servicesgroupsummary' => $this->subQuery), array()); 'Emptyservicegroup',
$this->group(array('servicegroup_name', 'servicegroup_alias')); [
'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; $this->joinedVirtualTables['servicegroupsummary'] = true;
} }