Centralize operations on hardcoded paths

refs #54
This commit is contained in:
Alexander A. Klimov 2017-09-29 10:26:58 +02:00
parent f9b8541c51
commit 1c5c57b5cf
4 changed files with 58 additions and 42 deletions

View file

@ -5,17 +5,14 @@ namespace Icinga\Module\Graphite\Controllers;
use DateTimeZone;
use Icinga\Exception\Http\HttpBadRequestException;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Graphite\Graphing\MetricsDataSource;
use Icinga\Module\Graphite\GraphiteUtil;
use Icinga\Module\Graphite\GraphiteWebClient;
use Icinga\Module\Graphite\Graphing\GraphingTrait;
use Icinga\Module\Graphite\Web\Controller\MonitoringAwareController;
use Icinga\Module\Graphite\Web\Widget\GraphsTrait;
use Icinga\Util\TimezoneDetect;
use Icinga\Web\UrlParams;
class GraphController extends MonitoringAwareController
{
use GraphsTrait;
use GraphingTrait;
/**
* The URL parameters for the graph
@ -93,7 +90,7 @@ class GraphController extends MonitoringAwareController
}
$charts = $templates[$this->graphParams['template']]->getCharts(
new MetricsDataSource(GraphiteWebClient::getInstance()),
static::getMetricsDataSource(),
$this->filterParams->toArray(false)
);

View file

@ -0,0 +1,52 @@
<?php
namespace Icinga\Module\Graphite\Graphing;
use Icinga\Application\Config;
use Icinga\Module\Graphite\GraphiteWebClient;
trait GraphingTrait
{
/**
* All loaded templates
*
* @var Templates
*/
protected static $allTemplates;
/**
* Metrics data source
*
* @var MetricsDataSource
*/
protected static $metricsDataSource;
/**
* Load and get all templates
*
* @return Templates
*/
protected static function getAllTemplates()
{
if (static::$allTemplates === null) {
static::$allTemplates = (new Templates())
->loadDir(Config::resolvePath('modules/graphite/templates'));
}
return static::$allTemplates;
}
/**
* Get metrics data source
*
* @return MetricsDataSource
*/
public static function getMetricsDataSource()
{
if (static::$metricsDataSource === null) {
static::$metricsDataSource = new MetricsDataSource(GraphiteWebClient::getInstance());
}
return static::$metricsDataSource;
}
}

View file

@ -4,9 +4,8 @@ namespace Icinga\Module\Graphite\Web\Widget;
use Icinga\Application\Icinga;
use Icinga\Module\Graphite\Forms\TimeRangePicker\TimeRangePickerTrait;
use Icinga\Module\Graphite\Graphing\MetricsDataSource;
use Icinga\Module\Graphite\Graphing\GraphingTrait;
use Icinga\Module\Graphite\Graphing\Template;
use Icinga\Module\Graphite\GraphiteWebClient;
use Icinga\Module\Graphite\Web\Widget\Graphs\Host as HostGraphs;
use Icinga\Module\Graphite\Web\Widget\Graphs\Service as ServiceGraphs;
use Icinga\Module\Monitoring\Object\Host;
@ -19,7 +18,7 @@ use Icinga\Web\Widget\AbstractWidget;
abstract class Graphs extends AbstractWidget
{
use GraphsTrait;
use GraphingTrait;
/**
* Graph image width
@ -102,7 +101,6 @@ abstract class Graphs extends AbstractWidget
/** @var View $view */
$view = $this->view();
$result = []; // kind of string builder
$dataSource = new MetricsDataSource(GraphiteWebClient::getInstance());
$filter = $this->getMonitoredObjectFilter();
$imageBaseUrl = $this->getImageBaseUrl();
$templates = static::getAllTemplates()->getTemplates();
@ -112,7 +110,7 @@ abstract class Graphs extends AbstractWidget
foreach ($templateNames as $templateName) {
if ($this->designedForMyMonitoredObjectType($templates[$templateName])) {
$charts = $templates[$templateName]->getCharts($dataSource, $filter);
$charts = $templates[$templateName]->getCharts(static::getMetricsDataSource(), $filter);
if (! empty($charts)) {
$result[] = '<div class="images">';

View file

@ -1,31 +0,0 @@
<?php
namespace Icinga\Module\Graphite\Web\Widget;
use Icinga\Application\Config;
use Icinga\Module\Graphite\Graphing\Templates;
trait GraphsTrait
{
/**
* All loaded templates
*
* @var Templates
*/
protected static $allTemplates;
/**
* Load and get all templates
*
* @return Templates
*/
protected static function getAllTemplates()
{
if (static::$allTemplates === null) {
static::$allTemplates = (new Templates())
->loadDir(Config::resolvePath('modules/graphite/templates'));
}
return static::$allTemplates;
}
}