From f02ab198725b33bf7f3237c2cd79e19b3fe5463b Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 7 Sep 2017 17:33:46 +0200 Subject: [PATCH] Use the time range picker in the detail view refs #27 --- application/controllers/ShowController.php | 52 +++++++++++++------ .../TimeRangePicker/TimeRangePickerTrait.php | 28 ++++++++++ library/Graphite/EmbedGraphs.php | 13 +++-- library/Graphite/GraphiteChart.php | 38 +++++++++++++- .../Monitoring/DetailviewExtension.php | 14 +++-- 5 files changed, 114 insertions(+), 31 deletions(-) diff --git a/application/controllers/ShowController.php b/application/controllers/ShowController.php index 849f8c6..3b85366 100644 --- a/application/controllers/ShowController.php +++ b/application/controllers/ShowController.php @@ -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 diff --git a/application/forms/TimeRangePicker/TimeRangePickerTrait.php b/application/forms/TimeRangePicker/TimeRangePickerTrait.php index 26577a5..cb4f0bd 100644 --- a/application/forms/TimeRangePicker/TimeRangePickerTrait.php +++ b/application/forms/TimeRangePicker/TimeRangePickerTrait.php @@ -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 * diff --git a/library/Graphite/EmbedGraphs.php b/library/Graphite/EmbedGraphs.php index 4ff2e2a..66212fc 100644 --- a/library/Graphite/EmbedGraphs.php +++ b/library/Graphite/EmbedGraphs.php @@ -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 "
is enough", // but this seems not to work for me return '
'; + . ' data-icinga-url="' . $url->setParam('view', 'compact') . '">
'; } /** diff --git a/library/Graphite/GraphiteChart.php b/library/Graphite/GraphiteChart.php index 13d2f1a..d09e00a 100644 --- a/library/Graphite/GraphiteChart.php +++ b/library/Graphite/GraphiteChart.php @@ -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() diff --git a/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php b/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php index 42beac2..541b8b0 100644 --- a/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php +++ b/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php @@ -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 '

' . mt('graphite', 'Graphs') . '

'; + $this->handleTimeRangePickerRequest(); + return '

' . mt('graphite', 'Graphs') . '

' . $this->renderTimeRangePicker($this->getView()); } }