monitoring: Adjust custom navigation renderer for DataView badges

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-04 13:50:53 +02:00
parent a96bead01b
commit 7a5dceac7b
2 changed files with 76 additions and 76 deletions

View File

@ -90,7 +90,7 @@ $this->provideSearchUrl($this->translate('Servicegroups'), 'monitoring/list/serv
*/ */
$section = $this->menuSection($this->translate('Problems'), array( $section = $this->menuSection($this->translate('Problems'), array(
'renderer' => array( 'renderer' => array(
'SummaryMenuItemRenderer', 'SummaryNavigationItemRenderer',
'state' => 'critical' 'state' => 'critical'
), ),
'icon' => 'block', 'icon' => 'block',
@ -98,7 +98,7 @@ $section = $this->menuSection($this->translate('Problems'), array(
)); ));
$section->add($this->translate('Unhandled Hosts'), array( $section->add($this->translate('Unhandled Hosts'), array(
'renderer' => array( 'renderer' => array(
'Icinga\Module\Monitoring\Web\Menu\MonitoringBadgeMenuItemRenderer', 'MonitoringBadgeNavigationItemRenderer',
'columns' => array( 'columns' => array(
'hosts_down_unhandled' => $this->translate('%d unhandled hosts down') 'hosts_down_unhandled' => $this->translate('%d unhandled hosts down')
), ),
@ -110,7 +110,7 @@ $section->add($this->translate('Unhandled Hosts'), array(
)); ));
$section->add($this->translate('Unhandled Services'), array( $section->add($this->translate('Unhandled Services'), array(
'renderer' => array( 'renderer' => array(
'Icinga\Module\Monitoring\Web\Menu\MonitoringBadgeMenuItemRenderer', 'MonitoringBadgeNavigationItemRenderer',
'columns' => array( 'columns' => array(
'services_critical_unhandled' => $this->translate('%d unhandled services critical') 'services_critical_unhandled' => $this->translate('%d unhandled services critical')
), ),

View File

@ -1,23 +1,26 @@
<?php <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */ /* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Web\Menu; namespace Icinga\Module\Monitoring\Web\Navigation\Renderer;
use Exception;
use Icinga\Authentication\Auth; use Icinga\Authentication\Auth;
use Icinga\Data\ConfigObject;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Data\Filterable; use Icinga\Data\Filterable;
use Icinga\Web\Menu;
use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Menu\BadgeMenuItemRenderer; use Icinga\Web\Navigation\Renderer\SummaryNavigationItemRenderer;
/** /**
* Render generic dataView columns as badges in MenuItems * Render generic dataView columns as badges in MenuItems
* *
* Renders numeric data view column values into menu item badges, fully configurable * Renders numeric data view column values into menu item badges, fully configurable
* and with a caching mechanism to prevent needless requests to the same data view * and with a caching mechanism to prevent needless requests to the same data view.
*
* It is possible to configure the class of the rendered badge as option 'class', the
* column to fetch using the option 'column' and the dataView from which the columns
* will be fetched using the option 'dataView'.
*/ */
class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer class MonitoringBadgeNavigationItemRenderer extends SummaryNavigationItemRenderer
{ {
/** /**
* Caches the responses for all executed summaries * Caches the responses for all executed summaries
@ -35,7 +38,7 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer
protected static $dataViews = array(); protected static $dataViews = array();
/** /**
* The data view displayed by this menu item * The dataview referred to by the navigation item
* *
* @var string * @var string
*/ */
@ -49,36 +52,56 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer
protected $columns; protected $columns;
/** /**
* The titles that will be used to render this menu item tooltip * Set the dataview referred to by the navigation item
* *
* @var String[] * @param string $dataView
*
* @return $this
*/ */
protected $titles; public function setDataView($dataView)
/**
* The class of the badge element
*
* @var string
*/
protected $state;
/**
* Create a new instance of ColumnMenuItemRenderer
*
* It is possible to configure the class of the rendered badge as option 'class', the column
* to fetch using the option 'column' and the dataView from which the columns will be
* fetched using the option 'dataView'.
*
* @param $configuration ConfigObject The configuration to use
*/
public function __construct(ConfigObject $configuration)
{ {
parent::__construct($configuration); $this->dataView = $dataView;
return $this;
}
$this->columns = $configuration->get('columns'); /**
$this->state = $configuration->get('state'); * Return the dataview referred to by the navigation item
$this->dataView = $configuration->get('dataView'); *
* @return string
*/
public function getDataView()
{
return $this->dataView;
}
/**
* Set the columns and titles displayed in the badge
*
* @param array $columns
*
* @return $this
*/
public function setColumns(array $columns)
{
$this->columns = $columns;
return $this;
}
/**
* Return the columns and titles displayed in the badge
*
* @return array
*/
public function getColumns()
{
return $this->columns;
}
/**
* {@inheritdoc}
*/
public function init()
{
// clear the outdated summary cache, since new columns are being added. Optimally all menu item are constructed // clear the outdated summary cache, since new columns are being added. Optimally all menu item are constructed
// before any rendering is going on to avoid trashing too man old requests // before any rendering is going on to avoid trashing too man old requests
if (isset(self::$summaries[$this->dataView])) { if (isset(self::$summaries[$this->dataView])) {
@ -89,11 +112,11 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer
if (! isset(self::$dataViews[$this->dataView])) { if (! isset(self::$dataViews[$this->dataView])) {
self::$dataViews[$this->dataView] = array(); self::$dataViews[$this->dataView] = array();
} }
foreach ($this->columns as $column => $title) { foreach ($this->columns as $column => $title) {
if (! array_search($column, self::$dataViews[$this->dataView])) { if (! array_search($column, self::$dataViews[$this->dataView])) {
self::$dataViews[$this->dataView][] = $column; self::$dataViews[$this->dataView][] = $column;
} }
$this->titles[$column] = $title;
} }
} }
@ -116,12 +139,11 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer
} }
/** /**
* Fetch the response from the database or access cache * Fetch the dataview from the database or access cache
* *
* @param $view * @param string $view
* *
* @return null * @return object
* @throws \Icinga\Exception\ConfigurationError
*/ */
protected static function summary($view) protected static function summary($view)
{ {
@ -133,51 +155,29 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer
static::applyRestriction('monitoring/filter/objects', $summary); static::applyRestriction('monitoring/filter/objects', $summary);
self::$summaries[$view] = $summary->fetchRow(); self::$summaries[$view] = $summary->fetchRow();
} }
return isset(self::$summaries[$view]) ? self::$summaries[$view] : null;
return self::$summaries[$view];
} }
/** /**
* Defines the color of the badge * {@inheritdoc}
*
* @return string
*/
public function getState()
{
return $this->state;
}
/**
* The amount of items to display in the badge
*
* @return int
*/ */
public function getCount() public function getCount()
{ {
$sum = self::summary($this->dataView); try {
$count = 0; $summary = self::summary($this->getDataView());
} catch (Exception $_) {
return 0;
}
foreach ($this->columns as $col => $title) { $count = 0;
if (isset($sum->$col)) { foreach ($this->getColumns() as $column => $title) {
$count += $sum->$col; if (isset($summary->$column) && $summary->$column > 0) {
$this->titles[] = sprintf($title, $summary->$column);
$count += $summary->$column;
} }
} }
return $count; return $count;
} }
/**
* The tooltip title
*
* @return string
*/
public function getTitle()
{
$titles = array();
$sum = $this->summary($this->dataView);
foreach ($this->columns as $column => $value) {
if (isset($sum->$column) && $sum->$column > 0) {
$titles[] = sprintf($this->titles[$column], $sum->$column);
}
}
return implode(', ', $titles);
}
} }