Skip to content

Commit 4296549

Browse files
authored
Merge pull request #23 from NETWAYS/refactor/caching
Rework caching
2 parents 3607666 + 9335fc0 commit 4296549

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

application/controllers/IcingadbimgController.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class IcingadbimgController extends IcingadbGrafanaController
4040
protected $custvarconfig = "grafana_graph_config";
4141
protected $SSLVerifyPeer = false;
4242
protected $SSLVerifyHost = "0";
43-
protected $cacheTime;
43+
protected $cacheTime = 300;
4444
protected $defaultdashboarduid;
45-
protected $refresh = "yes";
45+
protected $enableCache = "yes";
4646
protected $customVars;
4747
protected $timerangeto;
4848
protected $object;
@@ -68,7 +68,9 @@ public function init()
6868
} else {
6969
$this->timerangeto = strpos($this->timerange ?? '', '/') ? 'now-' . $this->timerange ?? '' : "now";
7070
}
71-
$this->cacheTime = $this->hasParam('cachetime') ? $this->getParam('cachetime') : 300;
71+
72+
// Get the cachetime URL parameter and use the default if not present
73+
$this->cacheTime = $this->hasParam('cachetime') ? $this->getParam('cachetime') : $this->cacheTime;
7274

7375
/* load global configuration */
7476
$this->myConfig = Config::module('grafana')->getSection('grafana');
@@ -97,7 +99,7 @@ public function init()
9799
$this->height = $this->myConfig->get('height', $this->height);
98100
$this->width = $this->myConfig->get('width', $this->width);
99101
$this->proxyTimeout = $this->myConfig->get('proxytimeout', $this->proxyTimeout);
100-
$this->refresh = $this->myConfig->get('indirectproxyrefresh', $this->refresh);
102+
$this->enableCache = $this->myConfig->get('enablecache', $this->enableCache);
101103
/**
102104
* Read the global default timerange
103105
*/
@@ -214,21 +216,15 @@ public function indexAction()
214216
}
215217

216218
$imageHtml = "";
217-
$res = $this->getMyimageHtml($serviceName, $hostName, $imageHtml);
218-
header('Pragma: public');
219-
if ($this->refresh == "yes") {
220-
header('Pragma: public');
221-
header("Expires: ".gmdate("D, d M Y H:i:s", time() + $this->cacheTime)." GMT");
222-
header('Cache-Control: max-age='.$this->cacheTime).', public';
223-
} else {
224-
header("Expires: ".gmdate("D, d M Y H:i:s", time() + 365*86440)." GMT");
225-
header('Cache-Control: max-age='. (365*86440));
219+
$result = $this->getMyimageHtml($serviceName, $hostName, $imageHtml);
220+
221+
if ($this->enableCache === 'yes') {
222+
header('Cache-Control: public, max-age=' . $this->cacheTime);
226223
}
224+
227225
header("Content-type: image/png");
228-
if (! $res) {
229-
// set expire to now and max age to 1 minute
230-
header("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");
231-
header('Cache-Control: max-age='. 120);
226+
227+
if (! $result) {
232228
$string = wordwrap($this->translate('Error'). ': ' . $imageHtml, 40, "\n");
233229
$lines = explode("\n", $string);
234230
$im = @imagecreate($this->width, $this->height);
@@ -368,11 +364,11 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
368364
}
369365

370366
curl_setopt_array($curl_handle, $curl_opts);
371-
$res = curl_exec($curl_handle);
367+
$result = curl_exec($curl_handle);
372368

373369
$statusCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
374370

375-
if ($res === false) {
371+
if ($result === false) {
376372
$imageHtml .=$this->translate('Cannot fetch graph with curl') .': '. curl_error($curl_handle). '.';
377373

378374
// Provide a hint for 'Failed to connect to ...: Permission denied'
@@ -383,7 +379,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
383379
}
384380

385381
if ($statusCode > 299) {
386-
$error = @json_decode($res);
382+
$error = @json_decode($result);
387383
$imageHtml .= $this->translate('Cannot fetch Grafana graph')
388384
. ": "
389385
. Util::httpStatusCodeToString($statusCode)
@@ -395,7 +391,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
395391
}
396392

397393
curl_close($curl_handle);
398-
$imageHtml = $res;
394+
$imageHtml = $result;
399395
return true;
400396
}
401397

application/forms/Config/GeneralConfigForm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ public function createElements(array $formData)
239239
if (isset($formData['grafana_accessmode']) && $formData['grafana_accessmode'] === 'indirectproxy') {
240240
$this->addElement(
241241
'select',
242-
'grafana_indirectproxyrefresh',
242+
'grafana_enablecache',
243243
[
244-
'label' => $this->translate('Refresh on indirect proxy'),
244+
'label' => $this->translate('Enable cache'),
245245
'value' => 'yes',
246246
'multiOptions' => [
247247
'yes' => $this->translate('Yes'),
248248
'no' => $this->translate('No'),
249249
],
250-
'description' => $this->translate('Refresh graphs on indirect proxy mode.')
250+
'description' => $this->translate('Enable cache on indirect proxy mode.')
251251
]
252252
);
253253
}

doc/03-module-configuration.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ datasource = "influxdb"
4444
defaultdashboardstore = "db"
4545
accessmode = "proxy"
4646
timeout = "5"
47-
directrefresh = "no"
47+
enablecache = "no"
4848
usepublic = "no"
4949
publichost = "otherhost:3000"
5050
publicprotocol = "http"
@@ -80,7 +80,7 @@ ssl_verifyhost = "0"
8080
|username | **Proxy with basic only** **Required** HTTP Basic Auth user name to access Grafana.|
8181
|password | **Proxy with basic only** **Required** HTTP Basic Auth password to access Grafana. Requires the username setting.|
8282
|apitoken | **Proxy with token only** **Required** Token of a Service Account to access Grafana.|
83-
|indirectproxyrefresh | **Indirect Proxy Only** **Optional.** Refresh graphs on indirect proxy mode. Defaults to `yes`.|
83+
|enablecache | **Indirect Proxy Only** **Optional.** Enables or disable caching graphs. Defaults to `yes`. |
8484
|usepublic | **Optional** Enable usage of publichost/protocol. Defaults to `no`.|
8585
|publichost | **Optional** Use a different host for the graph links.|
8686
|publicprotocol | **Optional** Use a different protocol for the graph links.|
@@ -190,9 +190,10 @@ The password used to authenticate to Grafana server.
190190
For token access you need to create a Service Account and assign it a token.
191191
See the [Grafana Docs](https://grafana.com/docs/grafana/latest/administration/service-accounts/).
192192

193-
### indirectproxyrefresh
194-
Only for `indirectproxy` access mode. Enables or disable graph refresh with an interval of the service interval.
195-
Defaults to `yes`.
193+
### enablecache
194+
Only for `indirectproxy` access mode. Enables or disable caching graphs. Defaults to `yes`.
195+
196+
Note that depending on your browser images might be cached implicitly.
196197

197198
### usepublic
198199
Enables/Disables the usage of a `public` URL to the Grafana server.

library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
352352
public function getPreviewHtml(Model $object, $report = false)
353353
{
354354
$this->object = $object;
355-
//$this->cacheTime = round($object->state->next_check - $object->state->last_update);
356-
$this->cacheTime = 0;
355+
// Use the cachetime based on the check intervals
356+
$this->cacheTime = $object->check_interval;
357357

358358
if ($object instanceof Host) {
359359
$serviceName = $object->checkcommand_name;

0 commit comments

Comments
 (0)