diff --git a/library/Icinga/Web/Widget/Chart/InlinePie.php b/library/Icinga/Web/Widget/Chart/InlinePie.php index f9fac171a..e19310d49 100644 --- a/library/Icinga/Web/Widget/Chart/InlinePie.php +++ b/library/Icinga/Web/Widget/Chart/InlinePie.php @@ -46,7 +46,8 @@ use Icinga\Logger\Logger; */ class InlinePie extends AbstractWidget { - const NUMBER_FORMAT_TIME = 'time'; + const NUMBER_FORMAT_NONE = 'none'; + const NUMBER_FORMAT_TIME = 'time'; const NUMBER_FORMAT_BYTES = 'bytes'; const NUMBER_FORMAT_RATIO = 'ratio'; @@ -126,7 +127,7 @@ EOD; * * @var string */ - private $title = ''; + private $title; /** * The style for the HtmlElement @@ -168,7 +169,7 @@ EOD; * * @var array */ - private $format = self::NUMBER_FORMAT_BYTES; + private $format = self::NUMBER_FORMAT_NONE; /** * Set if the tooltip for the empty area should be hidden @@ -194,18 +195,22 @@ EOD; /** * The labels to be displayed in the pie-chart * - * @param null $labels + * @param mixed $label The label of the displayed value, or null for no labels * - * @return $this + * @return $this Fluent interface */ - public function setLabels($labels = null) + public function setLabel($label) { - if ($labels != null) { - $this->url->setParam('labels', implode(',', $labels)); + if (is_array($label)) { + $this->url->setParam('labels', implode(',', array_keys($label))); + } elseif ($label != null) { + $labelArr = array($label, $label, $label, ''); + $this->url->setParam('labels', implode(',', $labelArr)); + $label = $labelArr; } else { $this->url->removeKey('labels'); } - $this->labels = $labels; + $this->labels = $label; return $this; } @@ -318,10 +323,12 @@ EOD; * Create a new InlinePie * * @param array $data The data displayed by the slices + * @param string $title The title of this Pie * @param array $colors An array of RGB-Color values to use */ - public function __construct(array $data, $colors = null) + public function __construct(array $data, $title, $colors = null) { + $this->title = $title; $this->url = Url::fromPath('svg/chart.php'); if (array_key_exists('data', $data)) { $this->data = $data['data']; @@ -346,10 +353,11 @@ EOD; * * @return string A serialized array of labels */ - private function createLabelString () { + private function createLabelString () + { $labels = $this->labels; foreach ($labels as $key => $label) { - $labels[$key] = preg_replace('/|/', '', $label); + $labels[$key] = str_replace('|', '', $label); } return isset($this->labels) && is_array($this->labels) ? implode('|', $this->labels) : ''; } @@ -363,27 +371,27 @@ EOD; public function render() { $template = $this->template; - $template = preg_replace('{{url}}', $this->url, $template); - + $template = str_replace('{url}', $this->url, $template); + // style - $template = preg_replace('{{width}}', htmlspecialchars($this->width), $template); - $template = preg_replace('{{height}}', htmlspecialchars($this->height), $template); - $template = preg_replace('{{title}}', htmlspecialchars($this->title), $template); - $template = preg_replace('{{style}}', $this->style, $template); - $template = preg_replace('{{colors}}', implode(',', $this->colors), $template); - $template = preg_replace('{{borderWidth}}', htmlspecialchars($this->borderWidth), $template); - $template = preg_replace('{{borderColor}}', htmlspecialchars($this->borderColor), $template); - $template = preg_replace('{{hideEmptyLabel}}', $this->hideEmptyLabel ? 'true' : 'false', $template); + $template = str_replace('{width}', $this->width, $template); + $template = str_replace('{height}', $this->height, $template); + $template = str_replace('{title}', htmlspecialchars($this->title), $template); + $template = str_replace('{style}', $this->style, $template); + $template = str_replace('{colors}', implode(',', $this->colors), $template); + $template = str_replace('{borderWidth}', $this->borderWidth, $template); + $template = str_replace('{borderColor}', $this->borderColor, $template); + $template = str_replace('{hideEmptyLabel}', $this->hideEmptyLabel ? 'true' : 'false', $template); // values $formatted = array(); foreach ($this->data as $key => $value) { $formatted[$key] = $this->formatValue($value); } - $template = preg_replace('{{data}}', htmlspecialchars(implode(',', $this->data)), $template); - $template = preg_replace('{{formatted}}', htmlspecialchars(implode('|', $formatted)), $template); - $template = preg_replace('{{labels}}', htmlspecialchars($this->createLabelString()), $template); - $template = preg_replace('{{tooltipFormat}}', $this->tooltipFormat, $template); + $template = str_replace('{data}', htmlspecialchars(implode(',', $this->data)), $template); + $template = str_replace('{formatted}', htmlspecialchars(implode('|', $formatted)), $template); + $template = str_replace('{labels}', htmlspecialchars($this->createLabelString()), $template); + $template = str_replace('{tooltipFormat}', $this->tooltipFormat, $template); return $template; } @@ -396,11 +404,13 @@ EOD; */ private function formatValue($value) { - if ($this->format === self::NUMBER_FORMAT_BYTES) { + if ($this->format === self::NUMBER_FORMAT_NONE) { + return (string)$value; + } elseif ($this->format === self::NUMBER_FORMAT_BYTES) { return Format::bytes($value); - } else if ($this->format === self::NUMBER_FORMAT_TIME) { + } elseif ($this->format === self::NUMBER_FORMAT_TIME) { return Format::duration($value); - } else if ($this->format === self::NUMBER_FORMAT_RATIO) { + } elseif ($this->format === self::NUMBER_FORMAT_RATIO) { return $value; } else { Logger::warning('Unknown format string "' . $this->format . '" for InlinePie, value not formatted.'); diff --git a/modules/monitoring/application/controllers/MultiController.php b/modules/monitoring/application/controllers/MultiController.php index 6b85a9ee5..28661d54b 100644 --- a/modules/monitoring/application/controllers/MultiController.php +++ b/modules/monitoring/application/controllers/MultiController.php @@ -73,14 +73,18 @@ class Monitoring_MultiController extends Controller $uniqueComments = array_keys($this->getUniqueValues($comments->getQuery()->fetchAll(), 'comment_internal_id')); // Populate view - $this->view->objects = $this->view->hosts = $hosts; - $this->view->problems = $this->getProblems($hosts); - $this->view->comments = $uniqueComments; + $this->view->objects = $this->view->hosts = $hosts; + $this->view->problems = $this->getProblems($hosts); + $this->view->comments = $uniqueComments; $this->view->hostnames = $this->getProperties($hosts, 'host_name'); $this->view->downtimes = $this->getDowntimes($hosts); - $this->view->errors = $errors; - $this->view->states = $this->countStates($hosts, 'host', 'host_name'); - $this->view->pie = $this->createPie($this->view->states, $this->view->getHelper('MonitoringState')->getHostStateColors()); + $this->view->errors = $errors; + $this->view->states = $this->countStates($hosts, 'host', 'host_name'); + $this->view->pie = $this->createPie( + $this->view->states, + $this->view->getHelper('MonitoringState')->getHostStateColors(), + t('Host State') + ); // Handle configuration changes $this->handleConfigurationForm(array( @@ -138,9 +142,17 @@ class Monitoring_MultiController extends Controller $this->view->servicenames = $this->getProperties($services, 'service_description'); $this->view->downtimes = $this->getDowntimes($services); $this->view->service_states = $this->countStates($services, 'service'); - $this->view->host_states = $this->countStates($services, 'host', 'host_name'); - $this->view->service_pie = $this->createPie($this->view->service_states, $this->view->getHelper('MonitoringState')->getServiceStateColors()); - $this->view->host_pie = $this->createPie($this->view->host_states, $this->view->getHelper('MonitoringState')->getHostStateColors()); + $this->view->host_states = $this->countStates($services, 'host', 'host_name'); + $this->view->service_pie = $this->createPie( + $this->view->service_states, + $this->view->getHelper('MonitoringState')->getServiceStateColors(), + t('Service State') + ); + $this->view->host_pie = $this->createPie( + $this->view->host_states, + $this->view->getHelper('MonitoringState')->getHostStateColors(), + t('Host State') + ); $this->view->errors = $errors; $this->handleConfigurationForm(array( @@ -181,13 +193,12 @@ class Monitoring_MultiController extends Controller private function getUniqueValues($values, $key) { $unique = array(); - foreach ($values as $value) - { - if (is_array($value)) { - $unique[$value[$key]] = $value[$key]; - } else { - $unique[$value->$key] = $value->$key; - } + foreach ($values as $value) { + if (is_array($value)) { + $unique[$value[$key]] = $value[$key]; + } else { + $unique[$value->$key] = $value->$key; + } } return $unique; } @@ -236,10 +247,11 @@ class Monitoring_MultiController extends Controller return $states; } - private function createPie($states, $colors) + private function createPie($states, $colors, $title) { - $chart = new InlinePie(array_values($states), $colors); - $chart->setLabels(array_keys($states))->setHeight(100)->setWidth(100); + $chart = new InlinePie(array_values($states), $title, $colors); + $chart->setLabel(array_keys($states))->setHeight(100)->setWidth(100); + $chart->setTitle($title); return $chart; } diff --git a/modules/monitoring/application/views/helpers/Perfdata.php b/modules/monitoring/application/views/helpers/Perfdata.php index 72d99c574..2f3c70d1b 100644 --- a/modules/monitoring/application/views/helpers/Perfdata.php +++ b/modules/monitoring/application/views/helpers/Perfdata.php @@ -18,18 +18,14 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) { continue; } - $pieChart = $this->createInlinePie($perfdata, $label); + $pieChart = $this->createInlinePie($perfdata, $label, htmlspecialchars($label)); if ($compact) { - $pieChart->setTitle( - htmlspecialchars($label) /* . ': ' . htmlspecialchars($this->formatPerfdataValue($perfdata) */ - ); if (! $float) { $result .= $pieChart->render(); } else { $result .= '