-
Notifications
You must be signed in to change notification settings - Fork 6
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
SLEEP-1499 Support embedding of Superset dashboard in Iaso #1698
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>{{title}}</title> | ||
{{ analytics_script | safe }} | ||
<script src="https://unpkg.com/@superset-ui/embedded-sdk"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, we should have this copied in our code base and not depending on an external source |
||
</head> | ||
|
||
<body> | ||
<div id="my-superset-container"></div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not very important, but why "my" ? |
||
|
||
<script> | ||
async function fetchGuestTokenFromBackend() { | ||
response = await fetch('/api/superset/token/', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
dashboard_id: '{{dashboard_id}}', | ||
}), | ||
}); | ||
const json_resp = await response.json(); | ||
return json_resp.token; | ||
} | ||
|
||
const containerRef = document.getElementById( | ||
'my-superset-container', | ||
); | ||
|
||
supersetEmbeddedSdk.embedDashboard({ | ||
id: '{{dashboard_id}}', | ||
supersetDomain: 'https://superset.trypelim.org', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Domain name seems hardcoded? |
||
mountPoint: containerRef, | ||
fetchGuestToken: () => fetchGuestTokenFromBackend(), | ||
dashboardUiConfig: { | ||
hideTitle: true, | ||
hideTab: true, | ||
hideChartControls: true, | ||
filters: { | ||
visible: false, | ||
expanded: false, | ||
}, | ||
}, | ||
}); | ||
|
||
const iframe = containerRef.querySelector('iframe'); | ||
if (iframe) { | ||
iframe.style.width = '100%'; | ||
iframe.style.height = '97vh'; | ||
iframe.style.border = '0'; | ||
} | ||
</script> | ||
|
||
{% include "iaso/pages/refresh_data_set_snippet.html" %} | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import requests | ||
|
||
from django.conf import settings | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework import status, viewsets | ||
from rest_framework.response import Response | ||
|
||
|
||
@swagger_auto_schema() | ||
class SupersetTokenViewSet(viewsets.ViewSet): | ||
""" | ||
POST /api/superset/token | ||
|
||
This endpoint creates a "guest token" to embed private Superset dashboards | ||
in an iframe in Iaso (typically via a "Page") | ||
|
||
See: | ||
https://www.npmjs.com/package/@superset-ui/embedded-sdk | ||
""" | ||
|
||
def create(self, request): | ||
dashboard_id = request.data.get("dashboard_id") | ||
|
||
base_url = settings.SUPERSET_URL | ||
headers = {"Content-Type": "application/json"} | ||
|
||
# Log in to Superset to get access_token | ||
payload = { | ||
"username": settings.SUPERSET_ADMIN_USERNAME, | ||
"password": settings.SUPERSET_ADMIN_PASSWORD, | ||
"provider": "db", | ||
"refresh": True, | ||
} | ||
response = requests.post(base_url + "/api/v1/security/login", json=payload, headers=headers) | ||
access_token = response.json()["access_token"] | ||
headers["Authorization"] = f"Bearer {access_token}" | ||
|
||
# Fetch CSRF token | ||
response = requests.get(base_url + "/api/v1/security/csrf_token/", headers=headers) | ||
headers["X-CSRF-TOKEN"] = response.json()["result"] | ||
headers["Cookie"] = response.headers.get("Set-Cookie") | ||
headers["Referer"] = base_url | ||
|
||
# Fetch Guest token | ||
current_user = request.user | ||
payload = { | ||
"user": { | ||
"username": current_user.username, | ||
"first_name": current_user.first_name, | ||
"last_name": current_user.last_name, | ||
}, | ||
"resources": [{"type": "dashboard", "id": dashboard_id}], | ||
"rls": [], | ||
} | ||
|
||
response = requests.post(base_url + "/api/v1/security/guest_token/", json=payload, headers=headers) | ||
guest_token = response.json()["token"] | ||
|
||
return Response({"token": guest_token}, status=status.HTTP_201_CREATED) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Generated by Django 4.2.14 on 2024-09-26 15:03 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("iaso", "0299_merge_0297_entity_merged_to_0298_profile_organization"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="page", | ||
name="type", | ||
field=models.CharField( | ||
choices=[ | ||
("RAW", "Raw html"), | ||
("TEXT", "Text"), | ||
("IFRAME", "Iframe"), | ||
("POWERBI", "PowerBI report"), | ||
("SUPERSET", "Superset dashboard"), | ||
], | ||
default="RAW", | ||
max_length=40, | ||
), | ||
), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that here?