Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] support splitting single graph into multiple graphs via imagefilters #41

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions application/controllers/IcingadbimgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class IcingadbimgController extends IcingadbGrafanaController
protected $dashboarduid;
protected $panelId;
protected $orgId;
protected $imagefilter;

/**
* Mainly loading defaults and the configuration.
Expand Down Expand Up @@ -115,6 +116,12 @@ public function init()
$this->dataSource = $this->myConfig->get('datasource', $this->dataSource);
$this->shadows = $this->myConfig->get('shadows', $this->shadows);
$this->custvarconfig = ($this->myConfig->get('custvarconfig', $this->custvarconfig));

$this->imagefilter = $this->hasParam('imagefilter') ? urldecode($this->getParam('imagefilter')) : "";

/**
* Verify the certificate's name against host
*/
$this->SSLVerifyHost = ($this->myConfig->get('ssl_verifyhost', $this->SSLVerifyHost));
$this->SSLVerifyPeer = ($this->myConfig->get('ssl_verifypeer', $this->SSLVerifyPeer));

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

$pngUrl = sprintf(
'%s://%s/render/d-solo/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s'
. '&width=%s&height=%s&theme=%s&from=%s&to=%s',
. '&width=%s&height=%s&theme=%s&from=%s&to=%s%s',
$this->protocol,
$this->grafanaHost,
$this->dashboarduid,
Expand All @@ -293,7 +300,8 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
$this->height,
$this->grafanaTheme,
urlencode($this->timerange),
urlencode($this->timerangeto)
urlencode($this->timerangeto),
$this->imagefilter
);

// fetch image with curl
Expand Down
35 changes: 26 additions & 9 deletions library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private function getGraphConf($serviceName, $serviceCommand = null): ?self
* @return bool
* @throws \Icinga\Exception\ProgrammingError
*/
private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $previewHtml): bool
private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $previewHtml, $imageFilter = ""): bool
{
$imgClass = $this->shadows ? "grafana-img grafana-img-shadows" : "grafana-img";

Expand All @@ -249,7 +249,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
'panelid' => $this->panelId,
'timerange' => urlencode($this->timerange),
'timerangeto' => urlencode($this->timerangeto),
'cachetime' => $this->cacheTime
'cachetime' => $this->cacheTime,
'imagefilter' => urlencode($imageFilter)
]
);
} else {
Expand All @@ -260,7 +261,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
'panelid' => $this->panelId,
'timerange' => urlencode($this->timerange),
'timerangeto' => urlencode($this->timerangeto),
'cachetime' => $this->cacheTime
'cachetime' => $this->cacheTime,
'imagefilter' => urlencode($imageFilter)
]
);
}
Expand Down Expand Up @@ -458,14 +460,29 @@ public function getPreviewHtml(Model $object, $report = false)
$html = new HtmlDocument();
$this->panelId = $panelid;

// The image value will be returned as reference
$previewHtml = new HtmlDocument();
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml);

if ($res) {
$html->addHtml($previewHtml);
$flattened_vars = $object->vars;
// The $object->vars array is flattened, we unflatten the subarray grafanaimagefiltersarray here:
$i = 0;
$info = [];
while (isset($flattened_vars['grafanaimagefiltersarray[' . $i . ']'])) {
$info[$i] = $flattened_vars['grafanaimagefiltersarray[' . $i . ']'];
$i++;
}

$i = 0;
do {
$value = $info ? $info[$i] : "";

// The image value will be returned as reference
$previewHtml = new HtmlDocument();
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml, $value);

if ($res) {
$html->addHtml($previewHtml);
}
$i++;
} while ($i < count($info));

$returnHtml->add($html);
}

Expand Down