From 9faea7c8830f24d1309a5518cd7c660eaeba9d77 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 14 Aug 2015 11:05:22 +0200 Subject: [PATCH] DataView: Introduce method getDynamicFilterColumns() refs #9029 --- .../Backend/Ido/Query/CustomvarQuery.php | 29 ++++++++++++++--- .../library/Monitoring/DataView/DataView.php | 31 +++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php index 3bf54756a..e4b87abfa 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php @@ -6,7 +6,7 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; class CustomvarQuery extends IdoQuery { protected $columnMap = array( - 'customvars' => array( + 'customvariablestatus' => array( 'varname' => 'cvs.varname', 'varvalue' => 'cvs.varvalue', 'is_json' => 'cvs.is_json', @@ -38,7 +38,7 @@ class CustomvarQuery extends IdoQuery protected function joinBaseTables() { if (version_compare($this->getIdoVersion(), '1.12.0', '<')) { - $this->columnMap['customvars']['is_json'] = '(0)'; + $this->columnMap['customvariablestatus']['is_json'] = '(0)'; } $this->select->from( @@ -50,8 +50,29 @@ class CustomvarQuery extends IdoQuery array() ); $this->joinedVirtualTables = array( - 'customvars' => true, - 'objects' => true + 'customvariablestatus' => true, + 'objects' => true ); } + + /** + * {@inheritdoc} + */ + public function getGroup() + { + $group = parent::getGroup(); + if (! empty($group) && $this->ds->getDbType() === 'pgsql') { + foreach ($this->columnMap as $table => $columns) { + $pk = ($table === 'objects' ? 'cvo.' : 'cvs.') . $this->getPrimaryKeyColumn($table); + foreach ($columns as $alias => $_) { + if (! in_array($pk, $group, true) && in_array($alias, $group, true)) { + $group[] = $pk; + break; + } + } + } + } + + return $group; + } } diff --git a/modules/monitoring/library/Monitoring/DataView/DataView.php b/modules/monitoring/library/Monitoring/DataView/DataView.php index fe77b3d2a..181c159ab 100644 --- a/modules/monitoring/library/Monitoring/DataView/DataView.php +++ b/modules/monitoring/library/Monitoring/DataView/DataView.php @@ -191,6 +191,37 @@ abstract class DataView implements QueryInterface, SortRules, FilterColumns, Ite return array(); } + /** + * Return all dynamic filter columns such as custom variables + * + * @return array + */ + public function getDynamicFilterColumns() + { + $columns = array(); + if (! $this->query->allowsCustomVars()) { + return $columns; + } + + $query = MonitoringBackend::instance() + ->select() + ->from('customvar', array('varname', 'object_type')) + ->where('is_json', 0) + ->where('object_type_id', array(1, 2)) + ->getQuery()->group(array('varname', 'object_type')); + foreach ($query as $row) { + if ($row->object_type === 'host') { + $label = t('Host') . ' ' . ucwords(str_replace('_', ' ', $row->varname)); + $columns[$label] = '_host_' . $row->varname; + } else { // $row->object_type === 'service' + $label = t('Service') . ' ' . ucwords(str_replace('_', ' ', $row->varname)); + $columns[$label] = '_service_' . $row->varname; + } + } + + return $columns; + } + public function getFilter() { return $this->filter;