MultiControllers: use new filters

This makes things easier, no more nested lists - just filters easy
to read and understand
This commit is contained in:
Thomas Gelf 2014-06-20 13:38:58 +02:00
parent 255accb215
commit f2c67ea047

View File

@ -27,16 +27,14 @@
*/ */
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
use \Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Module\Monitoring\Controller; use Icinga\Module\Monitoring\Controller;
use \Icinga\Web\Widget\Tabextension\OutputFormat; use Icinga\Module\Monitoring\Backend;
use \Icinga\Module\Monitoring\Backend; use Icinga\Data\SimpleQuery;
use \Icinga\Data\SimpleQuery; use Icinga\Web\Widget\Chart\InlinePie;
use \Icinga\Web\Widget\Chart\InlinePie; use Icinga\Module\Monitoring\Form\Command\MultiCommandFlagForm;
use \Icinga\Module\Monitoring\Form\Command\MultiCommandFlagForm; use Icinga\Web\Widget;
use \Icinga\Module\Monitoring\DataView\HostStatus as HostStatusView; use Icinga\Data\Filter\Filter;
use \Icinga\Module\Monitoring\DataView\ServiceStatus as ServiceStatusView;
use \Icinga\Module\Monitoring\DataView\Comment as CommentView;
/** /**
* Displays aggregations collections of multiple objects. * Displays aggregations collections of multiple objects.
@ -45,12 +43,9 @@ class Monitoring_MultiController extends Controller
{ {
public function hostAction() public function hostAction()
{ {
$multiFilter = $this->getAllParamsAsArray();
$errors = array(); $errors = array();
$query = $this->backend->select()->from(
// Fetch Hosts 'hostStatus',
$hostQuery = HostStatusView::fromRequest(
$this->_request,
array( array(
'host_name', 'host_name',
'host_in_downtime', 'host_in_downtime',
@ -62,36 +57,31 @@ class Monitoring_MultiController extends Controller
'host_event_handler_enabled', 'host_event_handler_enabled',
'host_flap_detection_enabled', 'host_flap_detection_enabled',
'host_active_checks_enabled', 'host_active_checks_enabled',
// columns intended for filter-request // columns intended for filter-request
'host_problem', 'host_problem',
'host_handled' 'host_handled'
) )
)->getQuery(); )->getQuery();
$this->applyQueryFilter($hostQuery, $multiFilter); $this->applyQueryFilter($query);
$hosts = $hostQuery->fetchAll(); $hosts = $query->fetchAll();
// Fetch comments $comments = $this->backend->select()->from('comment', array(
$commentQuery = $this->applyQueryFilter( 'comment_internal_id',
CommentView::fromParams(array('backend' => $this->_request->getParam('backend')))->getQuery(), 'comment_host',
$multiFilter, ));
'comment_host' $this->applyQueryFilter($comments);
); $uniqueComments = array_keys($this->getUniqueValues($comments->getQuery()->fetchAll(), 'comment_internal_id'));
$comments = array_keys($this->getUniqueValues($commentQuery->fetchAll(), 'comment_internal_id'));
// Populate view // Populate view
$this->view->objects = $this->view->hosts = $hosts; $this->view->objects = $this->view->hosts = $hosts;
$this->view->problems = $this->getProblems($hosts); $this->view->problems = $this->getProblems($hosts);
$this->view->comments = isset($comments) ? $comments : $this->getComments($hosts); $this->view->comments = $uniqueComments;
$this->view->hostnames = $this->getProperties($hosts, 'host_name'); $this->view->hostnames = $this->getProperties($hosts, 'host_name');
$this->view->downtimes = $this->getDowntimes($hosts); $this->view->downtimes = $this->getDowntimes($hosts);
$this->view->errors = $errors; $this->view->errors = $errors;
$this->view->states = $this->countStates($hosts, 'host', 'host_name'); $this->view->states = $this->countStates($hosts, 'host', 'host_name');
$this->view->pie = $this->createPie($this->view->states, $this->view->getHelper('MonitoringState')->getHostStateColors()); $this->view->pie = $this->createPie($this->view->states, $this->view->getHelper('MonitoringState')->getHostStateColors());
// need the query content to list all hosts
$this->view->query = $this->_request->getQuery();
// Handle configuration changes // Handle configuration changes
$this->handleConfigurationForm(array( $this->handleConfigurationForm(array(
'host_passive_checks_enabled' => 'Passive Checks', 'host_passive_checks_enabled' => 'Passive Checks',
@ -103,54 +93,47 @@ class Monitoring_MultiController extends Controller
)); ));
} }
public function serviceAction() public function serviceAction()
{ {
$multiFilter = $this->getAllParamsAsArray();
$errors = array(); $errors = array();
$backendQuery = ServiceStatusView::fromRequest( $query = $this->backend->select()->from('serviceStatus', array(
$this->_request, 'host_name',
array( 'host_state',
'host_name', 'service_description',
'host_state', 'service_handled',
'service_description', 'service_state',
'service_handled', 'service_in_downtime',
'service_state', 'service_passive_checks_enabled',
'service_in_downtime', 'service_notifications_enabled',
'service_passive_checks_enabled', 'service_event_handler_enabled',
'service_notifications_enabled', 'service_flap_detection_enabled',
'service_event_handler_enabled', 'service_active_checks_enabled',
'service_flap_detection_enabled', 'service_obsessing',
'service_active_checks_enabled', // also accept all filter-requests from ListView
'service_obsessing', 'service_problem',
'service_severity',
'service_last_check',
'service_state_type',
'host_severity',
'host_address',
'host_last_check'
));
// also accept all filter-requests from ListView $this->applyQueryFilter($query);
'service_problem', $services = $query->getQuery()->fetchAll();
'service_severity',
'service_last_check',
'service_state_type',
'host_severity',
'host_address',
'host_last_check'
)
)->getQuery();
$this->applyQueryFilter($backendQuery, $multiFilter); $comments = $this->backend->select()->from('comment', array(
$services = $backendQuery->fetchAll(); 'comment_internal_id',
// Comments
$commentQuery = $this->applyQueryFilter(
CommentView::fromParams(array('backend' => $this->_request->getParam('backend')))->getQuery(),
$multiFilter,
'comment_host', 'comment_host',
'comment_service' 'comment_service'
); ));
$comments = array_keys($this->getUniqueValues($commentQuery->fetchAll(), 'comment_internal_id')); $this->applyQueryFilter($comments);
$uniqueComments = array_keys($this->getUniqueValues($comments->getQuery()->fetchAll(), 'comment_internal_id'));
// populate the view // populate the view
$this->view->objects = $this->view->services = $services; $this->view->objects = $this->view->services = $services;
$this->view->problems = $this->getProblems($services); $this->view->problems = $this->getProblems($services);
$this->view->comments = isset($comments) ? $comments : $this->getComments($services); $this->view->comments = $uniqueComments;
$this->view->hostnames = $this->getProperties($services, 'host_name'); $this->view->hostnames = $this->getProperties($services, 'host_name');
$this->view->servicenames = $this->getProperties($services, 'service_description'); $this->view->servicenames = $this->getProperties($services, 'service_description');
$this->view->downtimes = $this->getDowntimes($services); $this->view->downtimes = $this->getDowntimes($services);
@ -160,9 +143,6 @@ class Monitoring_MultiController extends Controller
$this->view->host_pie = $this->createPie($this->view->host_states, $this->view->getHelper('MonitoringState')->getHostStateColors()); $this->view->host_pie = $this->createPie($this->view->host_states, $this->view->getHelper('MonitoringState')->getHostStateColors());
$this->view->errors = $errors; $this->view->errors = $errors;
// need the query content to list all hosts
$this->view->query = $this->_request->getQuery();
$this->handleConfigurationForm(array( $this->handleConfigurationForm(array(
'service_passive_checks_enabled' => 'Passive Checks', 'service_passive_checks_enabled' => 'Passive Checks',
'service_active_checks_enabled' => 'Active Checks', 'service_active_checks_enabled' => 'Active Checks',
@ -173,38 +153,21 @@ class Monitoring_MultiController extends Controller
)); ));
} }
protected function applyQueryFilter($query)
{
$params = clone $this->params;
$modifyFilter = $params->shift('modifyFilter');
/** $filter = Filter::fromQueryString((string) $params);
* Apply the query-filter received if ($modifyFilter) {
* $this->view->filterWidget = Widget::create('filterEditor', array(
* @param $backendQuery SimpleQuery The query to apply the filter to 'filter' => $filter,
* @param $filter array Containing the queries of the current request, converted into an 'query' => $query
* array-structure. ));
* @param $hostColumn string The name of the host-column in the SimpleQuery, defaults to 'host_name'
* @param $serviceColumn string The name of the service-column in the SimpleQuery, defaults to 'service-description'
*
* @return SimpleQuery The given SimpleQuery
*/
private function applyQueryFilter(
SimpleQuery $backendQuery,
array $filter,
$hostColumn = 'host_name',
$serviceColumn = 'service_description'
) {
// fetch specified hosts
foreach ($filter as $index => $expr) {
// Every query entry must define at least the host.
if (!array_key_exists('host', $expr)) {
$errors[] = 'Query ' . $index . ' misses property host.';
continue;
}
// apply filter expressions from query
$backendQuery->orWhere($hostColumn, $expr['host']);
if (array_key_exists('service', $expr)) {
$backendQuery->andWhere($serviceColumn, $expr['service']);
}
} }
return $backendQuery; $this->view->filter = $filter;
$query->applyFilter($filter);
return $query;
} }
/** /**
@ -333,38 +296,4 @@ class Monitoring_MultiController extends Controller
$this->view->form->initFromItems($this->view->objects); $this->view->form->initFromItems($this->view->objects);
} }
} }
/**
* "Flips" the structure of the objects created by _getAllParams
*
* Regularly, _getAllParams would return queries like <b>host[0]=value1&service[0]=value2</b> as
* two entirely separate arrays. Instead, we want it as one single array, containing one single object
* for each index, containing all of its members as keys.
*
* @return array An array of all query parameters (See example above)
* <b>
* array( <br />
* 0 => array(host => value1, service => value2), <br />
* ... <br />
* )
* </b>
*/
private function getAllParamsAsArray()
{
$details = $this->_getAllParams();
$queries = array();
foreach ($details as $property => $values) {
if (!is_array($values)) {
continue;
}
foreach ($values as $index => $value) {
if (!array_key_exists($index, $queries)) {
$queries[$index] = array();
}
$queries[$index][$property] = $value;
}
}
return $queries;
}
} }