From 1e8151bed32398a27b0980e57fb8a54b8f5bc081 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Tue, 2 Jun 2015 17:09:40 +0200 Subject: [PATCH] Limit perfdata label size Add paratentheses when perfdata label size exceeds a certain limit. Display the full label as tooltip. refs #8334 --- library/Icinga/Util/String.php | 20 +++++++++++++++++++ .../application/views/helpers/Perfdata.php | 10 ++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Util/String.php b/library/Icinga/Util/String.php index 0d42d0e76..d67035b3f 100644 --- a/library/Icinga/Util/String.php +++ b/library/Icinga/Util/String.php @@ -54,6 +54,26 @@ class String return $string; } + /** + * Add ellipsis in the center of a string when a string is longer than max length + * + * @param string $string + * @param int $maxLength + * @param string $ellipsis + * + * @return string + */ + public static function ellipsisCenter($string, $maxLength, $ellipsis = '...') + { + $start = ceil($maxLength / 2.0); + $end = floor($maxLength / 2.0); + if (strlen($string) > $maxLength) { + return substr($string, 0, $start - strlen($ellipsis)) . $ellipsis . substr($string, - $end); + } + + return $string; + } + /** * Find and return all similar strings in $possibilites matching $string with the given minimum $similarity * diff --git a/modules/monitoring/application/views/helpers/Perfdata.php b/modules/monitoring/application/views/helpers/Perfdata.php index 5e17adea5..9c6a11d05 100644 --- a/modules/monitoring/application/views/helpers/Perfdata.php +++ b/modules/monitoring/application/views/helpers/Perfdata.php @@ -3,6 +3,7 @@ use Icinga\Module\Monitoring\Plugin\Perfdata; use Icinga\Module\Monitoring\Plugin\PerfdataSet; +use Icinga\Util\String; class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract { @@ -47,7 +48,7 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract $columns[$column] = $labels[$column]; } } - // restore original column array sorting sorting + // restore original column array sorting $headers = array(); foreach ($keys as $i => $column) { if (isset($columns[$column])) { @@ -70,7 +71,12 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract if (! isset($columns[$column])) { continue; } - $data []= empty($value) ? '-' : (string) $value; + $text = $this->view->escape(empty($value) ? '-' : $value); + $data []= sprintf( + '%s', + $text, + String::ellipsisCenter($text, 24) + ); } } $table []= '' . implode('', $data) . '';