icingaweb2-module-graphite/library/Graphite/GraphiteChart.php
2017-09-07 17:33:46 +02:00

148 lines
3.2 KiB
PHP

<?php
namespace Icinga\Module\Graphite;
use Icinga\Web\Url;
class GraphiteChart
{
protected $web;
protected $metric;
protected $from = '-4hours';
/**
* @var string
*/
protected $until;
protected $showLegend = true;
protected $height = 200;
protected $width = 300;
public function __construct(GraphiteWeb $web, GraphTemplate $template, $metric, $vars)
{
$this->web = $web;
$this->template = $template;
$this->metric = $metric;
$this->vars = $vars;
}
public function getTitle()
{
return $this->template->getTitle($this->vars);
}
public function setStart($start)
{
$this->from = $start;
return $this;
}
public function setWidth($width)
{
$this->width = $width;
return $this;
}
public function setHeight($height)
{
$this->height = $height;
return $this;
}
public function showLegend($show = true)
{
$this->showLegend = (bool) $show;
return $this;
}
public function setMetrics($metrics = array())
{
}
public function setFrom($from)
{
$this->from = $from;
return $this;
}
public function getFrom()
{
return $this->from;
}
/**
* Get {@link until}
*
* @return string
*/
public function getUntil()
{
return $this->until;
}
/**
* Set {@link until}
*
* @param string $until
*
* @return $this
*/
public function setUntil($until)
{
$this->until = $until;
return $this;
}
protected function getParams()
{
$params = [
'height' => $this->height,
'width' => $this->width,
'_salt' => time() . '.000',
'from' => $this->from,
// 'graphOnly' => (string) ! $this->showLegend,
'hideLegend' => (string) ! $this->showLegend,
'hideGrid' => 'true',
'vTitle' => 'Percent',
'lineMode' => 'connected', // staircase, slope
'xFormat' => '%a %H:%M',
'drawNullAsZero' => 'false',
'graphType' => 'line', // pie
'tz' => 'Europe/Berlin',
// 'hideAxes' => 'true',
// 'hideYAxis' => 'true',
// 'format' => 'svg',
// 'pieMode' => 'average',
];
if ($this->until !== null) {
$params['until'] = $this->until;
}
return $params;
}
public function getUrl()
{
$url = Url::fromPath('/render', $this->getParams());
$this->template->extendUrl($url, $this->metric, $this->vars);
$url->getParams()->add('_ext', 'whatever.svg');
return $url;
}
public function fetchImage()
{
$url = $this->getUrl();
return $this->web->getClient()->request(
$url->getPath(),
$url->getParams(),
'POST',
['Accept-language' => 'en', 'Content-type' => 'application/x-www-form-urlencoded']
);
}
}