From 1cfcb934b097846ed7d9be361129b785d44ea406 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Fri, 6 Mar 2015 17:30:59 +0100 Subject: [PATCH] Deduplicate state summary Use existing partials for rendering the state summary instead of a new one. refs #8565 --- .../controllers/HostsController.php | 14 +----- .../controllers/ServicesController.php | 36 +-------------- .../views/scripts/hosts/show.phtml | 2 +- .../partials/host/objects-tinysummary.phtml | 44 ------------------- .../service/objects-tinysummary.phtml | 42 ------------------ .../views/scripts/services/show.phtml | 2 +- .../library/Monitoring/Object/HostList.php | 27 ++++++++++++ .../library/Monitoring/Object/ObjectList.php | 11 +++++ .../library/Monitoring/Object/ServiceList.php | 42 ++++++++++++++++++ 9 files changed, 86 insertions(+), 134 deletions(-) delete mode 100644 modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml delete mode 100644 modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml diff --git a/modules/monitoring/application/controllers/HostsController.php b/modules/monitoring/application/controllers/HostsController.php index ec4e470a2..55f8a6e60 100644 --- a/modules/monitoring/application/controllers/HostsController.php +++ b/modules/monitoring/application/controllers/HostsController.php @@ -110,16 +110,7 @@ class Monitoring_HostsController extends Controller $acknowledgedObjects = array(); $objectsInDowntime = array(); $downtimeFilterExpressions = array(); - $hostStates = array( - 'hosts_' . Host::getStateText(Host::STATE_UP) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UP) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) . '_unhandled' => 0, - ); + foreach ($this->hostList as $host) { /** @var Host $host */ $unhandled = (bool) $host->problem === true && (bool) $host->handled === false; @@ -134,7 +125,6 @@ class Monitoring_HostsController extends Controller $objectsInDowntime[] = $host; $downtimeFilterExpressions[] = Filter::where('downtime_host', $host->getName()); } - ++$hostStates['hosts_' . $host::getStateText($host->state) . ($unhandled ? '_unhandled' : '')]; } if (! empty($acknowledgedObjects)) { $removeAckForm = new RemoveAcknowledgementCommandForm(); @@ -150,7 +140,7 @@ class Monitoring_HostsController extends Controller $this->view->processCheckResultAllLink = Url::fromRequest()->setPath('monitoring/hosts/process-check-result'); $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/hosts/add-comment'); $this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/hosts/delete-comment'); - $this->view->hostStates = (object)$hostStates; + $this->view->stats = (object)$this->hostList->getStateSummary(); $this->view->objects = $this->hostList; $this->view->unhandledObjects = $unhandledObjects; $unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString(); diff --git a/modules/monitoring/application/controllers/ServicesController.php b/modules/monitoring/application/controllers/ServicesController.php index 1514a0ea0..dcd95184c 100644 --- a/modules/monitoring/application/controllers/ServicesController.php +++ b/modules/monitoring/application/controllers/ServicesController.php @@ -132,35 +132,10 @@ class Monitoring_ServicesController extends Controller $acknowledgedObjects = array(); $objectsInDowntime = array(); $downtimeFilterExpressions = array(); - $serviceStates = array( - 'services_' . Service::getStateText(Service::STATE_OK) => 0, - 'services_' . Service::getStateText(Service::STATE_OK) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_WARNING) => 0, - 'services_' . Service::getStateText(Service::STATE_WARNING) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_CRITICAL) => 0, - 'services_' . Service::getStateText(Service::STATE_CRITICAL) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_UNKNOWN) => 0, - 'services_' . Service::getStateText(Service::STATE_UNKNOWN) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_PENDING) => 0, - 'services_' . Service::getStateText(Service::STATE_PENDING) . '_unhandled' => 0 - ); - $knownHostStates = array(); - $hostStates = array( - 'hosts_' . Host::getStateText(Host::STATE_UP) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UP) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) . '_unhandled' => 0 - ); - foreach ($this->serviceList as $service) { - $unhandled = false; + foreach ($this->serviceList as $service) { /** @var Service $service */ if ((bool) $service->problem === true && (bool) $service->handled === false) { - $unhandled = true; $unhandledObjects[] = $service; $unhandledFilterExpressions[] = Filter::matchAll( Filter::where('host', $service->getHost()->getName()), @@ -177,12 +152,6 @@ class Monitoring_ServicesController extends Controller Filter::where('downtime_service', $service->getName()) ); } - - ++$serviceStates['services_' . $service::getStateText($service->state) . ($unhandled ? '_unhandled' : '')]; - if (! isset($knownHostStates[$service->getHost()->getName()])) { - $knownHostStates[$service->getHost()->getName()] = true; - ++$hostStates['hosts_' . $service->getHost()->getStateText($service->host_state)]; - } } if (! empty($acknowledgedObjects)) { $removeAckForm = new RemoveAcknowledgementCommandForm(); @@ -208,8 +177,7 @@ class Monitoring_ServicesController extends Controller ); $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/services/add-comment'); $this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/services/delete-comment'); - $this->view->hostStates = (object)$hostStates; - $this->view->serviceStates = (object)$serviceStates; + $this->view->stats = $this->serviceList->getStateSummary(); $this->view->objects = $this->serviceList; $this->view->unhandledObjects = $unhandledObjects; $unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString(); diff --git a/modules/monitoring/application/views/scripts/hosts/show.phtml b/modules/monitoring/application/views/scripts/hosts/show.phtml index f4ac40904..d56f92af4 100644 --- a/modules/monitoring/application/views/scripts/hosts/show.phtml +++ b/modules/monitoring/application/views/scripts/hosts/show.phtml @@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm; - render('partials/host/objects-tinysummary.phtml') ?> + render('list/components/hostssummary.phtml') ?> render('partials/host/objects-header.phtml'); ?> diff --git a/modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml b/modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml deleted file mode 100644 index fa841c633..000000000 --- a/modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml +++ /dev/null @@ -1,44 +0,0 @@ - - -

- qlink( - sprintf($this->translate('%d Hosts Selected:'), count($objects)), - $listAllLink - ); ?> - - - hosts_up): ?> - - - - hosts_up ?> - - - - - 'down', - 2 => 'unreachable', - 99 => 'pending' - ) as $stateId => $state) { - - $stateName = 'hosts_' . $state; - $unhandledStateName = $stateName . '_unhandled'; - if ($hostStates->$unhandledStateName) { - echo '' . $hostStates->$unhandledStateName . ''; - } - if ($hostStates->$stateName) { - echo '' . $hostStates->$stateName . ''; - } - if ($hostStates->$unhandledStateName) { - echo ''; - } - $stateName .= '_unhandled'; - }?> - - -

\ No newline at end of file diff --git a/modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml b/modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml deleted file mode 100644 index 7707651d2..000000000 --- a/modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml +++ /dev/null @@ -1,42 +0,0 @@ - - -

- qlink( - sprintf($this->translate('%d Services Selected:'), count($objects)), - $listAllLink - ); ?> - - - services_ok): ?> - - - services_ok ?> - - - - 'critical', - 3 => 'unknown', - 1 => 'warning', - 4 => 'pending' - ) as $stateId => $state) { - - $stateName = 'services_' . $state; - $unhandledStateName = $stateName . '_unhandled'; - if ($serviceStates->$unhandledStateName) { - echo '' . $serviceStates->$unhandledStateName . ''; - } - if ($serviceStates->$stateName) { - echo '' . $serviceStates->$stateName . ''; - } - if ($serviceStates->$unhandledStateName) { - echo ''; - } - $stateName .= '_unhandled'; - }?> - -

\ No newline at end of file diff --git a/modules/monitoring/application/views/scripts/services/show.phtml b/modules/monitoring/application/views/scripts/services/show.phtml index 08e1737f3..1daa1a956 100644 --- a/modules/monitoring/application/views/scripts/services/show.phtml +++ b/modules/monitoring/application/views/scripts/services/show.phtml @@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm; - render('partials/service/objects-tinysummary.phtml') ?> + render('list/components/servicesummary.phtml') ?> render('partials/service/objects-header.phtml'); ?> diff --git a/modules/monitoring/library/Monitoring/Object/HostList.php b/modules/monitoring/library/Monitoring/Object/HostList.php index f0adced24..94157b6fc 100644 --- a/modules/monitoring/library/Monitoring/Object/HostList.php +++ b/modules/monitoring/library/Monitoring/Object/HostList.php @@ -25,4 +25,31 @@ class HostList extends ObjectList } return $hosts; } + + /** + * Create a state summary of all hosts that can be consumed by hostssummary.phtml + * + * @return object The summary + */ + public function getStateSummary() + { + $hostStates = $this->prepareStateNames('hosts_', array( + Host::getStateText(Host::STATE_UP), + Host::getStateText(Host::STATE_DOWN), + Host::getStateText(Host::STATE_UNREACHABLE), + Host::getStateText(Host::STATE_PENDING) + )); + + foreach ($this as $host) { + $unhandled = (bool) $host->problem === true && (bool) $host->handled === false; + + $stateName = 'hosts_' . $host::getStateText($host->state); + ++$hostStates[$stateName]; + ++$hostStates[$stateName. ($unhandled ? '_unhandled' : '_handled')]; + } + + $hostStates['hosts_total'] = count($this); + + return (object)$hostStates; + } } diff --git a/modules/monitoring/library/Monitoring/Object/ObjectList.php b/modules/monitoring/library/Monitoring/Object/ObjectList.php index 146499805..234ccb9bb 100644 --- a/modules/monitoring/library/Monitoring/Object/ObjectList.php +++ b/modules/monitoring/library/Monitoring/Object/ObjectList.php @@ -85,4 +85,15 @@ abstract class ObjectList implements Countable, IteratorAggregate { return $this->backend->select()->from('comment')->applyFilter($this->filter); } + + protected function prepareStateNames($prefix, array $names) { + $new = array(); + foreach ($names as $name) { + $new[$prefix . $name] = 0; + $new[$prefix . $name . '_handled'] = 0; + $new[$prefix . $name . '_unhandled'] = 0; + } + $new[$prefix . 'total'] = 0; + return $new; + } } diff --git a/modules/monitoring/library/Monitoring/Object/ServiceList.php b/modules/monitoring/library/Monitoring/Object/ServiceList.php index d9ffa0d8e..0625ae0f8 100644 --- a/modules/monitoring/library/Monitoring/Object/ServiceList.php +++ b/modules/monitoring/library/Monitoring/Object/ServiceList.php @@ -25,4 +25,46 @@ class ServiceList extends ObjectList } return $services; } + + /** + * Create a state summary of all services that can be consumed by servicesummary.phtml + * + * @return object The summary + */ + public function getStateSummary() + { + $serviceStates = $this->prepareStateNames('services_', array( + Service::getStateText(Service::STATE_OK), + Service::getStateText(Service::STATE_WARNING), + Service::getStateText(Service::STATE_CRITICAL), + Service::getStateText(Service::STATE_UNKNOWN), + Service::getStateText(Service::STATE_PENDING), + )); + + $hostStates = $this->prepareStateNames('hosts_', array( + Host::getStateText(Host::STATE_UP), + Host::getStateText(Host::STATE_DOWN), + Host::getStateText(Host::STATE_UNREACHABLE), + Host::getStateText(Host::STATE_PENDING), + )); + + foreach ($this as $service) { + $unhandled = false; + if ((bool) $service->problem === true && (bool) $service->handled === false) { + $unhandled = true; + } + + $stateName = 'services_' . $service::getStateText($service->state); + ++$serviceStates[$stateName]; + ++$serviceStates[$stateName . ($unhandled ? '_unhandled' : '_handled')]; + if (! isset($knownHostStates[$service->getHost()->getName()])) { + $knownHostStates[$service->getHost()->getName()] = true; + ++$hostStates['hosts_' . $service->getHost()->getStateText($service->host_state)]; + } + } + + $serviceStates['services_total'] = count($this); + + return (object)$serviceStates; + } }