Skip to content

Commit

Permalink
Deduplicate state summary
Browse files Browse the repository at this point in the history
Use existing partials for rendering the state summary instead of a new one.

refs #8565
  • Loading branch information
majentsch committed Mar 6, 2015
1 parent 9c45e99 commit 1cfcb93
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 134 deletions.
14 changes: 2 additions & 12 deletions modules/monitoring/application/controllers/HostsController.php
Expand Up @@ -110,16 +110,7 @@ public function showAction()
$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;
Expand All @@ -134,7 +125,6 @@ public function showAction()
$objectsInDowntime[] = $host;
$downtimeFilterExpressions[] = Filter::where('downtime_host', $host->getName());
}
++$hostStates['hosts_' . $host::getStateText($host->state) . ($unhandled ? '_unhandled' : '')];
}
if (! empty($acknowledgedObjects)) {
$removeAckForm = new RemoveAcknowledgementCommandForm();
Expand All @@ -150,7 +140,7 @@ public function showAction()
$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();
Expand Down
36 changes: 2 additions & 34 deletions modules/monitoring/application/controllers/ServicesController.php
Expand Up @@ -132,35 +132,10 @@ public function showAction()
$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()),
Expand All @@ -177,12 +152,6 @@ public function showAction()
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();
Expand All @@ -208,8 +177,7 @@ public function showAction()
);
$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();
Expand Down
Expand Up @@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
<?= $tabs; ?>
<?php endif ?>

<?= $this->render('partials/host/objects-tinysummary.phtml') ?>
<?= $this->render('list/components/hostssummary.phtml') ?>
<?= $this->render('partials/host/objects-header.phtml'); ?>

</div>
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
<?= $tabs; ?>
<?php endif ?>

<?= $this->render('partials/service/objects-tinysummary.phtml') ?>
<?= $this->render('list/components/servicesummary.phtml') ?>
<?= $this->render('partials/service/objects-header.phtml'); ?>

</div>
Expand Down
27 changes: 27 additions & 0 deletions modules/monitoring/library/Monitoring/Object/HostList.php
Expand Up @@ -25,4 +25,31 @@ protected function fetchObjects()
}
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;
}
}
11 changes: 11 additions & 0 deletions modules/monitoring/library/Monitoring/Object/ObjectList.php
Expand Up @@ -85,4 +85,15 @@ public function getComments()
{
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;
}
}
42 changes: 42 additions & 0 deletions modules/monitoring/library/Monitoring/Object/ServiceList.php
Expand Up @@ -25,4 +25,46 @@ protected function fetchObjects()
}
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;
}
}

0 comments on commit 1cfcb93

Please sign in to comment.