Respect limits and sort orders for format requests

fixes #2967
This commit is contained in:
Johannes Meyer 2017-10-17 16:05:07 +02:00
parent e914f121e4
commit 37aeff7791
2 changed files with 56 additions and 45 deletions

View File

@ -79,10 +79,8 @@ class ListController extends Controller
'host_passive_checks_enabled' 'host_passive_checks_enabled'
), $this->addColumns())); ), $this->addColumns()));
$this->applyRestriction('monitoring/filter/objects', $hosts); $this->applyRestriction('monitoring/filter/objects', $hosts);
$this->filterQuery($hosts);
$this->setupPaginationControl($hosts); $this->setupPaginationControl($hosts);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'host_severity' => $this->translate('Severity'), 'host_severity' => $this->translate('Severity'),
'host_state' => $this->translate('Current State'), 'host_state' => $this->translate('Current State'),
@ -91,6 +89,8 @@ class ListController extends Controller
'host_last_check' => $this->translate('Last Check'), 'host_last_check' => $this->translate('Last Check'),
'host_last_state_change' => $this->translate('Last State Change') 'host_last_state_change' => $this->translate('Last State Change')
), $hosts); ), $hosts);
$this->filterQuery($hosts);
$this->setupLimitControl();
$stats = $this->backend->select()->from('hoststatussummary', array( $stats = $this->backend->select()->from('hoststatussummary', array(
'hosts_total', 'hosts_total',
@ -160,10 +160,8 @@ class ListController extends Controller
'service_passive_checks_enabled' 'service_passive_checks_enabled'
), $this->addColumns())); ), $this->addColumns()));
$this->applyRestriction('monitoring/filter/objects', $services); $this->applyRestriction('monitoring/filter/objects', $services);
$this->filterQuery($services);
$this->setupPaginationControl($services); $this->setupPaginationControl($services);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'service_severity' => $this->translate('Service Severity'), 'service_severity' => $this->translate('Service Severity'),
'service_state' => $this->translate('Current Service State'), 'service_state' => $this->translate('Current Service State'),
@ -176,6 +174,8 @@ class ListController extends Controller
'host_address' => $this->translate('Host Address'), 'host_address' => $this->translate('Host Address'),
'host_last_check' => $this->translate('Last Host Check') 'host_last_check' => $this->translate('Last Host Check')
), $services); ), $services);
$this->filterQuery($services);
$this->setupLimitControl();
$stats = $this->backend->select()->from('servicestatussummary', array( $stats = $this->backend->select()->from('servicestatussummary', array(
'services_critical', 'services_critical',
@ -238,10 +238,8 @@ class ListController extends Controller
'service_display_name' 'service_display_name'
)); ));
$this->applyRestriction('monitoring/filter/objects', $downtimes); $this->applyRestriction('monitoring/filter/objects', $downtimes);
$this->filterQuery($downtimes);
$this->setupPaginationControl($downtimes); $this->setupPaginationControl($downtimes);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'downtime_is_in_effect' => $this->translate('Is In Effect'), 'downtime_is_in_effect' => $this->translate('Is In Effect'),
'host_display_name' => $this->translate('Host'), 'host_display_name' => $this->translate('Host'),
@ -254,6 +252,8 @@ class ListController extends Controller
'downtime_scheduled_end' => $this->translate('Scheduled End'), 'downtime_scheduled_end' => $this->translate('Scheduled End'),
'downtime_duration' => $this->translate('Duration') 'downtime_duration' => $this->translate('Duration')
), $downtimes); ), $downtimes);
$this->filterQuery($downtimes);
$this->setupLimitControl();
$this->view->downtimes = $downtimes; $this->view->downtimes = $downtimes;
@ -287,13 +287,13 @@ class ListController extends Controller
'service_display_name' 'service_display_name'
)); ));
$this->applyRestriction('monitoring/filter/objects', $notifications); $this->applyRestriction('monitoring/filter/objects', $notifications);
$this->filterQuery($notifications);
$this->setupPaginationControl($notifications); $this->setupPaginationControl($notifications);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'notification_timestamp' => $this->translate('Notification Start') 'notification_timestamp' => $this->translate('Notification Start')
), $notifications); ), $notifications);
$this->filterQuery($notifications);
$this->setupLimitControl();
$this->view->notifications = $notifications; $this->view->notifications = $notifications;
} }
@ -318,16 +318,16 @@ class ListController extends Controller
'contact_notify_host_timeperiod' 'contact_notify_host_timeperiod'
)); ));
$this->applyRestriction('monitoring/filter/objects', $contacts); $this->applyRestriction('monitoring/filter/objects', $contacts);
$this->filterQuery($contacts);
$this->setupPaginationControl($contacts); $this->setupPaginationControl($contacts);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'contact_name' => $this->translate('Name'), 'contact_name' => $this->translate('Name'),
'contact_alias' => $this->translate('Alias'), 'contact_alias' => $this->translate('Alias'),
'contact_email' => $this->translate('Email'), 'contact_email' => $this->translate('Email'),
'contact_pager' => $this->translate('Pager Address / Number') 'contact_pager' => $this->translate('Pager Address / Number')
), $contacts); ), $contacts);
$this->filterQuery($contacts);
$this->setupLimitControl();
$this->view->contacts = $contacts; $this->view->contacts = $contacts;
} }
@ -389,14 +389,14 @@ class ListController extends Controller
'contact_count' 'contact_count'
)); ));
$this->applyRestriction('monitoring/filter/objects', $contactGroups); $this->applyRestriction('monitoring/filter/objects', $contactGroups);
$this->filterQuery($contactGroups);
$this->setupPaginationControl($contactGroups); $this->setupPaginationControl($contactGroups);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'contactgroup_name' => $this->translate('Contactgroup Name'), 'contactgroup_name' => $this->translate('Contactgroup Name'),
'contactgroup_alias' => $this->translate('Contactgroup Alias') 'contactgroup_alias' => $this->translate('Contactgroup Alias')
), $contactGroups); ), $contactGroups);
$this->filterQuery($contactGroups);
$this->setupLimitControl();
$this->view->contactGroups = $contactGroups; $this->view->contactGroups = $contactGroups;
} }
@ -430,10 +430,8 @@ class ListController extends Controller
'service_display_name' 'service_display_name'
)); ));
$this->applyRestriction('monitoring/filter/objects', $comments); $this->applyRestriction('monitoring/filter/objects', $comments);
$this->filterQuery($comments);
$this->setupPaginationControl($comments); $this->setupPaginationControl($comments);
$this->setupLimitControl();
$this->setupSortControl( $this->setupSortControl(
array( array(
'comment_timestamp' => $this->translate('Comment Timestamp'), 'comment_timestamp' => $this->translate('Comment Timestamp'),
@ -444,6 +442,8 @@ class ListController extends Controller
), ),
$comments $comments
); );
$this->filterQuery($comments);
$this->setupLimitControl();
$this->view->comments = $comments; $this->view->comments = $comments;
@ -480,14 +480,14 @@ class ListController extends Controller
'services_warning_unhandled' 'services_warning_unhandled'
)); ));
$this->applyRestriction('monitoring/filter/objects', $serviceGroups); $this->applyRestriction('monitoring/filter/objects', $serviceGroups);
$this->filterQuery($serviceGroups);
$this->setupPaginationControl($serviceGroups); $this->setupPaginationControl($serviceGroups);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'servicegroup_alias' => $this->translate('Service Group Name'), 'servicegroup_alias' => $this->translate('Service Group Name'),
'services_total' => $this->translate('Total Services') 'services_total' => $this->translate('Total Services')
), $serviceGroups); ), $serviceGroups);
$this->filterQuery($serviceGroups);
$this->setupLimitControl();
$this->view->serviceGroups = $serviceGroups; $this->view->serviceGroups = $serviceGroups;
} }
@ -526,15 +526,15 @@ class ListController extends Controller
'services_warning_unhandled' 'services_warning_unhandled'
)); ));
$this->applyRestriction('monitoring/filter/objects', $hostGroups); $this->applyRestriction('monitoring/filter/objects', $hostGroups);
$this->filterQuery($hostGroups);
$this->setupPaginationControl($hostGroups); $this->setupPaginationControl($hostGroups);
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'hostgroup_alias' => $this->translate('Host Group Name'), 'hostgroup_alias' => $this->translate('Host Group Name'),
'hosts_total' => $this->translate('Total Hosts'), 'hosts_total' => $this->translate('Total Hosts'),
'services_total' => $this->translate('Total Services') 'services_total' => $this->translate('Total Services')
), $hostGroups); ), $hostGroups);
$this->filterQuery($hostGroups);
$this->setupLimitControl();
$this->view->hostGroups = $hostGroups; $this->view->hostGroups = $hostGroups;
} }
@ -560,13 +560,13 @@ class ListController extends Controller
)); ));
$this->applyRestriction('monitoring/filter/objects', $query); $this->applyRestriction('monitoring/filter/objects', $query);
$this->filterQuery($query);
$this->view->history = $query; $this->view->history = $query;
$this->setupLimitControl();
$this->setupSortControl(array( $this->setupSortControl(array(
'timestamp' => $this->translate('Occurence') 'timestamp' => $this->translate('Occurence')
), $query); ), $query);
$this->filterQuery($query);
$this->setupLimitControl();
} }
public function servicegridAction() public function servicegridAction()

View File

@ -32,33 +32,44 @@ class Controller extends IcingaWebController
protected function handleFormatRequest($query) protected function handleFormatRequest($query)
{ {
if ($this->_getParam('format') === 'sql') { $desiredContentType = $this->getRequest()->getHeader('Accept');
echo '<pre>' if ($desiredContentType === 'application/json') {
. htmlspecialchars(wordwrap($query->dump())) $desiredFormat = 'json';
. '</pre>'; } elseif ($desiredContentType === 'text/csv') {
exit; $desiredFormat = 'csv';
} else {
$desiredFormat = strtolower($this->params->get('format', 'html'));
} }
if ($this->_getParam('format') === 'json'
|| $this->_request->getHeader('Accept') === 'application/json') { if ($desiredFormat !== 'html' && ! $this->params->has('limit')) {
$response = $this->getResponse(); $query->limit(); // Resets any default limit and offset
$response
->setHeader('Content-Type', 'application/json')
->setHeader('Cache-Control', 'no-store')
->setHeader('Content-Disposition', 'inline; filename=' . $this->getRequest()->getActionName() . '.json')
->appendBody(Json::encode($query->getQuery()->fetchAll()))
->sendResponse();
exit;
} }
if ($this->_getParam('format') === 'csv'
|| $this->_request->getHeader('Accept') === 'text/csv') { switch ($desiredFormat)
$response = $this->getResponse(); {
$response case 'sql':
->setHeader('Content-Type', 'text/csv') echo '<pre>'
->setHeader('Cache-Control', 'no-store') . htmlspecialchars(wordwrap($query->dump()))
->setHeader('Content-Disposition', 'attachment; filename=' . $this->getRequest()->getActionName() . '.csv') . '</pre>';
->appendBody((string) Csv::fromQuery($query)) exit;
->sendResponse(); case 'json':
exit; $response = $this->getResponse();
$response
->setHeader('Content-Type', 'application/json')
->setHeader('Cache-Control', 'no-store')
->setHeader('Content-Disposition', 'inline; filename=' . $this->getRequest()->getActionName() . '.json')
->appendBody(Json::encode($query->getQuery()->fetchAll()))
->sendResponse();
exit;
case 'csv':
$response = $this->getResponse();
$response
->setHeader('Content-Type', 'text/csv')
->setHeader('Cache-Control', 'no-store')
->setHeader('Content-Disposition', 'attachment; filename=' . $this->getRequest()->getActionName() . '.csv')
->appendBody((string) Csv::fromQuery($query))
->sendResponse();
exit;
} }
} }