Monitoring/MenuRenderers: unify logic, perftuning

This reduces duplicate code, query will be fired only once right now.

fixes #7554
This commit is contained in:
Thomas Gelf 2014-11-11 21:50:49 +01:00
parent 0d4d4930a9
commit 0992f6cc15
4 changed files with 106 additions and 96 deletions

View File

@ -0,0 +1,90 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Menu;
use Icinga\Module\Monitoring\Backend;
use Icinga\Web\Menu;
use Icinga\Web\Url;
class MonitoringMenuItemRenderer implements MenuItemRenderer {
protected static $summary;
protected $columns = array();
protected static function summary($column = null)
{
if (self::$summary === null) {
self::$summary = Backend::createBackend()->select()->from(
'statusSummary',
array(
'hosts_down_unhandled',
'services_critical_unhandled'
)
)->getQuery()->fetchRow();
}
if ($column === null) {
return self::$summary;
} elseif (isset(self::$summary->$column)) {
return self::$summary->$column;
} else {
return null;
}
}
protected function getBadgeTitle()
{
$translations = array(
'hosts_down_unhandled' => mt('monitoring', '%d unhandled hosts down'),
'services_critical_unhandled' => mt('monitoring', '%d unhandled services critical')
);
$titles = array();
$sum = $this->summary();
foreach ($this->columns as $col) {
if (isset($sum->$col) && $sum->$col > 0) {
$titles[] = sprintf($translations[$col], $sum->$col);
}
}
return implode(', ', $titles);
}
protected function countItems()
{
$sum = self::summary();
$count = 0;
foreach ($this->columns as $col) {
if (isset($sum->$col)) {
$count += $sum->$col;
}
}
return $count;
}
public function render(Menu $menu)
{
$count = $this->countItems();
$badge = '';
if ($count) {
$badge = sprintf(
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
$this->getBadgeTitle(),
$count
);
}
return sprintf(
'<a href="%s">%s%s%s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
}

View File

@ -1,39 +1,11 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Menu;
use Icinga\Module\Monitoring\Backend;
use Icinga\Web\Menu;
use Icinga\Web\Url;
class ProblemMenuItemRenderer implements MenuItemRenderer {
public function render(Menu $menu)
{
$statusSummary = Backend::createBackend()
->select()->from(
'statusSummary',
array(
'hosts_down_unhandled',
'services_critical_unhandled'
)
)->getQuery()->fetchRow();
$unhandled = $statusSummary->hosts_down_unhandled + $statusSummary->services_critical_unhandled;
$badge = '';
if ($unhandled) {
$badge = sprintf(
'<div class="badge-container"><span class="badge badge-critical">%s</span></div>',
$unhandled
);
}
return sprintf(
'<a href="%s">%s%s %s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
class ProblemMenuItemRenderer extends MonitoringMenuItemRenderer
{
protected $columns = array(
'hosts_down_unhandled',
'services_critical_unhandled'
);
}

View File

@ -1,38 +1,12 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Menu;
use Icinga\Module\Monitoring\Backend;
use Icinga\Web\Menu;
use Icinga\Web\Url;
class UnhandledHostMenuItemRenderer implements MenuItemRenderer {
public function render(Menu $menu)
{
$statusSummary = Backend::createBackend()
->select()->from(
'statusSummary',
array(
'hosts_down_unhandled'
)
)->getQuery()->fetchRow();
$badge = '';
if ($statusSummary->hosts_down_unhandled) {
$badge = sprintf(
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
t(sprintf('%d unhandled host problems', $statusSummary->hosts_down_unhandled)),
$statusSummary->hosts_down_unhandled
);
}
return sprintf(
'<a href="%s">%s%s %s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
class UnhandledHostMenuItemRenderer extends MonitoringMenuItemRenderer
{
protected $columns = array(
'hosts_down_unhandled',
);
}

View File

@ -1,38 +1,12 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Menu;
use Icinga\Module\Monitoring\Backend;
use Icinga\Web\Menu;
use Icinga\Web\Url;
class UnhandledServiceMenuItemRenderer implements MenuItemRenderer {
public function render(Menu $menu)
{
$statusSummary = Backend::createBackend()
->select()->from(
'statusSummary',
array(
'services_critical_unhandled'
)
)->getQuery()->fetchRow();
$badge = '';
if ($statusSummary->services_critical_unhandled) {
$badge = sprintf(
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
t(sprintf('%d unhandled service problems', $statusSummary->services_critical_unhandled)),
$statusSummary->services_critical_unhandled
);
}
return sprintf(
'<a href="%s">%s%s %s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
class UnhandledServiceMenuItemRenderer extends MonitoringMenuItemRenderer
{
protected $columns = array(
'services_critical_unhandled'
);
}