Skip to content

Commit

Permalink
Detail view extension: only show the graphs section if there are grap…
Browse files Browse the repository at this point in the history
…hs to show
  • Loading branch information
Al2Klimov committed Nov 27, 2017
1 parent db9b59d commit ceb1147
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 82 deletions.
25 changes: 15 additions & 10 deletions library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ class DetailviewExtension extends DetailviewExtensionHook

public function getHtmlForObject(MonitoredObject $object)
{
$this->handleTimeRangePickerRequest();
return '<h2>' . mt('graphite', 'Graphs') . '</h2>'
. $this->renderTimeRangePicker($this->getView())
. Graphs::forMonitoredObject($object)
->setWidth(440)
->setHeight(220)
->setClasses(['monitored-object-detail-view'])
->setMaxVisibleGraphs(2)
->setPreloadDummy()
->handleRequest();
$graphs = Graphs::forMonitoredObject($object)
->setWidth(440)
->setHeight(220)
->setClasses(['monitored-object-detail-view'])
->setMaxVisibleGraphs(2)
->setPreloadDummy()
->handleRequest();

if ($graphs->hasGraphs()) {
$this->handleTimeRangePickerRequest();
return '<h2>' . mt('graphite', 'Graphs') . '</h2>'
. $this->renderTimeRangePicker($this->getView()) . $graphs;
}

return '';
}
}
181 changes: 109 additions & 72 deletions library/Graphite/Web/Widget/Graphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ abstract class Graphs extends AbstractWidget
*/
protected $preloadDummy = false;

/**
* Cache for {@link getGraphsList()}
*
* @var string
*/
protected $graphsList;

/**
* Factory, based on the given object
*
Expand Down Expand Up @@ -155,91 +162,111 @@ public function handleRequest(Request $request = null)
return $this;
}

public function render()
/**
* Render the graphs list
*
* @return string
*/
protected function getGraphsList()
{
/** @var View $view */
$view = $this->view();
$result = []; // kind of string builder
$filter = $this->getMonitoredObjectFilter();
$imageBaseUrl = $this->preloadDummy ? $this->getDummyImageBaseUrl() : $this->getImageBaseUrl();
$templates = static::getAllTemplates()->getTemplates();
$checkCommand = $this->obscuredCheckCommand === null ? $this->checkCommand : $this->obscuredCheckCommand;
$limit = $this->maxVisibleGraphs;

$classes = $this->classes;
$classes[] = 'images';
$div = '<div class="' . implode(' ', $classes) . '">';

$concreteTemplates = [];
$defaultTemplates = [];
foreach ($templates as $templateName => $template) {
if ($this->designedForMyMonitoredObjectType($template)) {
$templateCheckCommand = $template->getCheckCommand();

if ($templateCheckCommand === $checkCommand) {
$concreteTemplates[$templateName] = $template;
} elseif ($templateCheckCommand === null) {
$defaultTemplates[$templateName] = $template;
if ($this->graphsList === null) {
/** @var View $view */
$view = $this->view();
$result = []; // kind of string builder
$filter = $this->getMonitoredObjectFilter();
$imageBaseUrl = $this->preloadDummy ? $this->getDummyImageBaseUrl() : $this->getImageBaseUrl();
$templates = static::getAllTemplates()->getTemplates();
$checkCommand = $this->obscuredCheckCommand === null ? $this->checkCommand : $this->obscuredCheckCommand;
$limit = $this->maxVisibleGraphs;

$classes = $this->classes;
$classes[] = 'images';
$div = '<div class="' . implode(' ', $classes) . '">';

$concreteTemplates = [];
$defaultTemplates = [];
foreach ($templates as $templateName => $template) {
if ($this->designedForMyMonitoredObjectType($template)) {
$templateCheckCommand = $template->getCheckCommand();

if ($templateCheckCommand === $checkCommand) {
$concreteTemplates[$templateName] = $template;
} elseif ($templateCheckCommand === null) {
$defaultTemplates[$templateName] = $template;
}
}
}
}

$renderedGraphs = 0;
foreach ((empty($concreteTemplates) ? $defaultTemplates : $concreteTemplates) as $templateName => $template) {
$charts = $template->getCharts(static::getMetricsDataSource(), $filter, $this->checkCommand);
if (! empty($charts)) {
foreach ($charts as $chart) {
if (empty($result)) {
$result[] = $div;
} elseif ($limit && $renderedGraphs === $limit) {
$result[] = sprintf(
'<input type="checkbox" id="toggle-%1$s" class="expandable-toggle">'
. '<label for="toggle-%1$s" class="link-button">'
. '<span class="expandable-expand-label">%2$s</span>'
. '<span class="expandable-collapse-label">%3$s</span>'
. '</label>'
. '<div class="expandable-content">',
$view->protectId($this->getMonitoredObjectIdentifier()),
$view->translate('Show More'),
$view->translate('Show Less')
);
}

$imageUrl = $this->filterImageUrl($imageBaseUrl->with($chart->getMetricVariables()))
->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);
$renderedGraphs = 0;
foreach ((empty($concreteTemplates) ? $defaultTemplates : $concreteTemplates) as $templateName => $template) {
$charts = $template->getCharts(static::getMetricsDataSource(), $filter, $this->checkCommand);
if (! empty($charts)) {
foreach ($charts as $chart) {
if (empty($result)) {
$result[] = $div;
} elseif ($limit && $renderedGraphs === $limit) {
$result[] = sprintf(
'<input type="checkbox" id="toggle-%1$s" class="expandable-toggle">'
. '<label for="toggle-%1$s" class="link-button">'
. '<span class="expandable-expand-label">%2$s</span>'
. '<span class="expandable-collapse-label">%3$s</span>'
. '</label>'
. '<div class="expandable-content">',
$view->protectId($this->getMonitoredObjectIdentifier()),
$view->translate('Show More'),
$view->translate('Show Less')
);
}

$imageUrl = $this->filterImageUrl($imageBaseUrl->with($chart->getMetricVariables()))
->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);
}

$result[] = '<img id="graphiteImg-';
$result[] = md5((string) $imageUrl->without('cachebuster'));
$result[] = '" src="';
$result[] = (string) $imageUrl;
$result[] = '" class="detach graphiteImg" alt="" width="';
$result[] = $this->width;
$result[] = '" height="';
$result[] = $this->height;
$result[] = '">';
$renderedGraphs++;
}

$result[] = '<img id="graphiteImg-';
$result[] = md5((string) $imageUrl->without('cachebuster'));
$result[] = '" src="';
$result[] = (string) $imageUrl;
$result[] = '" class="detach graphiteImg" alt="" width="';
$result[] = $this->width;
$result[] = '" height="';
$result[] = $this->height;
$result[] = '">';
$renderedGraphs++;
}
}
}

if (! empty($result)) {
if ($limit && $renderedGraphs > $limit) {
if (! empty($result)) {
if ($limit && $renderedGraphs > $limit) {
$result[] = '</div>';
}

$result[] = '</div>';
}

$result[] = '</div>';
return implode($result);
} else {
$this->graphsList = implode($result);
}

return $this->graphsList;
}

public function render()
{
$result = $this->getGraphsList();

if ($result === '') {
$view = $this->view();
return "<p>{$view->escape($view->translate('No graphs found'))}</p>";
}

return $result;
}

/**
Expand Down Expand Up @@ -448,4 +475,14 @@ public function setPreloadDummy($preloadDummy = true)

return $this;
}

/**
* Whether there are any graphs to display
*
* @return bool
*/
public function hasGraphs()
{
return $this->getGraphsList() !== '';
}
}

0 comments on commit ceb1147

Please sign in to comment.