Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monitoring/MenuRenderers: unify logic, perftuning
This reduces duplicate code, query will be fired only once right now. fixes #7554
- Loading branch information
1 parent
0d4d493
commit 0992f6c
Showing
4 changed files
with
106 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
); | ||
} |
36 changes: 5 additions & 31 deletions
36
library/Icinga/Web/Menu/UnhandledServiceMenuItemRenderer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
); | ||
} |