DataView: Introduce method getDynamicFilterColumns()

refs #9029
This commit is contained in:
Johannes Meyer 2015-08-14 11:05:22 +02:00
parent 71fba8f7df
commit 9faea7c883
2 changed files with 56 additions and 4 deletions

View File

@ -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,
'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;
}
}

View File

@ -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;