mirror of
https://github.com/Icinga/icingaweb2-module-graphite.git
synced 2026-05-28 04:34:57 -04:00
Detail view extension: only show the graphs section if there are graphs to show
This commit is contained in:
parent
db9b59d4de
commit
ceb114701b
2 changed files with 121 additions and 79 deletions
|
|
@ -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 '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
@ -155,91 +162,111 @@ abstract class Graphs extends AbstractWidget
|
|||
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;
|
||||
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) . '">';
|
||||
$classes = $this->classes;
|
||||
$classes[] = 'images';
|
||||
$div = '<div class="' . implode(' ', $classes) . '">';
|
||||
|
||||
$concreteTemplates = [];
|
||||
$defaultTemplates = [];
|
||||
foreach ($templates as $templateName => $template) {
|
||||
if ($this->designedForMyMonitoredObjectType($template)) {
|
||||
$templateCheckCommand = $template->getCheckCommand();
|
||||
$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 ($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')
|
||||
);
|
||||
$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++;
|
||||
}
|
||||
|
||||
$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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -448,4 +475,14 @@ abstract class Graphs extends AbstractWidget
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether there are any graphs to display
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasGraphs()
|
||||
{
|
||||
return $this->getGraphsList() !== '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue