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

Check dashboard server side #159

Merged
merged 6 commits into from
Dec 15, 2020
Merged
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
6 changes: 5 additions & 1 deletion dask_labextension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import config
from .clusterhandler import DaskClusterHandler
from .dashboardhandler import DaskDashboardHandler
from .dashboardhandler import DaskDashboardCheckHandler, DaskDashboardHandler


from ._version import get_versions
Expand Down Expand Up @@ -32,9 +32,13 @@ def load_jupyter_server_extension(nb_server_app):
get_dashboard_path = url_path_join(
base_url, f"dask/dashboard/{cluster_id_regex}(?P<proxied_path>.+)"
)
check_dashboard_path = url_path_join(
base_url, "dask/dashboard-check/(?P<url>.+)"
)
handlers = [
(get_cluster_path, DaskClusterHandler),
(list_clusters_path, DaskClusterHandler),
(get_dashboard_path, DaskDashboardHandler),
(check_dashboard_path, DaskDashboardCheckHandler),
]
web_app.add_handlers(".*$", handlers)
2 changes: 1 addition & 1 deletion dask_labextension/clusterhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def delete(self, cluster_id: str) -> None:
raise web.HTTPError(500, str(e))

@web.authenticated
def get(self, cluster_id: str = "") -> None:
async def get(self, cluster_id: str = "") -> None:
"""
Get a cluster by id. If no id is given, lists known clusters.
"""
Expand Down
54 changes: 52 additions & 2 deletions dask_labextension/dashboardhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,66 @@
This proxies the bokeh server http and ws requests through the notebook
server, preventing CORS issues.
"""
import json
from urllib import parse

from tornado import web
from tornado import httpclient, web

from notebook.base.handlers import APIHandler
from notebook.utils import url_path_join
from jupyter_server_proxy.handlers import ProxyHandler

from .manager import manager


class DaskDashboardCheckHandler(APIHandler):
"""
A handler for checking validity of a dask dashboard.
"""

@web.authenticated
async def get(self, url) -> None:
"""
Test if a given url string hosts a dask dashboard. Should always return a
200 code, any errors are presumed to result from an invalid/inactive dashboard.
"""
try:
client = httpclient.AsyncHTTPClient()

# Check the user-provided url, following any redirects.
url = _normalize_dashboard_link(parse.unquote(url), self.request)
response = await client.fetch(url)
effective_url = response.effective_url if response.effective_url != url else None

# Fetch the individual plots
individual_plots_response = await client.fetch(
url_path_join(
_normalize_dashboard_link(effective_url or url, self.request),
"individual-plots.json"
)
)
# If we didn't get individual plots, it may not be a dask dashboard
if individual_plots_response.code != 200:
raise ValueError("Does not seem to host a dask dashboard")
individual_plots = json.loads(individual_plots_response.body)

self.set_status(200)
self.finish(json.dumps({
"url": url,
"isActive": response.code == 200,
"effectiveUrl": effective_url,
"plots": individual_plots,
}))
except:
self.log.warn(f"{url} does not seem to host a dask dashboard")
self.set_status(200)
self.finish(json.dumps({
"url": url,
"isActive": False,
"plots": {},
}))


class DaskDashboardHandler(ProxyHandler):
"""
A handler that proxies the dask dashboard to the notebook server.
Expand Down Expand Up @@ -92,6 +142,6 @@ def _normalize_dashboard_link(link, request):
# as the application, and prepend that.
link = url_path_join(f"{request.protocol}://{request.host}", link)
if link.endswith("/status"):
# If the default "status" dashboard is give, strip it.
# If the default "status" dashboard is given, strip it.
link = link[:-len("/status")]
return link
Loading