Skip to content

Commit

Permalink
Merge branch 'feature/always-use-default-template-135'
Browse files Browse the repository at this point in the history
resolves #135
  • Loading branch information
Al2Klimov committed Mar 19, 2018
2 parents 7b6827a + 57150d2 commit 6caea7a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 50 deletions.
24 changes: 18 additions & 6 deletions application/controllers/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class GraphController extends MonitoringAwareController
*
* @var string[]
*/
protected $graphParamsNames = ['start', 'end', 'width', 'height', 'legend', 'template', 'cachebuster'];
protected $graphParamsNames = [
'start', 'end',
'width', 'height',
'legend',
'template', 'default_template',
'cachebuster'
];

/**
* The URL parameters for metrics filtering
Expand Down Expand Up @@ -89,15 +95,21 @@ public function serviceAction()
*/
protected function supplyImage($checkCommand, $obscuredCheckCommand)
{
$templates = $this->getAllTemplates()->getTemplates(
$obscuredCheckCommand === null ? $checkCommand : $obscuredCheckCommand
);
if (isset($this->graphParams['default_template'])) {
$urlParam = 'default_template';
$templates = $this->getAllTemplates()->getDefaultTemplates();
} else {
$urlParam = 'template';
$templates = $this->getAllTemplates()->getTemplates(
$obscuredCheckCommand === null ? $checkCommand : $obscuredCheckCommand
);
}

if (! isset($templates[$this->graphParams['template']])) {
if (! isset($templates[$this->graphParams[$urlParam]])) {
throw new HttpNotFoundException($this->translate('No such template'));
}

$charts = $templates[$this->graphParams['template']]->getCharts(
$charts = $templates[$this->graphParams[$urlParam]]->getCharts(
static::getMetricsDataSource(),
array_map('rawurldecode', $this->filterParams->toArray(false)),
$checkCommand
Expand Down
9 changes: 8 additions & 1 deletion library/Graphite/Graphing/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public function __construct()
* @param MetricsDataSource $dataSource
* @param string[] $filter
* @param string $checkCommand The check command of the monitored object we fetch charts for
* @param MacroTemplate[] $excludeMetrics
*
* @return Chart[]
*/
public function getCharts(MetricsDataSource $dataSource, array $filter, $checkCommand)
public function getCharts(MetricsDataSource $dataSource, array $filter, $checkCommand, array & $excludeMetrics = [])
{
$metrics = [];
foreach ($this->curves as $curveName => $curve) {
Expand All @@ -79,6 +80,12 @@ public function getCharts(MetricsDataSource $dataSource, array $filter, $checkCo
}

foreach ($query->fetchColumn() as $metric) {
foreach ($excludeMetrics as $excludeMetric) {
if ($excludeMetric->reverseResolve($metric) !== false) {
continue 2;
}
}

$vars = $curve[0]->reverseResolve($metric);
if ($vars !== false) {
$metrics[$curveName][$metric] = $vars;
Expand Down
14 changes: 12 additions & 2 deletions library/Graphite/Graphing/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ function($option) {
}

/**
* Get all loaded templates for the given check command by their name, fall back to the default one(s)
* Get all loaded templates for the given check command by their names
*
* @param string $checkCommand
*
* @return Template[]
*/
public function getTemplates($checkCommand)
{
return isset($this->templates[$checkCommand]) ? $this->templates[$checkCommand] : $this->defaultTemplates;
return isset($this->templates[$checkCommand]) ? $this->templates[$checkCommand] : [];
}

/**
Expand All @@ -315,4 +315,14 @@ public function getAllTemplates()
{
return $this->templates;
}

/**
* Get all loaded default templates by their names
*
* @return Template[]
*/
public function getDefaultTemplates()
{
return $this->defaultTemplates;
}
}
102 changes: 61 additions & 41 deletions library/Graphite/Web/Widget/Graphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,54 +196,74 @@ protected function getGraphsList()
$result = []; // kind of string builder
$filter = $this->getMonitoredObjectFilter();
$imageBaseUrl = $this->preloadDummy ? $this->getDummyImageBaseUrl() : $this->getImageBaseUrl();

foreach (static::getAllTemplates()->getTemplates(
$allTemplates = $this->getAllTemplates();
$concreteTemplates = $allTemplates->getTemplates(
$this->obscuredCheckCommand === null ? $this->checkCommand : $this->obscuredCheckCommand
) as $templateName => $template) {
if ($this->designedForMyMonitoredObjectType($template)) {
$charts = $template->getCharts(static::getMetricsDataSource(), $filter, $this->checkCommand);
if (! empty($charts)) {
$currentGraphs = [];

foreach ($charts as $chart) {
$metricVariables = $chart->getMetricVariables();
$imageUrl = $this->filterImageUrl($imageBaseUrl->with($metricVariables))
->setParam('template', $templateName)
->setParam('start', $this->start)
->setParam('end', $this->end)
->setParam('width', $this->width)
->setParam('height', $this->height)
->setParam('cachebuster', time() * 65536 + mt_rand(0, 65535));
if (! $this->compact) {
$imageUrl->setParam('legend', 1);
}
);

$excludedMetrics = [];

foreach ($concreteTemplates as $concreteTemplate) {
foreach ($concreteTemplate->getCurves() as $curve) {
$excludedMetrics[] = $curve[0];
}
}

foreach ([
['template', $concreteTemplates, []],
['default_template', $allTemplates->getDefaultTemplates(), $excludedMetrics],
] as $templateSet) {
list($urlParam, $templates, $excludeMetrics) = $templateSet;

foreach ($templates as $templateName => $template) {
if ($this->designedForMyMonitoredObjectType($template)) {
$charts = $template->getCharts(
static::getMetricsDataSource(), $filter, $this->checkCommand, $excludeMetrics
);

if (! empty($charts)) {
$currentGraphs = [];

foreach ($charts as $chart) {
$metricVariables = $chart->getMetricVariables();
$imageUrl = $this->filterImageUrl($imageBaseUrl->with($metricVariables))
->setParam($urlParam, $templateName)
->setParam('start', $this->start)
->setParam('end', $this->end)
->setParam('width', $this->width)
->setParam('height', $this->height)
->setParam('cachebuster', time() * 65536 + mt_rand(0, 65535));
if (! $this->compact) {
$imageUrl->setParam('legend', 1);
}

$bestIntersect = -1;
$bestPos = count($result);
$bestIntersect = -1;
$bestPos = count($result);

foreach ($result as $graphPos => & $graph) {
$currentIntersect = count(array_intersect_assoc($graph[1], $metricVariables));
foreach ($result as $graphPos => & $graph) {
$currentIntersect = count(array_intersect_assoc($graph[1], $metricVariables));

if ($currentIntersect >= $bestIntersect) {
$bestIntersect = $currentIntersect;
$bestPos = $graphPos + 1;
if ($currentIntersect >= $bestIntersect) {
$bestIntersect = $currentIntersect;
$bestPos = $graphPos + 1;
}
}
unset($graph);

$currentGraphs[] = [
'<img id="graphiteImg-'
. md5((string) $imageUrl->without('cachebuster'))
. "\" src=\"$imageUrl\" class=\"detach graphiteImg\" alt=\"\""
. " width=\"$this->width\" height=\"$this->height\">",
$metricVariables,
$bestPos
];
}
unset($graph);

$currentGraphs[] = [
'<img id="graphiteImg-'
. md5((string) $imageUrl->without('cachebuster'))
. "\" src=\"$imageUrl\" class=\"detach graphiteImg\" alt=\"\""
. " width=\"$this->width\" height=\"$this->height\">",
$metricVariables,
$bestPos
];
}

foreach (array_reverse($currentGraphs) as $graph) {
list($img, $metricVariables, $bestPos) = $graph;
array_splice($result, $bestPos, 0, [[$img, $metricVariables]]);
foreach (array_reverse($currentGraphs) as $graph) {
list($img, $metricVariables, $bestPos) = $graph;
array_splice($result, $bestPos, 0, [[$img, $metricVariables]]);
}
}
}
}
Expand Down

0 comments on commit 6caea7a

Please sign in to comment.