Skip to content

Commit 3c01a4b

Browse files
committed
support splitting into multiple graphs via imagefilters
1 parent 677fada commit 3c01a4b

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

application/controllers/IcingadbimgController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class IcingadbimgController extends IcingadbGrafanaController
4949
protected $dashboarduid;
5050
protected $panelId;
5151
protected $orgId;
52+
protected $imagefilter;
5253

5354
/**
5455
* Mainly loading defaults and the configuration.
@@ -115,6 +116,12 @@ public function init()
115116
$this->dataSource = $this->myConfig->get('datasource', $this->dataSource);
116117
$this->shadows = $this->myConfig->get('shadows', $this->shadows);
117118
$this->custvarconfig = ($this->myConfig->get('custvarconfig', $this->custvarconfig));
119+
120+
$this->imagefilter = $this->hasParam('imagefilter') ? urldecode($this->getParam('imagefilter')) : "";
121+
122+
/**
123+
* Verify the certificate's name against host
124+
*/
118125
$this->SSLVerifyHost = ($this->myConfig->get('ssl_verifyhost', $this->SSLVerifyHost));
119126
$this->SSLVerifyPeer = ($this->myConfig->get('ssl_verifypeer', $this->SSLVerifyPeer));
120127

@@ -278,7 +285,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
278285

279286
$pngUrl = sprintf(
280287
'%s://%s/render/d-solo/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s'
281-
. '&width=%s&height=%s&theme=%s&from=%s&to=%s',
288+
. '&width=%s&height=%s&theme=%s&from=%s&to=%s%s',
282289
$this->protocol,
283290
$this->grafanaHost,
284291
$this->dashboarduid,
@@ -293,7 +300,8 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
293300
$this->height,
294301
$this->grafanaTheme,
295302
urlencode($this->timerange),
296-
urlencode($this->timerangeto)
303+
urlencode($this->timerangeto),
304+
$this->imagefilter
297305
);
298306

299307
// fetch image with curl

library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private function getGraphConf($serviceName, $serviceCommand = null): ?self
235235
* @return bool
236236
* @throws \Icinga\Exception\ProgrammingError
237237
*/
238-
private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $previewHtml): bool
238+
private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $previewHtml, $imageFilter = ""): bool
239239
{
240240
$imgClass = $this->shadows ? "grafana-img grafana-img-shadows" : "grafana-img";
241241

@@ -249,7 +249,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
249249
'panelid' => $this->panelId,
250250
'timerange' => urlencode($this->timerange),
251251
'timerangeto' => urlencode($this->timerangeto),
252-
'cachetime' => $this->cacheTime
252+
'cachetime' => $this->cacheTime,
253+
'imagefilter' => urlencode($imageFilter)
253254
]
254255
);
255256
} else {
@@ -260,7 +261,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
260261
'panelid' => $this->panelId,
261262
'timerange' => urlencode($this->timerange),
262263
'timerangeto' => urlencode($this->timerangeto),
263-
'cachetime' => $this->cacheTime
264+
'cachetime' => $this->cacheTime,
265+
'imagefilter' => urlencode($imageFilter)
264266
]
265267
);
266268
}
@@ -458,12 +460,24 @@ public function getPreviewHtml(Model $object, $report = false)
458460
$html = new HtmlDocument();
459461
$this->panelId = $panelid;
460462

461-
// The image value will be returned as reference
462-
$previewHtml = new HtmlDocument();
463-
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml);
463+
$flattened_vars = $object->vars;
464+
// The $object->vars array is flattened, we unflatten the subarray grafanaimagefiltersarray here:
465+
$i = 0;
466+
$info = [];
467+
while (isset($flattened_vars['grafanaimagefiltersarray[' . $i . ']'])) {
468+
$info[$i] = $flattened_vars['grafanaimagefiltersarray[' . $i . ']'];
469+
$i++;
470+
}
471+
472+
foreach ($info as $value) {
464473

465-
if ($res) {
466-
$html->addHtml($previewHtml);
474+
// The image value will be returned as reference
475+
$previewHtml = new HtmlDocument();
476+
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml, $value);
477+
478+
if ($res) {
479+
$html->addHtml($previewHtml);
480+
}
467481
}
468482

469483
$returnHtml->add($html);

0 commit comments

Comments
 (0)