Deduplicate state summary

Use existing partials for rendering the state summary instead of a new one.

refs #8565
This commit is contained in:
Matthias Jentsch 2015-03-06 17:30:59 +01:00
parent 9c45e99b57
commit 1cfcb934b0
9 changed files with 86 additions and 134 deletions

View File

@ -110,16 +110,7 @@ class Monitoring_HostsController extends Controller
$acknowledgedObjects = array(); $acknowledgedObjects = array();
$objectsInDowntime = array(); $objectsInDowntime = array();
$downtimeFilterExpressions = 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) { foreach ($this->hostList as $host) {
/** @var Host $host */ /** @var Host $host */
$unhandled = (bool) $host->problem === true && (bool) $host->handled === false; $unhandled = (bool) $host->problem === true && (bool) $host->handled === false;
@ -134,7 +125,6 @@ class Monitoring_HostsController extends Controller
$objectsInDowntime[] = $host; $objectsInDowntime[] = $host;
$downtimeFilterExpressions[] = Filter::where('downtime_host', $host->getName()); $downtimeFilterExpressions[] = Filter::where('downtime_host', $host->getName());
} }
++$hostStates['hosts_' . $host::getStateText($host->state) . ($unhandled ? '_unhandled' : '')];
} }
if (! empty($acknowledgedObjects)) { if (! empty($acknowledgedObjects)) {
$removeAckForm = new RemoveAcknowledgementCommandForm(); $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->processCheckResultAllLink = Url::fromRequest()->setPath('monitoring/hosts/process-check-result');
$this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/hosts/add-comment'); $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/hosts/add-comment');
$this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/hosts/delete-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->objects = $this->hostList;
$this->view->unhandledObjects = $unhandledObjects; $this->view->unhandledObjects = $unhandledObjects;
$unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString(); $unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString();

View File

@ -132,35 +132,10 @@ class Monitoring_ServicesController extends Controller
$acknowledgedObjects = array(); $acknowledgedObjects = array();
$objectsInDowntime = array(); $objectsInDowntime = array();
$downtimeFilterExpressions = 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 */ /** @var Service $service */
if ((bool) $service->problem === true && (bool) $service->handled === false) { if ((bool) $service->problem === true && (bool) $service->handled === false) {
$unhandled = true;
$unhandledObjects[] = $service; $unhandledObjects[] = $service;
$unhandledFilterExpressions[] = Filter::matchAll( $unhandledFilterExpressions[] = Filter::matchAll(
Filter::where('host', $service->getHost()->getName()), Filter::where('host', $service->getHost()->getName()),
@ -177,12 +152,6 @@ class Monitoring_ServicesController extends Controller
Filter::where('downtime_service', $service->getName()) 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)) { if (! empty($acknowledgedObjects)) {
$removeAckForm = new RemoveAcknowledgementCommandForm(); $removeAckForm = new RemoveAcknowledgementCommandForm();
@ -208,8 +177,7 @@ class Monitoring_ServicesController extends Controller
); );
$this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/services/add-comment'); $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/services/add-comment');
$this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/services/delete-comment'); $this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/services/delete-comment');
$this->view->hostStates = (object)$hostStates; $this->view->stats = $this->serviceList->getStateSummary();
$this->view->serviceStates = (object)$serviceStates;
$this->view->objects = $this->serviceList; $this->view->objects = $this->serviceList;
$this->view->unhandledObjects = $unhandledObjects; $this->view->unhandledObjects = $unhandledObjects;
$unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString(); $unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString();

View File

@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
<?= $tabs; ?> <?= $tabs; ?>
<?php endif ?> <?php endif ?>
<?= $this->render('partials/host/objects-tinysummary.phtml') ?> <?= $this->render('list/components/hostssummary.phtml') ?>
<?= $this->render('partials/host/objects-header.phtml'); ?> <?= $this->render('partials/host/objects-header.phtml'); ?>
</div> </div>

View File

@ -1,44 +0,0 @@
<?php
use Icinga\Web\Url;
?>
<h1 class="tinystatesummary">
<?= $this->qlink(
sprintf($this->translate('%d Hosts Selected:'), count($objects)),
$listAllLink
); ?>
<span class="badges">
<?php if ($hostStates->hosts_up): ?>
<?php $selfUrl = Url::fromPath('monitoring/list/hosts'); ?>
<span class="state up"><b>
<?= $hostStates->hosts_up ?>
</b></span>
<?php endif ?>
<?php
foreach (
array(
1 => 'down',
2 => 'unreachable',
99 => 'pending'
) as $stateId => $state) {
$stateName = 'hosts_' . $state;
$unhandledStateName = $stateName . '_unhandled';
if ($hostStates->$unhandledStateName) {
echo '<span class="state ' . $state . '"><b>' . $hostStates->$unhandledStateName . '</b>';
}
if ($hostStates->$stateName) {
echo '<span class="state ' . $state . ' handled"><b>' . $hostStates->$stateName . '</b></span>';
}
if ($hostStates->$unhandledStateName) {
echo '</span>';
}
$stateName .= '_unhandled';
}?>
</span>
</h1>

View File

@ -1,42 +0,0 @@
<?php
use Icinga\Web\Url;
?>
<h1 class="tinystatesummary">
<?= $this->qlink(
sprintf($this->translate('%d Services Selected:'), count($objects)),
$listAllLink
); ?>
<span class="badges">
<?php if ($serviceStates->services_ok): ?>
<?php $selfUrl = Url::fromPath('monitoring/list/services'); ?>
<span class="state ok"><b>
<?= $serviceStates->services_ok ?>
</b></span>
<?php endif ?>
<?php
foreach (
array(
2 => 'critical',
3 => 'unknown',
1 => 'warning',
4 => 'pending'
) as $stateId => $state) {
$stateName = 'services_' . $state;
$unhandledStateName = $stateName . '_unhandled';
if ($serviceStates->$unhandledStateName) {
echo '<span class="state ' . $state . '"><b>' . $serviceStates->$unhandledStateName . '</b>';
}
if ($serviceStates->$stateName) {
echo '<span class="state ' . $state . ' handled"><b>' . $serviceStates->$stateName . '</b></span>';
}
if ($serviceStates->$unhandledStateName) {
echo '</span>';
}
$stateName .= '_unhandled';
}?>
</span>
</h1>

View File

@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
<?= $tabs; ?> <?= $tabs; ?>
<?php endif ?> <?php endif ?>
<?= $this->render('partials/service/objects-tinysummary.phtml') ?> <?= $this->render('list/components/servicesummary.phtml') ?>
<?= $this->render('partials/service/objects-header.phtml'); ?> <?= $this->render('partials/service/objects-header.phtml'); ?>
</div> </div>

View File

@ -25,4 +25,31 @@ class HostList extends ObjectList
} }
return $hosts; 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;
}
} }

View File

@ -85,4 +85,15 @@ abstract class ObjectList implements Countable, IteratorAggregate
{ {
return $this->backend->select()->from('comment')->applyFilter($this->filter); 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;
}
} }

View File

@ -25,4 +25,46 @@ class ServiceList extends ObjectList
} }
return $services; 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;
}
} }