Revert "Refractor InlinePie and Perfdata helper"

This reverts commit f003c38abd969ac0a728749563727454cfc744f5.
This commit is contained in:
Matthias Jentsch 2014-06-25 14:29:17 +02:00
parent c18b6f26f0
commit 35a5431512
6 changed files with 86 additions and 105 deletions

View File

@ -46,7 +46,6 @@ use Icinga\Logger\Logger;
*/ */
class InlinePie extends AbstractWidget class InlinePie extends AbstractWidget
{ {
const NUMBER_FORMAT_NONE = 'none';
const NUMBER_FORMAT_TIME = 'time'; const NUMBER_FORMAT_TIME = 'time';
const NUMBER_FORMAT_BYTES = 'bytes'; const NUMBER_FORMAT_BYTES = 'bytes';
const NUMBER_FORMAT_RATIO = 'ratio'; const NUMBER_FORMAT_RATIO = 'ratio';
@ -127,7 +126,7 @@ EOD;
* *
* @var string * @var string
*/ */
private $title; private $title = '';
/** /**
* The style for the HtmlElement * The style for the HtmlElement
@ -169,7 +168,7 @@ EOD;
* *
* @var array * @var array
*/ */
private $format = self::NUMBER_FORMAT_NONE; private $format = self::NUMBER_FORMAT_BYTES;
/** /**
* Set if the tooltip for the empty area should be hidden * Set if the tooltip for the empty area should be hidden
@ -195,22 +194,18 @@ EOD;
/** /**
* The labels to be displayed in the pie-chart * The labels to be displayed in the pie-chart
* *
* @param mixed $label The label of the displayed value, or null for no labels * @param null $labels
* *
* @return $this Fluent interface * @return $this
*/ */
public function setLabel($label) public function setLabels($labels = null)
{ {
if (is_array($label)) { if ($labels != null) {
$this->url->setParam('labels', implode(',', array_keys($label))); $this->url->setParam('labels', implode(',', $labels));
} elseif ($label != null) {
$labelArr = array($label, $label, $label, '');
$this->url->setParam('labels', implode(',', $labelArr));
$label = $labelArr;
} else { } else {
$this->url->removeKey('labels'); $this->url->removeKey('labels');
} }
$this->labels = $label; $this->labels = $labels;
return $this; return $this;
} }
@ -323,12 +318,10 @@ EOD;
* Create a new InlinePie * Create a new InlinePie
* *
* @param array $data The data displayed by the slices * @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 * @param array $colors An array of RGB-Color values to use
*/ */
public function __construct(array $data, $title, $colors = null) public function __construct(array $data, $colors = null)
{ {
$this->title = $title;
$this->url = Url::fromPath('svg/chart.php'); $this->url = Url::fromPath('svg/chart.php');
if (array_key_exists('data', $data)) { if (array_key_exists('data', $data)) {
$this->data = $data['data']; $this->data = $data['data'];
@ -353,11 +346,10 @@ EOD;
* *
* @return string A serialized array of labels * @return string A serialized array of labels
*/ */
private function createLabelString () private function createLabelString () {
{
$labels = $this->labels; $labels = $this->labels;
foreach ($labels as $key => $label) { foreach ($labels as $key => $label) {
$labels[$key] = str_replace('|', '', $label); $labels[$key] = preg_replace('/|/', '', $label);
} }
return isset($this->labels) && is_array($this->labels) ? implode('|', $this->labels) : ''; return isset($this->labels) && is_array($this->labels) ? implode('|', $this->labels) : '';
} }
@ -371,27 +363,27 @@ EOD;
public function render() public function render()
{ {
$template = $this->template; $template = $this->template;
$template = str_replace('{url}', $this->url, $template); $template = preg_replace('{{url}}', $this->url, $template);
// style // style
$template = str_replace('{width}', $this->width, $template); $template = preg_replace('{{width}}', htmlspecialchars($this->width), $template);
$template = str_replace('{height}', $this->height, $template); $template = preg_replace('{{height}}', htmlspecialchars($this->height), $template);
$template = str_replace('{title}', htmlspecialchars($this->title), $template); $template = preg_replace('{{title}}', htmlspecialchars($this->title), $template);
$template = str_replace('{style}', $this->style, $template); $template = preg_replace('{{style}}', $this->style, $template);
$template = str_replace('{colors}', implode(',', $this->colors), $template); $template = preg_replace('{{colors}}', implode(',', $this->colors), $template);
$template = str_replace('{borderWidth}', $this->borderWidth, $template); $template = preg_replace('{{borderWidth}}', htmlspecialchars($this->borderWidth), $template);
$template = str_replace('{borderColor}', $this->borderColor, $template); $template = preg_replace('{{borderColor}}', htmlspecialchars($this->borderColor), $template);
$template = str_replace('{hideEmptyLabel}', $this->hideEmptyLabel ? 'true' : 'false', $template); $template = preg_replace('{{hideEmptyLabel}}', $this->hideEmptyLabel ? 'true' : 'false', $template);
// values // values
$formatted = array(); $formatted = array();
foreach ($this->data as $key => $value) { foreach ($this->data as $key => $value) {
$formatted[$key] = $this->formatValue($value); $formatted[$key] = $this->formatValue($value);
} }
$template = str_replace('{data}', htmlspecialchars(implode(',', $this->data)), $template); $template = preg_replace('{{data}}', htmlspecialchars(implode(',', $this->data)), $template);
$template = str_replace('{formatted}', htmlspecialchars(implode('|', $formatted)), $template); $template = preg_replace('{{formatted}}', htmlspecialchars(implode('|', $formatted)), $template);
$template = str_replace('{labels}', htmlspecialchars($this->createLabelString()), $template); $template = preg_replace('{{labels}}', htmlspecialchars($this->createLabelString()), $template);
$template = str_replace('{tooltipFormat}', $this->tooltipFormat, $template); $template = preg_replace('{{tooltipFormat}}', $this->tooltipFormat, $template);
return $template; return $template;
} }
@ -404,9 +396,7 @@ EOD;
*/ */
private function formatValue($value) private function formatValue($value)
{ {
if ($this->format === self::NUMBER_FORMAT_NONE) { if ($this->format === self::NUMBER_FORMAT_BYTES) {
return (string)$value;
} elseif ($this->format === self::NUMBER_FORMAT_BYTES) {
return Format::bytes($value); return Format::bytes($value);
} else if ($this->format === self::NUMBER_FORMAT_TIME) { } else if ($this->format === self::NUMBER_FORMAT_TIME) {
return Format::duration($value); return Format::duration($value);

View File

@ -80,11 +80,7 @@ class Monitoring_MultiController extends Controller
$this->view->downtimes = $this->getDowntimes($hosts); $this->view->downtimes = $this->getDowntimes($hosts);
$this->view->errors = $errors; $this->view->errors = $errors;
$this->view->states = $this->countStates($hosts, 'host', 'host_name'); $this->view->states = $this->countStates($hosts, 'host', 'host_name');
$this->view->pie = $this->createPie( $this->view->pie = $this->createPie($this->view->states, $this->view->getHelper('MonitoringState')->getHostStateColors());
$this->view->states,
$this->view->getHelper('MonitoringState')->getHostStateColors(),
t('Host State')
);
// Handle configuration changes // Handle configuration changes
$this->handleConfigurationForm(array( $this->handleConfigurationForm(array(
@ -143,16 +139,8 @@ class Monitoring_MultiController extends Controller
$this->view->downtimes = $this->getDowntimes($services); $this->view->downtimes = $this->getDowntimes($services);
$this->view->service_states = $this->countStates($services, 'service'); $this->view->service_states = $this->countStates($services, 'service');
$this->view->host_states = $this->countStates($services, 'host', 'host_name'); $this->view->host_states = $this->countStates($services, 'host', 'host_name');
$this->view->service_pie = $this->createPie( $this->view->service_pie = $this->createPie($this->view->service_states, $this->view->getHelper('MonitoringState')->getServiceStateColors());
$this->view->service_states, $this->view->host_pie = $this->createPie($this->view->host_states, $this->view->getHelper('MonitoringState')->getHostStateColors());
$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->view->errors = $errors;
$this->handleConfigurationForm(array( $this->handleConfigurationForm(array(
@ -193,7 +181,8 @@ class Monitoring_MultiController extends Controller
private function getUniqueValues($values, $key) private function getUniqueValues($values, $key)
{ {
$unique = array(); $unique = array();
foreach ($values as $value) { foreach ($values as $value)
{
if (is_array($value)) { if (is_array($value)) {
$unique[$value[$key]] = $value[$key]; $unique[$value[$key]] = $value[$key];
} else { } else {
@ -247,11 +236,10 @@ class Monitoring_MultiController extends Controller
return $states; return $states;
} }
private function createPie($states, $colors, $title) private function createPie($states, $colors)
{ {
$chart = new InlinePie(array_values($states), $title, $colors); $chart = new InlinePie(array_values($states), $colors);
$chart->setLabel(array_keys($states))->setHeight(100)->setWidth(100); $chart->setLabels(array_keys($states))->setHeight(100)->setWidth(100);
$chart->setTitle($title);
return $chart; return $chart;
} }

View File

@ -18,14 +18,18 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) { if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) {
continue; continue;
} }
$pieChart = $this->createInlinePie($perfdata, $label, htmlspecialchars($label)); $pieChart = $this->createInlinePie($perfdata, $label);
if ($compact) { if ($compact) {
$pieChart->setTitle(
htmlspecialchars($label) /* . ': ' . htmlspecialchars($this->formatPerfdataValue($perfdata) */
);
if (! $float) { if (! $float) {
$result .= $pieChart->render(); $result .= $pieChart->render();
} else { } else {
$result .= '<div style="float: right;">' . $pieChart->render() . '</div>'; $result .= '<div style="float: right;">' . $pieChart->render() . '</div>';
} }
} else { } else {
$pieChart->setTitle(htmlspecialchars($label));
if (! $perfdata->isPercentage()) { if (! $perfdata->isPercentage()) {
$pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)'); $pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)');
} }
@ -57,9 +61,9 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
$gray = $unusedValue; $gray = $unusedValue;
$green = $orange = $red = 0; $green = $orange = $red = 0;
// TODO(#6122): Add proper treshold parsing. // TODO(#6122): Add proper treshold parsing.
if ($perfdata->getCriticalThreshold() && $perfdata->getValue() > $perfdata->getCriticalThreshold()) { if ($perfdata->getCriticalTreshold() && $perfdata->getValue() > $perfdata->getCriticalTreshold()) {
$red = $usedValue; $red = $usedValue;
} elseif ($perfdata->getWarningThreshold() && $perfdata->getValue() > $perfdata->getWarningThreshold()) { } elseif ($perfdata->getWarningTreshold() && $perfdata->getValue() > $perfdata->getWarningTreshold()) {
$orange = $usedValue; $orange = $usedValue;
} else { } else {
$green = $usedValue; $green = $usedValue;
@ -81,10 +85,10 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
return $perfdata->getValue(); return $perfdata->getValue();
} }
protected function createInlinePie(Perfdata $perfdata, $title, $label = '') protected function createInlinePie(Perfdata $perfdata, $label = '')
{ {
$pieChart = new InlinePie($this->calculatePieChartData($perfdata), $title); $pieChart = new InlinePie($this->calculatePieChartData($perfdata));
$pieChart->setLabel($label); $pieChart->setLabels(array($label, $label, $label, ''));
$pieChart->setHideEmptyLabel(); $pieChart->setHideEmptyLabel();
//$pieChart->setHeight(32)->setWidth(32); //$pieChart->setHeight(32)->setWidth(32);

View File

@ -19,7 +19,6 @@ class Perfdata
* Unit of measurement (UOM) * Unit of measurement (UOM)
* *
* @var string * @var string
* @var string
*/ */
protected $unit; protected $unit;
@ -45,18 +44,18 @@ class Perfdata
protected $maxValue; protected $maxValue;
/** /**
* The WARNING threshold * The WARNING treshold
* *
* @var string * @var string
*/ */
protected $warningThreshold; protected $warningTreshold;
/** /**
* The CRITICAL threshold * The CRITICAL treshold
* *
* @var string * @var string
*/ */
protected $criticalThreshold; protected $criticalTreshold;
/** /**
* Create a new Perfdata object based on the given performance data value * Create a new Perfdata object based on the given performance data value
@ -181,9 +180,9 @@ class Perfdata
* *
* @return null|string * @return null|string
*/ */
public function getWarningThreshold() public function getWarningTreshold()
{ {
return $this->warningThreshold; return $this->warningTreshold;
} }
/** /**
@ -191,9 +190,9 @@ class Perfdata
* *
* @return null|string * @return null|string
*/ */
public function getCriticalThreshold() public function getCriticalTreshold()
{ {
return $this->criticalThreshold; return $this->criticalTreshold;
} }
/** /**
@ -241,10 +240,10 @@ class Perfdata
$this->minValue = self::convert($parts[3], $this->unit); $this->minValue = self::convert($parts[3], $this->unit);
case 3: case 3:
// TODO(#6123): Tresholds have the same UOM and need to be converted as well! // TODO(#6123): Tresholds have the same UOM and need to be converted as well!
$this->criticalThreshold = trim($parts[2]) ? trim($parts[2]) : null; $this->criticalTreshold = trim($parts[2]) ? trim($parts[2]) : null;
case 2: case 2:
// TODO(#6123): Tresholds have the same UOM and need to be converted as well! // TODO(#6123): Tresholds have the same UOM and need to be converted as well!
$this->warningThreshold = trim($parts[1]) ? trim($parts[1]) : null; $this->warningTreshold = trim($parts[1]) ? trim($parts[1]) : null;
} }
} }

View File

@ -61,27 +61,27 @@ class PerfdataTest extends BaseTestCase
{ {
$this->assertEquals( $this->assertEquals(
'10', '10',
Perfdata::fromString('1;10')->getWarningThreshold(), Perfdata::fromString('1;10')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values' 'Perfdata::getWarningTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'10:', '10:',
Perfdata::fromString('1;10:')->getWarningThreshold(), Perfdata::fromString('1;10:')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values' 'Perfdata::getWarningTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'~:10', '~:10',
Perfdata::fromString('1;~:10')->getWarningThreshold(), Perfdata::fromString('1;~:10')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values' 'Perfdata::getWarningTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'10:20', '10:20',
Perfdata::fromString('1;10:20')->getWarningThreshold(), Perfdata::fromString('1;10:20')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values' 'Perfdata::getWarningTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'@10:20', '@10:20',
Perfdata::fromString('1;@10:20')->getWarningThreshold(), Perfdata::fromString('1;@10:20')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values' 'Perfdata::getWarningTreshold does not return correct values'
); );
} }
@ -90,27 +90,27 @@ class PerfdataTest extends BaseTestCase
{ {
$this->assertEquals( $this->assertEquals(
'10', '10',
Perfdata::fromString('1;;10')->getCriticalThreshold(), Perfdata::fromString('1;;10')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values' 'Perfdata::getCriticalTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'10:', '10:',
Perfdata::fromString('1;;10:')->getCriticalThreshold(), Perfdata::fromString('1;;10:')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values' 'Perfdata::getCriticalTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'~:10', '~:10',
Perfdata::fromString('1;;~:10')->getCriticalThreshold(), Perfdata::fromString('1;;~:10')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values' 'Perfdata::getCriticalTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'10:20', '10:20',
Perfdata::fromString('1;;10:20')->getCriticalThreshold(), Perfdata::fromString('1;;10:20')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values' 'Perfdata::getCriticalTreshold does not return correct values'
); );
$this->assertEquals( $this->assertEquals(
'@10:20', '@10:20',
Perfdata::fromString('1;;@10:20')->getCriticalThreshold(), Perfdata::fromString('1;;@10:20')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values' 'Perfdata::getCriticalTreshold does not return correct values'
); );
} }
@ -147,7 +147,7 @@ class PerfdataTest extends BaseTestCase
{ {
$perfdata = Perfdata::fromString('1;;3;5'); $perfdata = Perfdata::fromString('1;;3;5');
$this->assertNull( $this->assertNull(
$perfdata->getWarningThreshold(), $perfdata->getWarningTreshold(),
'Perfdata objects do not return null for missing warning tresholds' 'Perfdata objects do not return null for missing warning tresholds'
); );
$this->assertNull( $this->assertNull(