From 9d1f03d6fd747adfb4ca6a03f136a9c435cb054c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 19 Oct 2017 18:00:19 +0200 Subject: [PATCH] Monitored object detail view extension: serve a 1x1 #00000000 image first refs #68 --- .../controllers/GraphDummyController.php | 39 ++++++++++++++++++ .../Monitoring/DetailviewExtension.php | 1 + library/Graphite/Web/Widget/Graphs.php | 40 ++++++++++++++++++- library/Graphite/Web/Widget/Graphs/Host.php | 5 +++ .../Graphite/Web/Widget/Graphs/Service.php | 5 +++ public/js/module.js | 2 +- 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 application/controllers/GraphDummyController.php diff --git a/application/controllers/GraphDummyController.php b/application/controllers/GraphDummyController.php new file mode 100644 index 0000000..976433d --- /dev/null +++ b/application/controllers/GraphDummyController.php @@ -0,0 +1,39 @@ +supplyImage(); + } + + public function serviceAction() + { + $this->supplyImage(); + } + + /** + * Do all monitored object type independend actions + */ + protected function supplyImage() + { + $this->getResponse() + ->setHeader('Content-Type', 'image/png', true) + ->setHeader('Content-Disposition', 'inline; filename="dummy.png"', true) + ->setBody( + "\x89PNG\x0d\x0a\x1a\x0a\x00\x00\x00\x0dIHDR\x00\x00\x00\x01" + . "\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x06bKG" + . "D\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00\x00\x09pHYs\x00" + . "\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x0bIDAT" + . "\x08\xd7c`\x00\x02\x00\x00\x05\x00\x01\xe2&\x05\x9b\x00\x00\x00\x00I" + . "END\xaeB`\x82" + ) + ->sendResponse(); + + exit; + } +} diff --git a/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php b/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php index 7450167..b044ddd 100644 --- a/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php +++ b/library/Graphite/ProvidedHook/Monitoring/DetailviewExtension.php @@ -21,6 +21,7 @@ class DetailviewExtension extends DetailviewExtensionHook ->setWidth(440) ->setHeight(220) ->setClasses(['monitored-object-detail-view']) + ->setPreloadDummy() ->handleRequest(); } } diff --git a/library/Graphite/Web/Widget/Graphs.php b/library/Graphite/Web/Widget/Graphs.php index f3f23c4..a90b1cc 100644 --- a/library/Graphite/Web/Widget/Graphs.php +++ b/library/Graphite/Web/Widget/Graphs.php @@ -78,6 +78,13 @@ abstract class Graphs extends AbstractWidget */ protected $classes = []; + /** + * Whether to serve a transparent dummy image first and let the JS code load the actual graph + * + * @var bool + */ + protected $preloadDummy = false; + /** * Factory, based on the given object * @@ -147,7 +154,7 @@ abstract class Graphs extends AbstractWidget $view = $this->view(); $result = []; // kind of string builder $filter = $this->getMonitoredObjectFilter(); - $imageBaseUrl = $this->getImageBaseUrl(); + $imageBaseUrl = $this->preloadDummy ? $this->getDummyImageBaseUrl() : $this->getImageBaseUrl(); $templates = static::getAllTemplates()->getTemplates(); $checkCommand = $this->obscuredCheckCommand === null ? $this->checkCommand : $this->obscuredCheckCommand; @@ -234,6 +241,13 @@ abstract class Graphs extends AbstractWidget */ abstract protected function getImageBaseUrl(); + /** + * Get the base URL to a dummy image specifying just the monitored object kind + * + * @return Url + */ + abstract protected function getDummyImageBaseUrl(); + /** * Extend the {@link getImageBaseUrl()}'s result's parameters with the concrete monitored object * @@ -337,4 +351,28 @@ abstract class Graphs extends AbstractWidget return $this; } + + /** + * Get whether to serve a transparent dummy image first and let the JS code load the actual graph + * + * @return bool + */ + public function getPreloadDummy() + { + return $this->preloadDummy; + } + + /** + * Set whether to serve a transparent dummy image first and let the JS code load the actual graph + * + * @param bool $preloadDummy + * + * @return $this + */ + public function setPreloadDummy($preloadDummy = true) + { + $this->preloadDummy = $preloadDummy; + + return $this; + } } diff --git a/library/Graphite/Web/Widget/Graphs/Host.php b/library/Graphite/Web/Widget/Graphs/Host.php index 8e45476..21a669f 100644 --- a/library/Graphite/Web/Widget/Graphs/Host.php +++ b/library/Graphite/Web/Widget/Graphs/Host.php @@ -35,6 +35,11 @@ class Host extends Graphs return Url::fromPath('graphite/graph/host'); } + protected function getDummyImageBaseUrl() + { + return Url::fromPath('graphite/graph-dummy/host'); + } + protected function filterImageUrl(Url $url) { return $url->setParam('host.name', $this->host); diff --git a/library/Graphite/Web/Widget/Graphs/Service.php b/library/Graphite/Web/Widget/Graphs/Service.php index 55b049e..3b292d2 100644 --- a/library/Graphite/Web/Widget/Graphs/Service.php +++ b/library/Graphite/Web/Widget/Graphs/Service.php @@ -44,6 +44,11 @@ class Service extends Graphs return Url::fromPath('graphite/graph/service'); } + protected function getDummyImageBaseUrl() + { + return Url::fromPath('graphite/graph-dummy/service'); + } + protected function filterImageUrl(Url $url) { return $url->setParam('host.name', $this->host)->setParam('service.name', $this->service); diff --git a/public/js/module.js b/public/js/module.js index b0c0e7b..08087fd 100644 --- a/public/js/module.js +++ b/public/js/module.js @@ -139,7 +139,7 @@ renderedUrlParams.push(urlParam + "=" + urlParams[urlParam]); } - e.attr("src", matchParams[1] + "?" + renderedUrlParams.join("&")); + e.attr("src", matchParams[1].replace("-dummy", "") + "?" + renderedUrlParams.join("&")); } } }