2014-03-26 14:56:35 +01:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
namespace Icinga\Web\Widget\Chart;
|
|
|
|
|
2014-12-15 13:55:20 +01:00
|
|
|
use Icinga\Chart\PieChart;
|
2014-12-23 15:26:45 +01:00
|
|
|
use Icinga\Module\Monitoring\Plugin\PerfdataSet;
|
2016-01-27 15:56:27 +01:00
|
|
|
use Icinga\Util\StringHelper;
|
2014-03-26 14:56:35 +01:00
|
|
|
use Icinga\Web\Widget\AbstractWidget;
|
|
|
|
use Icinga\Web\Url;
|
2014-06-18 13:53:38 +02:00
|
|
|
use Icinga\Util\Format;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2014-12-15 13:55:20 +01:00
|
|
|
use Icinga\Exception\IcingaException;
|
2015-04-02 16:25:20 +02:00
|
|
|
use stdClass;
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A SVG-PieChart intended to be displayed as a small icon next to labels, to offer a better visualization of the
|
|
|
|
* shown data
|
|
|
|
*
|
|
|
|
* NOTE: When InlinePies are shown in a dynamically loaded view, like the side-bar or in the dashboard, the SVGs will
|
|
|
|
* be replaced with a jQuery-Sparkline to save resources @see loader.js
|
|
|
|
*
|
|
|
|
* @package Icinga\Web\Widget\Chart
|
|
|
|
*/
|
|
|
|
class InlinePie extends AbstractWidget
|
|
|
|
{
|
2014-06-25 14:53:39 +02:00
|
|
|
const NUMBER_FORMAT_NONE = 'none';
|
|
|
|
const NUMBER_FORMAT_TIME = 'time';
|
2014-06-18 13:53:38 +02:00
|
|
|
const NUMBER_FORMAT_BYTES = 'bytes';
|
|
|
|
const NUMBER_FORMAT_RATIO = 'ratio';
|
2014-09-19 14:43:25 +02:00
|
|
|
|
2015-04-02 16:25:20 +02:00
|
|
|
public static $colorsHostStates = array(
|
|
|
|
'#44bb77', // up
|
|
|
|
'#ff99aa', // down
|
|
|
|
'#cc77ff', // unreachable
|
|
|
|
'#77aaff' // pending
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $colorsHostStatesHandledUnhandled = array(
|
|
|
|
'#44bb77', // up
|
|
|
|
'#44bb77',
|
|
|
|
'#ff99aa', // down
|
|
|
|
'#ff5566',
|
|
|
|
'#cc77ff', // unreachable
|
|
|
|
'#aa44ff',
|
|
|
|
'#77aaff', // pending
|
|
|
|
'#77aaff'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $colorsServiceStates = array(
|
|
|
|
'#44bb77', // Ok
|
|
|
|
'#ffaa44', // Warning
|
|
|
|
'#ff99aa', // Critical
|
|
|
|
'#aa44ff', // Unknown
|
|
|
|
'#77aaff' // Pending
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $colorsServiceStatesHandleUnhandled = array(
|
|
|
|
'#44bb77', // Ok
|
|
|
|
'#44bb77',
|
|
|
|
'#ffaa44', // Warning
|
|
|
|
'#ffcc66',
|
|
|
|
'#ff99aa', // Critical
|
|
|
|
'#ff5566',
|
|
|
|
'#cc77ff', // Unknown
|
|
|
|
'#aa44ff',
|
|
|
|
'#77aaff', // Pending
|
|
|
|
'#77aaff'
|
|
|
|
);
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* The template string used for rendering this widget
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $template =<<<'EOD'
|
2017-09-19 14:41:10 +02:00
|
|
|
<span sparkType="pie" class="sparkline {class}" title="{title}" role="img" aria-label="{title}"
|
|
|
|
sparkSliceColors="[{colors}]" values="{data}"></span>
|
2014-12-18 16:44:55 +01:00
|
|
|
{noscript}
|
2014-03-26 14:56:35 +01:00
|
|
|
EOD;
|
|
|
|
|
2014-12-18 16:44:55 +01:00
|
|
|
private $noscript =<<<'EOD'
|
|
|
|
<noscript>
|
2017-09-19 14:41:10 +02:00
|
|
|
<img width={size} height={size} class="inlinepie {class}" title="{title}" role="img" aria-label="{title}"
|
|
|
|
src="{url}" data-icinga-colors="{colors}" data-icinga-values="{data}"/>
|
2014-12-18 16:44:55 +01:00
|
|
|
</noscript>
|
|
|
|
EOD;
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* @var Url
|
|
|
|
*/
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The colors used to display the slices of this pie-chart.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-12-23 16:12:25 +01:00
|
|
|
private $colors = array('#049BAF', '#ffaa44', '#ff5566', '#ddccdd');
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The title of the chart
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-06-25 15:21:12 +02:00
|
|
|
private $title;
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-18 16:44:55 +01:00
|
|
|
* @var
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-12-18 16:44:55 +01:00
|
|
|
private $size;
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The data displayed by the pie-chart
|
|
|
|
*
|
2014-06-18 13:53:38 +02:00
|
|
|
* @var array
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
|
|
|
private $data;
|
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
/**
|
2014-12-23 15:26:45 +01:00
|
|
|
* @var
|
2014-06-18 13:53:38 +02:00
|
|
|
*/
|
2014-12-23 15:26:45 +01:00
|
|
|
private $class = '';
|
2014-06-18 13:53:38 +02:00
|
|
|
|
2014-06-18 17:37:01 +02:00
|
|
|
/**
|
2014-12-18 16:44:55 +01:00
|
|
|
* Set the data to be displayed.
|
2014-06-18 17:37:01 +02:00
|
|
|
*
|
2014-12-18 16:44:55 +01:00
|
|
|
* @param $data array
|
2014-09-19 14:43:25 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2014-06-18 17:37:01 +02:00
|
|
|
*/
|
2014-12-18 16:44:55 +01:00
|
|
|
public function setData(array $data)
|
2014-06-18 17:37:01 +02:00
|
|
|
{
|
2014-12-18 16:44:55 +01:00
|
|
|
$this->data = $data;
|
|
|
|
$this->url->setParam('data', implode(',', $data));
|
2014-09-19 14:43:25 +02:00
|
|
|
return $this;
|
2014-06-18 17:37:01 +02:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
2014-12-18 16:44:55 +01:00
|
|
|
* Set the size of the inline pie
|
2014-03-26 14:56:35 +01:00
|
|
|
*
|
2014-12-18 16:44:55 +01:00
|
|
|
* @param int $size Sets both, the height and width
|
2014-09-19 14:43:25 +02:00
|
|
|
*
|
2014-12-18 16:44:55 +01:00
|
|
|
* @return $this
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-12-18 16:44:55 +01:00
|
|
|
public function setSize($size = null)
|
2014-03-26 14:56:35 +01:00
|
|
|
{
|
2014-12-18 16:44:55 +01:00
|
|
|
$this->size = $size;
|
2014-09-19 14:43:25 +02:00
|
|
|
return $this;
|
2014-03-26 14:56:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 16:44:55 +01:00
|
|
|
/**
|
|
|
|
* Do not display the NoScript fallback html
|
|
|
|
*/
|
|
|
|
public function disableNoScript()
|
|
|
|
{
|
|
|
|
$this->noscript = '';
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
2014-12-23 15:26:45 +01:00
|
|
|
* Set the class to define the
|
2014-03-26 14:56:35 +01:00
|
|
|
*
|
2014-12-23 15:26:45 +01:00
|
|
|
* @param $class
|
2014-04-16 19:43:56 +02:00
|
|
|
*
|
2014-12-23 15:26:45 +01:00
|
|
|
* @return $this
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-12-23 15:26:45 +01:00
|
|
|
public function setSparklineClass($class)
|
2014-03-26 14:56:35 +01:00
|
|
|
{
|
2014-12-23 15:26:45 +01:00
|
|
|
$this->class = $class;
|
2014-04-16 19:43:56 +02:00
|
|
|
return $this;
|
2014-03-26 14:56:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the colors used by the slices of the pie chart.
|
|
|
|
*
|
2014-09-19 14:43:25 +02:00
|
|
|
* @param array $colors
|
|
|
|
*
|
|
|
|
* @return $this
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
|
|
|
public function setColors(array $colors = null)
|
|
|
|
{
|
|
|
|
$this->colors = $colors;
|
|
|
|
if (isset($colors)) {
|
|
|
|
$this->url->setParam('colors', implode(',', $colors));
|
|
|
|
} else {
|
|
|
|
$this->url->setParam('colors', null);
|
|
|
|
}
|
2014-09-19 14:43:25 +02:00
|
|
|
return $this;
|
2014-03-26 14:56:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-18 16:44:55 +01:00
|
|
|
* Set the title of the displayed Data
|
2014-03-26 14:56:35 +01:00
|
|
|
*
|
2014-12-18 16:44:55 +01:00
|
|
|
* @param string $title
|
2014-09-19 14:43:25 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-12-18 16:44:55 +01:00
|
|
|
public function setTitle($title)
|
2014-03-26 14:56:35 +01:00
|
|
|
{
|
2016-01-22 13:33:22 +01:00
|
|
|
$this->title = $this->view()->escape($title);
|
2014-03-26 14:56:35 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new InlinePie
|
|
|
|
*
|
2014-06-18 14:33:03 +02:00
|
|
|
* @param array $data The data displayed by the slices
|
2014-06-25 15:21:12 +02:00
|
|
|
* @param string $title The title of this Pie
|
2014-06-18 14:33:03 +02:00
|
|
|
* @param array $colors An array of RGB-Color values to use
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-06-25 15:21:12 +02:00
|
|
|
public function __construct(array $data, $title, $colors = null)
|
2014-03-26 14:56:35 +01:00
|
|
|
{
|
2014-12-18 16:44:55 +01:00
|
|
|
$this->setTitle($title);
|
2014-03-26 14:56:35 +01:00
|
|
|
$this->url = Url::fromPath('svg/chart.php');
|
|
|
|
if (array_key_exists('data', $data)) {
|
|
|
|
$this->data = $data['data'];
|
|
|
|
if (array_key_exists('colors', $data)) {
|
|
|
|
$this->colors = $data['colors'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->setData($data);
|
|
|
|
}
|
|
|
|
if (isset($colors)) {
|
|
|
|
$this->setColors($colors);
|
|
|
|
} else {
|
|
|
|
$this->setColors($this->colors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Renders this widget via the given view and returns the
|
|
|
|
* HTML as a string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
2014-12-15 13:55:20 +01:00
|
|
|
if ($this->view()->layout()->getLayout() === 'pdf') {
|
|
|
|
$pie = new PieChart();
|
|
|
|
$pie->alignTopLeft();
|
|
|
|
$pie->disableLegend();
|
|
|
|
$pie->drawPie(array(
|
2014-12-23 15:26:45 +01:00
|
|
|
'data' => $this->data, 'colors' => $this->colors
|
2014-12-15 13:55:20 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
try {
|
2014-12-18 16:44:55 +01:00
|
|
|
$png = $pie->toPng($this->size, $this->size);
|
2014-12-15 13:56:19 +01:00
|
|
|
return '<img class="inlinepie" src="data:image/png;base64,' . base64_encode($png) . '" />';
|
2014-12-15 13:55:20 +01:00
|
|
|
} catch (IcingaException $_) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
$template = $this->template;
|
2014-12-18 16:44:55 +01:00
|
|
|
// TODO: Check whether we are XHR and don't send
|
|
|
|
$template = str_replace('{noscript}', $this->noscript, $template);
|
2014-06-25 15:24:53 +02:00
|
|
|
$template = str_replace('{url}', $this->url, $template);
|
2014-12-23 15:26:45 +01:00
|
|
|
$template = str_replace('{class}', $this->class, $template);
|
2014-09-19 14:43:25 +02:00
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
// style
|
2015-02-03 16:45:01 +01:00
|
|
|
$template = str_replace('{size}', isset($this->size) ? $this->size : 16, $template);
|
2014-12-18 16:44:55 +01:00
|
|
|
$template = str_replace('{title}', $this->title, $template);
|
2014-12-23 15:26:45 +01:00
|
|
|
|
2014-06-25 15:24:53 +02:00
|
|
|
$template = str_replace('{colors}', implode(',', $this->colors), $template);
|
2014-06-18 13:53:38 +02:00
|
|
|
|
2014-06-25 22:13:19 +02:00
|
|
|
// Locale-ignorant string cast. Please. Do. NOT. Remove. This. Again.
|
|
|
|
// Problem is that implode respects locales when casting floats. This means
|
|
|
|
// that implode(',', array(1.1, 1.2)) would read '1,1,1,2'.
|
|
|
|
$data = array();
|
|
|
|
foreach ($this->data as $dat) {
|
|
|
|
$data[] = sprintf('%F', $dat);
|
|
|
|
}
|
|
|
|
|
2014-07-23 12:28:04 +02:00
|
|
|
$template = str_replace('{data}', htmlspecialchars(implode(',', $data)), $template);
|
2014-03-26 14:56:35 +01:00
|
|
|
return $template;
|
|
|
|
}
|
2015-04-02 16:25:20 +02:00
|
|
|
|
|
|
|
public static function createFromStateSummary(stdClass $states, $title, array $colors)
|
|
|
|
{
|
|
|
|
$handledUnhandledStates = array();
|
|
|
|
foreach ($states as $key => $value) {
|
2016-01-27 15:56:27 +01:00
|
|
|
if (StringHelper::endsWith($key, '_handled') || StringHelper::endsWith($key, '_unhandled')) {
|
2015-04-02 16:25:20 +02:00
|
|
|
$handledUnhandledStates[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$chart = new self(array_values($handledUnhandledStates), $title, $colors);
|
|
|
|
return $chart
|
|
|
|
->setSize(50)
|
|
|
|
->setTitle('')
|
|
|
|
->setSparklineClass('sparkline-multi');
|
|
|
|
}
|
2014-03-26 14:56:35 +01:00
|
|
|
}
|