Remove all kinds of flickering

refs #72
This commit is contained in:
Alexander A. Klimov 2017-11-16 18:34:32 +01:00
parent 0f536c660c
commit 369e39382b
4 changed files with 67 additions and 4 deletions

View file

@ -19,7 +19,7 @@ class GraphController extends MonitoringAwareController
*
* @var string[]
*/
protected $graphParamsNames = ['start', 'end', 'width', 'height', 'legend', 'template'];
protected $graphParamsNames = ['start', 'end', 'width', 'height', 'legend', 'template', 'cachebuster'];
/**
* The URL parameters for metrics filtering

View file

@ -158,6 +158,9 @@ class Chart
$response
->setHeader('Content-Type', 'image/png', true)
->setHeader('Content-Disposition', 'inline; filename="graph.png"', true)
->setHeader('Cache-Control', null, true)
->setHeader('Expires', null, true)
->setHeader('Pragma', null, true)
->setBody($image)
->sendResponse();

View file

@ -210,14 +210,17 @@ abstract class Graphs extends AbstractWidget
->setParam('start', $this->start)
->setParam('end', $this->end)
->setParam('width', $this->width)
->setParam('height', $this->height);
->setParam('height', $this->height)
->setParam('cachebuster', time() * 65536 + mt_rand(0, 65535));
if (! $this->compact) {
$imageUrl->setParam('legend', 1);
}
$result[] = '<img src="';
$result[] = '<img id="graphiteImg-';
$result[] = md5((string) $imageUrl->without('cachebuster'));
$result[] = '" src="';
$result[] = (string) $imageUrl;
$result[] = '" class="graphiteImg" alt="" width="';
$result[] = '" class="detach graphiteImg" alt="" width="';
$result[] = $this->width;
$result[] = '" height="';
$result[] = $this->height;

View file

@ -103,6 +103,63 @@
}(Icinga));
(function(Icinga, $) {
'use strict';
var extractUrlParams = /^([^?]*)\?(.+)$/;
var parseUrlParam = /^([^=]+)=(.*)$/;
function GraphiteCachebusterUpdater(icinga) {
Icinga.EventListener.call(this, icinga);
this.on('rendered', this.onRendered, this);
}
GraphiteCachebusterUpdater.prototype = new Icinga.EventListener();
GraphiteCachebusterUpdater.prototype.onRendered = function(event) {
$(event.target).find('img.graphiteImg').each(function() {
var e = $(this);
var src = e.attr('src');
if (typeof(src) !== 'undefined') {
var matchParams = extractUrlParams.exec(src);
if (matchParams !== null) {
var urlParams = Object.create(null);
matchParams[2].split('&').forEach(function(urlParam) {
var matchParam = parseUrlParam.exec(urlParam);
if (matchParam !== null) {
urlParams[matchParam[1]] = matchParam[2];
}
});
if (typeof(urlParams.cachebuster) !== 'undefined') {
var cachebuster = parseInt(urlParams.cachebuster);
if (cachebuster === cachebuster) {
urlParams.cachebuster = (cachebuster + 1).toString();
var renderedUrlParams = [];
for (var urlParam in urlParams) {
renderedUrlParams.push(urlParam + '=' + urlParams[urlParam]);
}
e.attr('src', matchParams[1] + '?' + renderedUrlParams.join('&'));
}
}
}
}
});
};
Icinga.Behaviors = Icinga.Behaviors || {};
Icinga.Behaviors.GraphiteCachebusterUpdater = GraphiteCachebusterUpdater;
}(Icinga, jQuery));
(function(Icinga, $) {
'use strict';