ServicegroupsummaryQuery: Fetch empty groups with a union query
refs #3928
This commit is contained in:
parent
faca6d53c8
commit
908c408d3d
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue