Use the time range picker in the detail view

refs #27
This commit is contained in:
Alexander A. Klimov 2017-09-07 17:33:46 +02:00
parent bba75938de
commit f02ab19872
5 changed files with 114 additions and 31 deletions

View file

@ -4,6 +4,7 @@ namespace Icinga\Module\Graphite\Controllers;
use DirectoryIterator;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Graphite\Forms\TimeRangePicker\TimeRangePickerTrait;
use Icinga\Module\Graphite\GraphiteChart;
use Icinga\Module\Graphite\GraphiteUtil;
use Icinga\Module\Graphite\GraphiteWeb;
@ -11,6 +12,7 @@ use Icinga\Module\Graphite\GraphiteWebClient;
use Icinga\Module\Graphite\GraphTemplate;
use Icinga\Module\Graphite\TemplateStore;
use Icinga\Web\Controller;
use Icinga\Web\UrlParams;
use Icinga\Web\Widget;
class ShowController extends Controller
@ -168,15 +170,14 @@ class ShowController extends Controller
foreach ($set->loadTemplates() as $key => $template) {
if (strpos($template->getFilterString(), '$service') !== false) continue;
$imgParams = array(
'template' => $key,
'start' => $view->start,
'width' => $view->width,
'height' => $view->height
);
$imgParams = (new UrlParams())
->set('template', $key)
->set('start', $view->start)
->set('width', $view->width)
->set('height', $view->height);
if ($this->view->disabledDatasources) {
$imgParams['disabled'] = $this->view->disabledDatasources;
$imgParams->set('disabled', $this->view->disabledDatasources);
foreach ($this->view->disabledDatasources as $dis) {
if ($template->hasDatasource($dis)) {
$template->getDatasource($dis)->disable();
@ -190,7 +191,7 @@ class ShowController extends Controller
->select()
->from($template->getFilterString())
->where('hostname', $hostname)
->getWrappedImageLinks($template, $imgParams);
->getWrappedImageLinks($template, TimeRangePickerTrait::copyAllRangeParameters($imgParams));
}
}
@ -228,15 +229,14 @@ class ShowController extends Controller
foreach ($set->loadTemplates() as $key => $template) {
if (strpos($template->getFilterString(), '$service') === false) continue;
$imgParams = array(
'template' => $key,
'start' => $view->start,
'width' => $view->width,
'height' => $view->height
);
$imgParams = (new UrlParams())
->set('template', $key)
->set('start', $view->start)
->set('width', $view->width)
->set('height', $view->height);;
if ($this->view->disabledDatasources) {
$imgParams['disabled'] = $this->view->disabledDatasources;
$imgParams->set('disabled', $this->view->disabledDatasources);
foreach ($this->view->disabledDatasources as $dis) {
if ($template->hasDatasource($dis)) {
$template->getDatasource($dis)->disable();
@ -252,7 +252,7 @@ class ShowController extends Controller
->from($template->getFilterString())
->where('hostname', $hostname)
->where('service', $service)
->getWrappedImageLinks($template, $imgParams);
->getWrappedImageLinks($template, TimeRangePickerTrait::copyAllRangeParameters($imgParams));
}
}
@ -329,12 +329,29 @@ class ShowController extends Controller
$this->view->disabledDatasources = $this->params->getValues('disabled');
}
/**
* Get time range parameters for Graphite from the URL
*
* @return string[]
*/
protected function getRangeFromTimeRangePicker()
{
$params = $this->getRequest()->getUrl()->getParams();
$relative = $params->get(TimeRangePickerTrait::getRelativeRangeParameter());
if ($relative !== null) {
return ["-{$relative}s", null];
}
$absolute = TimeRangePickerTrait::getAbsoluteRangeParameters();
return [$params->get($absolute['start'], '-1hours'), $params->get($absolute['end'])];
}
protected function handleGraphParams()
{
if ($this->handledGraphParams === false) {
$this->handledGraphParams = true;
$view = $this->view;
$view->start = $this->params->shift('start', '-1hours');
list($view->start, $view->end) = $this->getRangeFromTimeRangePicker();
$view->width = $this->params->shift('width', '300');
$view->height = $this->params->shift('height', '150');
}
@ -347,6 +364,7 @@ class ShowController extends Controller
$this->handleGraphParams();
$view = $this->view;
$chart->setStart($view->start)
->setUntil($view->end)
->setWidth($view->width)
->setHeight($view->height)
// TODO: handle before

View file

@ -2,6 +2,7 @@
namespace Icinga\Module\Graphite\Forms\TimeRangePicker;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
trait TimeRangePickerTrait
@ -46,6 +47,33 @@ trait TimeRangePickerTrait
return array_values(array_merge(static::getAllRangeParameters(), [static::getRangeCustomizationParameter()]));
}
/**
* Copy {@link getAllRangeParameters()} from one {@link UrlParams} instance to another
*
* @param UrlParams|null $copy Defaults to a new instance
* @param UrlParams|null $origin Defaults to the current request's params
*
* @return UrlParams The copy
*/
public static function copyAllRangeParameters(UrlParams $copy = null, UrlParams $origin = null)
{
if ($origin === null) {
$origin = Url::fromRequest()->getParams();
}
if ($copy === null) {
$copy = new UrlParams();
}
foreach (TimeRangePickerTrait::getAllRangeParameters() as $param) {
$value = $origin->get($param);
if ($value !== null) {
$copy->set($param, $value);
}
}
return $copy;
}
/**
* Extract the relative time range (if any) from the given URL parameters
*

View file

@ -3,6 +3,7 @@
namespace Icinga\Module\Graphite;
use Icinga\Application\Icinga;
use Icinga\Module\Graphite\Forms\TimeRangePicker\TimeRangePickerTrait;
use Icinga\Web\Url;
use Icinga\Web\View;
@ -17,10 +18,7 @@ class EmbedGraphs
*/
public static function host($host)
{
return static::url(static::getView()->href('graphite/show/host', [
'host' => $host,
'view' => 'compact'
]));
return static::url(static::getView()->href('graphite/show/host', ['host' => $host]));
}
/**
@ -35,8 +33,7 @@ class EmbedGraphs
{
return static::url(static::getView()->href('graphite/show/service', [
'host' => $host,
'service' => $service,
'view' => 'compact'
'service' => $service
]));
}
@ -49,10 +46,12 @@ class EmbedGraphs
*/
protected static function url(Url $url)
{
TimeRangePickerTrait::copyAllRangeParameters($url->getParams());
// TODO(ak): EL says "<div class="container" data-icinga-url="..." /> is enough",
// but this seems not to work for me
return '<div class="container" data-base-target="_main" data-last-update="-1" data-icinga-refresh="15"'
. ' data-icinga-url="' . $url . '"></div>';
. ' data-icinga-url="' . $url->setParam('view', 'compact') . '"></div>';
}
/**

View file

@ -12,6 +12,11 @@ class GraphiteChart
protected $from = '-4hours';
/**
* @var string
*/
protected $until;
protected $showLegend = true;
protected $height = 200;
@ -70,9 +75,32 @@ class GraphiteChart
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()
{
return array(
$params = [
'height' => $this->height,
'width' => $this->width,
'_salt' => time() . '.000',
@ -90,7 +118,13 @@ class GraphiteChart
// 'hideYAxis' => 'true',
// 'format' => 'svg',
// 'pieMode' => 'average',
);
];
if ($this->until !== null) {
$params['until'] = $this->until;
}
return $params;
}
public function getUrl()

View file

@ -3,6 +3,7 @@
namespace Icinga\Module\Graphite\ProvidedHook\Monitoring;
use Icinga\Module\Graphite\EmbedGraphs;
use Icinga\Module\Graphite\Web\Controller\TimeRangePickerTrait;
use Icinga\Module\Monitoring\Hook\DetailviewExtensionHook;
use Icinga\Module\Monitoring\Object\Host;
use Icinga\Module\Monitoring\Object\MonitoredObject;
@ -10,25 +11,28 @@ use Icinga\Module\Monitoring\Object\Service;
class DetailviewExtension extends DetailviewExtensionHook
{
use TimeRangePickerTrait;
public function getHtmlForObject(MonitoredObject $object)
{
switch ($object->getType()) {
case 'host':
/** @var Host $object */
return $this->getHeader() . EmbedGraphs::host($object->getName());
return $this->getGeneric() . EmbedGraphs::host($object->getName());
case 'service':
/** @var Service $object */
return $this->getHeader() . EmbedGraphs::service($object->getHost()->getName(), $object->getName());
return $this->getGeneric() . EmbedGraphs::service($object->getHost()->getName(), $object->getName());
}
}
/**
* Get HTML header to use
* Get monitored object type independend HTML to use
*
* @return string
*/
protected function getHeader()
protected function getGeneric()
{
return '<h2>' . mt('graphite', 'Graphs') . '</h2>';
$this->handleTimeRangePickerRequest();
return '<h2>' . mt('graphite', 'Graphs') . '</h2>' . $this->renderTimeRangePicker($this->getView());
}
}