Skip to content

Commit

Permalink
Add external link to Grafana in iFrame mode
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Dec 4, 2024
1 parent 463199c commit 2a753dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 16 additions & 6 deletions library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
use ipl\Stdlib\Filter;
use ipl\Web\Url;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\Icon;

trait IcingaDbGrapher
{
use Database;
use IcingaDbAuth;

// Could be constants in the future, but for now we want to keep compatibility
protected $GRAFANA_URL = "%s://%s/d/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&from=%s&to=%s&orgId=%s&viewPanel=%s";
protected $GRAFANA_URL_SOLO = "%s://%s/d-solo/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s&theme=%s&from=%s&to=%s";

protected $config;
protected $graphConfig;
protected $auth;
Expand Down Expand Up @@ -310,8 +315,7 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
$previewHtml->add($imgHtml);
} elseif ($this->accessMode === "iframe") {
$iFramesrc = sprintf(
"%s://%s/d-solo/%s/%s?" .
"var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s&theme=%s&from=%s&to=%s",
$this->GRAFANA_URL_SOLO,
$this->protocol,
$this->grafanaHost,
$this->dashboarduid,
Expand All @@ -338,6 +342,15 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
);

$previewHtml->add($iframeHtml);
// Add a link to Grafana in the title
$this->title->add(new Link(
new Icon(
'arrow-up-right-from-square',
['title' => 'View in Grafana']
),
str_replace('/d-solo/', '/d/', $iFramesrc),
['target' => '_blank', 'class' => 'external-link']
));
}
return true;
}
Expand Down Expand Up @@ -467,11 +480,8 @@ public function getPreviewHtml(Model $object, $report = false)
|| ! $this->permission->hasPermission('grafana/enablelink')) {
$html->addHtml($previewHtml);
} else {
$urlFormat = "%s://%s/d/%s/%s" .
"?var-hostname=%s&var-service=%s&var-command=%s%s&from=%s&to=%s&orgId=%s&viewPanel=%s";

$url = sprintf(
$urlFormat,
$this->GRAFANA_URL,
$this->publicProtocol,
$this->publicHost,
$this->dashboarduid,
Expand Down
6 changes: 6 additions & 0 deletions public/css/module.less
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
padding: 0;
}

/* Icon to external pages like Grafana */
.external-link {
margin: 0 0.33em;
font-size: 0.77em;
}

/* NAVIGATION */
.grafana-menu-navigation {
list-style: none;
Expand Down

0 comments on commit 2a753dd

Please sign in to comment.