Monitoring/MenuRenderers: unify logic, perftuning
This reduces duplicate code, query will be fired only once right now. fixes #7554
This commit is contained in:
parent
0d4d4930a9
commit
0992f6cc15
|
@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,39 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
|
|
||||||
namespace Icinga\Web\Menu;
|
namespace Icinga\Web\Menu;
|
||||||
|
|
||||||
use Icinga\Module\Monitoring\Backend;
|
class ProblemMenuItemRenderer extends MonitoringMenuItemRenderer
|
||||||
use Icinga\Web\Menu;
|
{
|
||||||
use Icinga\Web\Url;
|
protected $columns = array(
|
||||||
|
'hosts_down_unhandled',
|
||||||
class ProblemMenuItemRenderer implements MenuItemRenderer {
|
'services_critical_unhandled'
|
||||||
|
);
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
|
|
||||||
namespace Icinga\Web\Menu;
|
namespace Icinga\Web\Menu;
|
||||||
|
|
||||||
use Icinga\Module\Monitoring\Backend;
|
|
||||||
use Icinga\Web\Menu;
|
use Icinga\Web\Menu;
|
||||||
use Icinga\Web\Url;
|
|
||||||
|
|
||||||
class UnhandledHostMenuItemRenderer implements MenuItemRenderer {
|
class UnhandledHostMenuItemRenderer extends MonitoringMenuItemRenderer
|
||||||
|
{
|
||||||
public function render(Menu $menu)
|
protected $columns = array(
|
||||||
{
|
'hosts_down_unhandled',
|
||||||
$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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
|
|
||||||
namespace Icinga\Web\Menu;
|
namespace Icinga\Web\Menu;
|
||||||
|
|
||||||
use Icinga\Module\Monitoring\Backend;
|
|
||||||
use Icinga\Web\Menu;
|
use Icinga\Web\Menu;
|
||||||
use Icinga\Web\Url;
|
|
||||||
|
|
||||||
class UnhandledServiceMenuItemRenderer implements MenuItemRenderer {
|
class UnhandledServiceMenuItemRenderer extends MonitoringMenuItemRenderer
|
||||||
|
{
|
||||||
public function render(Menu $menu)
|
protected $columns = array(
|
||||||
{
|
'services_critical_unhandled'
|
||||||
$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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue