From 1c5c57b5cf2d134a5500aa9d1b112dc7c3f229cd Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 29 Sep 2017 10:26:58 +0200 Subject: [PATCH] Centralize operations on hardcoded paths refs #54 --- application/controllers/GraphController.php | 9 ++-- library/Graphite/Graphing/GraphingTrait.php | 52 +++++++++++++++++++++ library/Graphite/Web/Widget/Graphs.php | 8 ++-- library/Graphite/Web/Widget/GraphsTrait.php | 31 ------------ 4 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 library/Graphite/Graphing/GraphingTrait.php delete mode 100644 library/Graphite/Web/Widget/GraphsTrait.php diff --git a/application/controllers/GraphController.php b/application/controllers/GraphController.php index 37e73de..ef9c1d4 100644 --- a/application/controllers/GraphController.php +++ b/application/controllers/GraphController.php @@ -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) ); diff --git a/library/Graphite/Graphing/GraphingTrait.php b/library/Graphite/Graphing/GraphingTrait.php new file mode 100644 index 0000000..33378ac --- /dev/null +++ b/library/Graphite/Graphing/GraphingTrait.php @@ -0,0 +1,52 @@ +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; + } +} diff --git a/library/Graphite/Web/Widget/Graphs.php b/library/Graphite/Web/Widget/Graphs.php index 6d0eccb..1a47560 100644 --- a/library/Graphite/Web/Widget/Graphs.php +++ b/library/Graphite/Web/Widget/Graphs.php @@ -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[] = '
'; diff --git a/library/Graphite/Web/Widget/GraphsTrait.php b/library/Graphite/Web/Widget/GraphsTrait.php deleted file mode 100644 index b0f04e6..0000000 --- a/library/Graphite/Web/Widget/GraphsTrait.php +++ /dev/null @@ -1,31 +0,0 @@ -loadDir(Config::resolvePath('modules/graphite/templates')); - } - - return static::$allTemplates; - } -}