Skip to content

Commit

Permalink
Added metabase dashboard embed API template
Browse files Browse the repository at this point in the history
  • Loading branch information
MIKEINTOSHSYSTEMS committed Aug 24, 2024
1 parent 38a0300 commit af452db
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 206 deletions.
16 changes: 0 additions & 16 deletions analytics/meta/embed.php

This file was deleted.

188 changes: 0 additions & 188 deletions analytics/meta/metabase.php

This file was deleted.

44 changes: 44 additions & 0 deletions analytics/tests/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TESTING Metabase Dashboard TEMPLATE</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}

iframe {
border: none;
width: 100%;
height: 100vh;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>

<body>
<iframe id="metabase-iframe" src="" allowtransparency></iframe>
<script>
// Set dashboard ID dynamically
var dashboardId = 1; // Replace with dynamic ID if needed

// Make a request to the PHP server to generate the iframe URL for the selected dashboard ID
fetch('metabase.php?dashboardId=' + dashboardId)
.then(response => response.text())
.then(iframeUrl => {
document.getElementById('metabase-iframe').src = iframeUrl;
});
</script>
</body>
<!--If any Questions Please Contact https://github.com/MIKEINTOSHSYSTEMS -->

</html>
File renamed without changes.
7 changes: 5 additions & 2 deletions analytics/tests/index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<html>


<!--
<iframe
src="https://viz.hispmd.merqconsultancy.org/public/dashboard/d918846e-4cd4-4909-837e-73c99e8f85d8"
frameborder="0"
width="100%"
height="100%"
allowtransparency
></iframe>
</html>
-->
<h1>Analytics Testing</h1>
</html>
46 changes: 46 additions & 0 deletions analytics/tests/metabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
require '../../vendor/autoload.php';

use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;

// Metabase parameters
$metabaseSiteUrl = 'https://viz.hispmd.merqconsultancy.org'; //replace with the right metabase url
$metabaseSecretKey = '25893c16e94ac3c193ad2bfa3b2dbdba5ea0e663de266dc7649ffc9216e09865'; // Replace with your Metabase secret key

// Get the dashboard ID from the query parameter
$dashboardId = isset($_GET['dashboardId']) ? (int)$_GET['dashboardId'] : 0;

if ($dashboardId <= 0) {
die('Invalid dashboard ID.');
}

// Create JWT configuration with HMAC SHA-256 signer
$config = Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText($metabaseSecretKey)
);

// Create the token
$now = new DateTimeImmutable();
$exp = $now->add(new DateInterval('PT10M')); // Token expires in 10 minutes

// You can add parameters if needed for filtering or other purposes
$params = (object)[];

$token = $config->builder()
->issuedBy($metabaseSiteUrl) // Configures the issuer (iss claim)
->issuedAt($now) // Configures the time that the token was issued (iat claim)
->expiresAt($exp) // Configures the expiration time of the token (exp claim)
->withClaim('resource', ['dashboard' => $dashboardId]) // Add resource claim
->withClaim('params', $params) // Add params claim as an object
->getToken($config->signer(), $config->signingKey()); // Retrieves the generated token

// Generate iframe URL
$iframeUrl = $metabaseSiteUrl . "/embed/dashboard/" . $token->toString() . "#theme=transparent&bordered=false&titled=true";

// Return the iframe URL
echo htmlspecialchars($iframeUrl, ENT_QUOTES, 'UTF-8');
// If any Questions Please Contact https://github.com/MIKEINTOSHSYSTEMS
?>
46 changes: 46 additions & 0 deletions api/meta/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
require '../../vendor/autoload.php';

use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;

// Metabase parameters
$metabaseSiteUrl = 'https://viz.hispmd.merqconsultancy.org'; //replace with the right metabase url
$metabaseSecretKey = '25893c16e94ac3c193ad2bfa3b2dbdba5ea0e663de266dc7649ffc9216e09865'; // Replace with your Metabase secret key

// Get the dashboard ID from the query parameter
$dashboardId = isset($_GET['dashboardId']) ? (int)$_GET['dashboardId'] : 0;

if ($dashboardId <= 0) {
die('Invalid dashboard ID.');
}

// Create JWT configuration with HMAC SHA-256 signer
$config = Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText($metabaseSecretKey)
);

// Create the token
$now = new DateTimeImmutable();
$exp = $now->add(new DateInterval('PT10M')); // Token expires in 10 minutes

// You can add parameters if needed for filtering or other purposes
$params = (object)[];

$token = $config->builder()
->issuedBy($metabaseSiteUrl) // Configures the issuer (iss claim)
->issuedAt($now) // Configures the time that the token was issued (iat claim)
->expiresAt($exp) // Configures the expiration time of the token (exp claim)
->withClaim('resource', ['dashboard' => $dashboardId]) // Add resource claim
->withClaim('params', $params) // Add params claim as an object
->getToken($config->signer(), $config->signingKey()); // Retrieves the generated token

// Generate iframe URL
$iframeUrl = $metabaseSiteUrl . "/embed/dashboard/" . $token->toString() . "#theme=transparent&bordered=false&titled=true";

// Return the iframe URL
echo htmlspecialchars($iframeUrl, ENT_QUOTES, 'UTF-8');
// If any Questions Please Contact https://github.com/MIKEINTOSHSYSTEMS
?>
Loading

0 comments on commit af452db

Please sign in to comment.