icingaweb2/modules/monitoring/application/views/helpers/Perfdata.php
Matthias Jentsch c93b13b138 Rework behavior of perfdata visualization
Determine perfdata pie color from host or service state, display zero percent piecharts, only render displayabl PieCharts with min and max values, move perfdata to piechart conversion functions into the Perfdata object.

fixes #6423
fixes #6200
fixes #7170
fixes #7304
2014-12-23 15:54:27 +01:00

66 lines
2.1 KiB
PHP

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Util\Format;
use Icinga\Web\Widget\Chart\InlinePie;
use Icinga\Module\Monitoring\Plugin\Perfdata;
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
{
/**
* Display the given perfdata string to the user
*
* @param $perfdataStr The perfdata string
* @param bool $compact Whether to display the perfdata in compact mode
* @param $color The color indicating the perfdata state
*
* @return string
*/
public function perfdata($perfdataStr, $compact = false, $color = Perfdata::PERFDATA_GREEN)
{
$pieChartData = PerfdataSet::fromString($perfdataStr)->asArray();
$result = '';
$table = array();
foreach ($pieChartData as $perfdata) {
if ($perfdata->isVisualizable()) {
$pieChart = $perfdata->asInlinePie($color);
if ($compact) {
$result .= $pieChart->render();
} else {
$table[] = '<tr><th>' . $pieChart->render()
. htmlspecialchars($perfdata->getLabel())
. '</th><td> '
. htmlspecialchars($this->formatPerfdataValue($perfdata)) .
' </td></tr>';
}
} else {
$table[] = (string)$perfdata;
}
}
if ($compact) {
return $result;
} else {
$pieCharts = empty($table) ? '' : '<table class="perfdata">' . implode("\n", $table) . '</table>';
return $pieCharts;
}
}
protected function formatPerfdataValue(Perfdata $perfdata)
{
if ($perfdata->isBytes()) {
return Format::bytes($perfdata->getValue());
} elseif ($perfdata->isSeconds()) {
return Format::seconds($perfdata->getValue());
} elseif ($perfdata->isPercentage()) {
return $perfdata->getValue() . '%';
}
return $perfdata->getValue();
}
}