Revert "Refractor InlinePie and Perfdata helper"

This reverts commit f003c38abd.
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,8 +46,7 @@ use Icinga\Logger\Logger;
*/
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_RATIO = 'ratio';
@ -127,7 +126,7 @@ EOD;
*
* @var string
*/
private $title;
private $title = '';
/**
* The style for the HtmlElement
@ -169,7 +168,7 @@ EOD;
*
* @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
@ -195,22 +194,18 @@ EOD;
/**
* 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)) {
$this->url->setParam('labels', implode(',', array_keys($label)));
} elseif ($label != null) {
$labelArr = array($label, $label, $label, '');
$this->url->setParam('labels', implode(',', $labelArr));
$label = $labelArr;
if ($labels != null) {
$this->url->setParam('labels', implode(',', $labels));
} else {
$this->url->removeKey('labels');
}
$this->labels = $label;
$this->labels = $labels;
return $this;
}
@ -323,12 +318,10 @@ 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, $title, $colors = null)
public function __construct(array $data, $colors = null)
{
$this->title = $title;
$this->url = Url::fromPath('svg/chart.php');
if (array_key_exists('data', $data)) {
$this->data = $data['data'];
@ -353,11 +346,10 @@ EOD;
*
* @return string A serialized array of labels
*/
private function createLabelString ()
{
private function createLabelString () {
$labels = $this->labels;
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) : '';
}
@ -371,27 +363,27 @@ EOD;
public function render()
{
$template = $this->template;
$template = str_replace('{url}', $this->url, $template);
$template = preg_replace('{{url}}', $this->url, $template);
// style
$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);
$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);
// values
$formatted = array();
foreach ($this->data as $key => $value) {
$formatted[$key] = $this->formatValue($value);
}
$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);
$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);
return $template;
}
@ -404,13 +396,11 @@ EOD;
*/
private function formatValue($value)
{
if ($this->format === self::NUMBER_FORMAT_NONE) {
return (string)$value;
} elseif ($this->format === self::NUMBER_FORMAT_BYTES) {
if ($this->format === self::NUMBER_FORMAT_BYTES) {
return Format::bytes($value);
} elseif ($this->format === self::NUMBER_FORMAT_TIME) {
} else if ($this->format === self::NUMBER_FORMAT_TIME) {
return Format::duration($value);
} elseif ($this->format === self::NUMBER_FORMAT_RATIO) {
} else if ($this->format === self::NUMBER_FORMAT_RATIO) {
return $value;
} else {
Logger::warning('Unknown format string "' . $this->format . '" for InlinePie, value not formatted.');

View File

@ -73,18 +73,14 @@ 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(),
t('Host State')
);
$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());
// Handle configuration changes
$this->handleConfigurationForm(array(
@ -142,17 +138,9 @@ 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(),
t('Service State')
);
$this->view->host_pie = $this->createPie(
$this->view->host_states,
$this->view->getHelper('MonitoringState')->getHostStateColors(),
t('Host State')
);
$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->errors = $errors;
$this->handleConfigurationForm(array(
@ -193,12 +181,13 @@ 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;
}
@ -247,11 +236,10 @@ class Monitoring_MultiController extends Controller
return $states;
}
private function createPie($states, $colors, $title)
private function createPie($states, $colors)
{
$chart = new InlinePie(array_values($states), $title, $colors);
$chart->setLabel(array_keys($states))->setHeight(100)->setWidth(100);
$chart->setTitle($title);
$chart = new InlinePie(array_values($states), $colors);
$chart->setLabels(array_keys($states))->setHeight(100)->setWidth(100);
return $chart;
}

View File

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

View File

@ -3,8 +3,8 @@ $this->is_service = true;
$this->hostquery = implode($this->hostnames, ',');
$this->servicequery = implode($this->servicenames, ',');
$this->target = array(
'host' => $this->hostquery,
'service' => $this->servicequery
'host' => $this->hostquery,
'service' => $this->servicequery
);
?>
@ -24,11 +24,11 @@ $this->target = array(
<td align="center"><?= $this->service_pie->render() ?></td>
<td><?php
foreach ($service_states as $state => $count) {
if ($count > 0) {
echo ucfirst($state) . ': ' . $count . '<br />';
}
}
foreach ($service_states as $state => $count) {
if ($count > 0) {
echo ucfirst($state) . ': ' . $count . '<br />';
}
}
?></td>
<td align="center"><?= $this->host_pie->render() ?></td>

View File

@ -19,7 +19,6 @@ class Perfdata
* Unit of measurement (UOM)
*
* @var string
* @var string
*/
protected $unit;
@ -45,18 +44,18 @@ class Perfdata
protected $maxValue;
/**
* The WARNING threshold
* The WARNING treshold
*
* @var string
*/
protected $warningThreshold;
protected $warningTreshold;
/**
* The CRITICAL threshold
* The CRITICAL treshold
*
* @var string
*/
protected $criticalThreshold;
protected $criticalTreshold;
/**
* Create a new Perfdata object based on the given performance data value
@ -181,9 +180,9 @@ class Perfdata
*
* @return null|string
*/
public function getWarningThreshold()
public function getWarningTreshold()
{
return $this->warningThreshold;
return $this->warningTreshold;
}
/**
@ -191,9 +190,9 @@ class Perfdata
*
* @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);
case 3:
// 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:
// 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(
'10',
Perfdata::fromString('1;10')->getWarningThreshold(),
Perfdata::fromString('1;10')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values'
);
$this->assertEquals(
'10:',
Perfdata::fromString('1;10:')->getWarningThreshold(),
Perfdata::fromString('1;10:')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values'
);
$this->assertEquals(
'~:10',
Perfdata::fromString('1;~:10')->getWarningThreshold(),
Perfdata::fromString('1;~:10')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values'
);
$this->assertEquals(
'10:20',
Perfdata::fromString('1;10:20')->getWarningThreshold(),
Perfdata::fromString('1;10:20')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values'
);
$this->assertEquals(
'@10:20',
Perfdata::fromString('1;@10:20')->getWarningThreshold(),
Perfdata::fromString('1;@10:20')->getWarningTreshold(),
'Perfdata::getWarningTreshold does not return correct values'
);
}
@ -90,27 +90,27 @@ class PerfdataTest extends BaseTestCase
{
$this->assertEquals(
'10',
Perfdata::fromString('1;;10')->getCriticalThreshold(),
Perfdata::fromString('1;;10')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values'
);
$this->assertEquals(
'10:',
Perfdata::fromString('1;;10:')->getCriticalThreshold(),
Perfdata::fromString('1;;10:')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values'
);
$this->assertEquals(
'~:10',
Perfdata::fromString('1;;~:10')->getCriticalThreshold(),
Perfdata::fromString('1;;~:10')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values'
);
$this->assertEquals(
'10:20',
Perfdata::fromString('1;;10:20')->getCriticalThreshold(),
Perfdata::fromString('1;;10:20')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values'
);
$this->assertEquals(
'@10:20',
Perfdata::fromString('1;;@10:20')->getCriticalThreshold(),
Perfdata::fromString('1;;@10:20')->getCriticalTreshold(),
'Perfdata::getCriticalTreshold does not return correct values'
);
}
@ -147,7 +147,7 @@ class PerfdataTest extends BaseTestCase
{
$perfdata = Perfdata::fromString('1;;3;5');
$this->assertNull(
$perfdata->getWarningThreshold(),
$perfdata->getWarningTreshold(),
'Perfdata objects do not return null for missing warning tresholds'
);
$this->assertNull(