diff --git a/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php b/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php index 0b3f163..5c1613f 100644 --- a/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php +++ b/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php @@ -13,15 +13,20 @@ class DetailviewExtension extends DetailviewExtensionHook public function getHtmlForObject(MonitoredObject $object) { - $this->handleTimeRangePickerRequest(); - return '

' . mt('graphite', 'Graphs') . '

' - . $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 '

' . mt('graphite', 'Graphs') . '

' + . $this->renderTimeRangePicker($this->getView()) . $graphs; + } + + return ''; } } diff --git a/library/Graphite/Web/Widget/Graphs.php b/library/Graphite/Web/Widget/Graphs.php index 68c066d..aa7184e 100644 --- a/library/Graphite/Web/Widget/Graphs.php +++ b/library/Graphite/Web/Widget/Graphs.php @@ -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 = '
'; + $classes = $this->classes; + $classes[] = 'images'; + $div = '
'; - $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( - '' - . '' - . '
', - $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( + '' + . '' + . '
', + $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[] = ''; + $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[] = ''; - $renderedGraphs++; } } - } - if (! empty($result)) { - if ($limit && $renderedGraphs > $limit) { + if (! empty($result)) { + if ($limit && $renderedGraphs > $limit) { + $result[] = '
'; + } + $result[] = '
'; } - $result[] = '
'; - return implode($result); - } else { + $this->graphsList = implode($result); + } + + return $this->graphsList; + } + + public function render() + { + $result = $this->getGraphsList(); + + if ($result === '') { + $view = $this->view(); return "

{$view->escape($view->translate('No graphs found'))}

"; } + + 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() !== ''; + } }