Skip to content

Commit fd51c8e

Browse files
committed
Rework caching
- Rename indirectproxyrefresh to enablecaching, defaults to yes - Calculate the cache time based on the check interval
1 parent 0ca8260 commit fd51c8e

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

application/controllers/IcingadbimgController.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class IcingadbimgController extends IcingadbGrafanaController
4242
protected $SSLVerifyHost = "0";
4343
protected $cacheTime;
4444
protected $defaultdashboarduid;
45-
protected $refresh = "yes";
45+
protected $enableCache = "yes";
4646
protected $customVars;
4747
protected $timerangeto;
4848
protected $object;
@@ -68,6 +68,7 @@ public function init()
6868
} else {
6969
$this->timerangeto = strpos($this->timerange ?? '', '/') ? 'now-' . $this->timerange ?? '' : "now";
7070
}
71+
7172
$this->cacheTime = $this->hasParam('cachetime') ? $this->getParam('cachetime') : 300;
7273

7374
/* load global configuration */
@@ -97,7 +98,7 @@ public function init()
9798
$this->height = $this->myConfig->get('height', $this->height);
9899
$this->width = $this->myConfig->get('width', $this->width);
99100
$this->proxyTimeout = $this->myConfig->get('proxytimeout', $this->proxyTimeout);
100-
$this->refresh = $this->myConfig->get('indirectproxyrefresh', $this->refresh);
101+
$this->enableCache = $this->myConfig->get('enablecache', $this->enableCache);
101102
/**
102103
* Read the global default timerange
103104
*/
@@ -214,21 +215,15 @@ public function indexAction()
214215
}
215216

216217
$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));
218+
$result = $this->getMyimageHtml($serviceName, $hostName, $imageHtml);
219+
220+
if ($this->enableCache === 'yes') {
221+
header('Cache-Control: public, max-age=' . $this->cacheTime);
226222
}
223+
227224
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);
225+
226+
if (! $result) {
232227
$string = wordwrap($this->translate('Error'). ': ' . $imageHtml, 40, "\n");
233228
$lines = explode("\n", $string);
234229
$im = @imagecreate($this->width, $this->height);
@@ -368,11 +363,11 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
368363
}
369364

370365
curl_setopt_array($curl_handle, $curl_opts);
371-
$res = curl_exec($curl_handle);
366+
$result = curl_exec($curl_handle);
372367

373368
$statusCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
374369

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

378373
//provide a hint for 'Failed to connect to ...: Permission denied'
@@ -383,7 +378,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
383378
}
384379

385380
if ($statusCode > 299) {
386-
$error = @json_decode($res);
381+
$error = @json_decode($result);
387382
$imageHtml .= $this->translate('Cannot fetch Grafana graph')
388383
. ": "
389384
. Util::httpStatusCodeToString($statusCode)
@@ -395,7 +390,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
395390
}
396391

397392
curl_close($curl_handle);
398-
$imageHtml = $res;
393+
$imageHtml = $result;
399394
return true;
400395
}
401396

application/forms/Config/GeneralConfigForm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,15 @@ public function createElements(array $formData)
236236
if (isset($formData['grafana_accessmode']) && $formData['grafana_accessmode'] === 'indirectproxy') {
237237
$this->addElement(
238238
'select',
239-
'grafana_indirectproxyrefresh',
239+
'grafana_enablecache',
240240
[
241-
'label' => $this->translate('Refresh on indirect proxy'),
241+
'label' => $this->translate('Enable cache'),
242242
'value' => 'yes',
243243
'multiOptions' => [
244244
'yes' => $this->translate('Yes'),
245245
'no' => $this->translate('No'),
246246
],
247-
'description' => $this->translate('Refresh graphs on indirect proxy mode.')
247+
'description' => $this->translate('Enable cache on indirect proxy mode.')
248248
]
249249
);
250250
}

doc/03-module-configuration.md

Lines changed: 4 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,8 @@ 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`.
196195

197196
### usepublic
198197
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+
// Calculate the cachetime based on the check intervals
356+
$this->cacheTime = round($object->state->next_check->getTimestamp() - $object->state->last_update->getTimestamp());
357357

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

0 commit comments

Comments
 (0)