From 7a5dceac7bc950ad7e814880a742a87971326c04 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 4 Sep 2015 13:50:53 +0200 Subject: [PATCH] monitoring: Adjust custom navigation renderer for DataView badges refs #5600 --- modules/monitoring/configuration.php | 6 +- .../MonitoringBadgeNavigationItemRenderer.php | 146 +++++++++--------- 2 files changed, 76 insertions(+), 76 deletions(-) diff --git a/modules/monitoring/configuration.php b/modules/monitoring/configuration.php index 83eec6320..7beabde05 100644 --- a/modules/monitoring/configuration.php +++ b/modules/monitoring/configuration.php @@ -90,7 +90,7 @@ $this->provideSearchUrl($this->translate('Servicegroups'), 'monitoring/list/serv */ $section = $this->menuSection($this->translate('Problems'), array( 'renderer' => array( - 'SummaryMenuItemRenderer', + 'SummaryNavigationItemRenderer', 'state' => 'critical' ), 'icon' => 'block', @@ -98,7 +98,7 @@ $section = $this->menuSection($this->translate('Problems'), array( )); $section->add($this->translate('Unhandled Hosts'), array( 'renderer' => array( - 'Icinga\Module\Monitoring\Web\Menu\MonitoringBadgeMenuItemRenderer', + 'MonitoringBadgeNavigationItemRenderer', 'columns' => array( '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( 'renderer' => array( - 'Icinga\Module\Monitoring\Web\Menu\MonitoringBadgeMenuItemRenderer', + 'MonitoringBadgeNavigationItemRenderer', 'columns' => array( 'services_critical_unhandled' => $this->translate('%d unhandled services critical') ), diff --git a/modules/monitoring/library/Monitoring/Web/Navigation/Renderer/MonitoringBadgeNavigationItemRenderer.php b/modules/monitoring/library/Monitoring/Web/Navigation/Renderer/MonitoringBadgeNavigationItemRenderer.php index 90d02fe52..14c44956e 100644 --- a/modules/monitoring/library/Monitoring/Web/Navigation/Renderer/MonitoringBadgeNavigationItemRenderer.php +++ b/modules/monitoring/library/Monitoring/Web/Navigation/Renderer/MonitoringBadgeNavigationItemRenderer.php @@ -1,23 +1,26 @@ dataView = $dataView; + return $this; + } - $this->columns = $configuration->get('columns'); - $this->state = $configuration->get('state'); - $this->dataView = $configuration->get('dataView'); + /** + * Return the dataview referred to by the navigation item + * + * @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 // before any rendering is going on to avoid trashing too man old requests if (isset(self::$summaries[$this->dataView])) { @@ -89,11 +112,11 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer if (! isset(self::$dataViews[$this->dataView])) { self::$dataViews[$this->dataView] = array(); } + foreach ($this->columns as $column => $title) { if (! array_search($column, self::$dataViews[$this->dataView])) { 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 - * @throws \Icinga\Exception\ConfigurationError + * @return object */ protected static function summary($view) { @@ -133,51 +155,29 @@ class MonitoringBadgeMenuItemRenderer extends BadgeMenuItemRenderer static::applyRestriction('monitoring/filter/objects', $summary); self::$summaries[$view] = $summary->fetchRow(); } - return isset(self::$summaries[$view]) ? self::$summaries[$view] : null; + + return self::$summaries[$view]; } /** - * Defines the color of the badge - * - * @return string - */ - public function getState() - { - return $this->state; - } - - /** - * The amount of items to display in the badge - * - * @return int + * {@inheritdoc} */ public function getCount() { - $sum = self::summary($this->dataView); - $count = 0; + try { + $summary = self::summary($this->getDataView()); + } catch (Exception $_) { + return 0; + } - foreach ($this->columns as $col => $title) { - if (isset($sum->$col)) { - $count += $sum->$col; + $count = 0; + foreach ($this->getColumns() as $column => $title) { + if (isset($summary->$column) && $summary->$column > 0) { + $this->titles[] = sprintf($title, $summary->$column); + $count += $summary->$column; } } + 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); - } }