From 1d8530f18b22acd074fd5627191bbd2aabf22276 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 18 Dec 2015 09:05:04 +0100 Subject: [PATCH] Db: add object summary method --- library/Director/Db.php | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/library/Director/Db.php b/library/Director/Db.php index b1d7b718..dbded79c 100644 --- a/library/Director/Db.php +++ b/library/Director/Db.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Director; use Icinga\Data\Db\DbConnection; use Icinga\Module\Director\Objects\DirectorDeploymentLog; use Zend_Db_Expr; +use Zend_Db_Select; class Db extends DbConnection { @@ -283,6 +284,47 @@ class Db extends DbConnection return $db->fetchOne($lastRun); } + public function getObjectSummary() + { + $types = array( + 'host', + 'hostgroup', + 'service', + 'servicegroup', + 'user', + 'usergroup', + 'timeperiod', + 'apiuser', + 'endpoint', + 'zone', + ); + + $queries = array(); + $db = $this->db(); + foreach ($types as $type) { + $queries[] = $db->select()->from( + 'icinga_' . $type, + array( + 'icinga_type' => "('" . $type . "')", + 'cnt_object' => "COALESCE(SUM(CASE WHEN object_type = 'object' THEN 1 ELSE 0 END), 0)", + 'cnt_template' => "COALESCE(SUM(CASE WHEN object_type = 'template' THEN 1 ELSE 0 END), 0)", + 'cnt_external' => "COALESCE(SUM(CASE WHEN object_type = 'external_object' THEN 1 ELSE 0 END), 0)", + 'cnt_total' => 'COUNT(*)', + ) + ); + } + + $query = $this->db()->select()->union($queries, Zend_Db_Select::SQL_UNION_ALL); + + $result = array(); + + foreach ($db->fetchAll($query) as $row) { + $result[$row->icinga_type] = $row; + } + + return $result; + } + public function listImportedRowsetColumnNames($checksum) { $db = $this->db();