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

Add deploy-dashboard command #301

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

*.out
*.out
/.databricks-login.json
18 changes: 18 additions & 0 deletions labs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ commands:
description: Publish the dashboard after creating by setting to `yes` or `y`.
- name: open-browser
description: Open the dashboard in the browser after creating by setting to `yes` or `y`.

- name: deploy-dashboard
description: Create or update dashboard from code.
flags:
- name: folder
description: The folder with dashboard files. By default, the current working directory.
- name: catalog
description: |
Overwrite the catalog in the queries' `FROM` clauses with given value.
Useful when developing with separate catalogs, for example, for production and development.
- name: database
description: |
Overwrite the database in the queries' `FROM` clauses with given value.
Useful when developing with separate databases, for example, for production and development.
- name: publish
description: Publish the dashboard after creating by setting to `yes` or `y`.
- name: open-browser
description: Open the dashboard in the browser after creating by setting to `yes` or `y`.
15 changes: 15 additions & 0 deletions src/databricks/labs/lsql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def create_dashboard(
catalog=catalog or None,
database=database or None,
)
# TODO: rename this method and command to `deploy_dashboard`
sdk_dashboard = lakeview_dashboards.create_dashboard(dashboard_metadata, publish=should_publish)
if should_open_browser:
assert sdk_dashboard.dashboard_id is not None
Expand All @@ -43,6 +44,20 @@ def create_dashboard(
print(sdk_dashboard.dashboard_id)


@lsql.command
def deploy_dashboard(
w: WorkspaceClient,
folder: Path = Path.cwd(),
*,
catalog: str = "",
database: str = "",
publish: str = "false",
open_browser: str = "false",
):
"""Create a dashboard from queries"""
create_dashboard(w, folder, catalog=catalog, database=database, publish=publish, open_browser=open_browser)


@lsql.command(is_unauthenticated=True)
def fmt(folder: Path = Path.cwd(), *, normalize_case: str = "true", exclude: Iterable[str] = ()):
"""Format SQL files in a folder"""
Expand Down
32 changes: 32 additions & 0 deletions src/databricks/labs/lsql/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sqlglot
import yaml
from databricks.sdk import WorkspaceClient
from databricks.sdk.errors import NotFound
from databricks.sdk.service.dashboards import Dashboard as SDKDashboard
from databricks.sdk.service.workspace import ExportFormat

Expand Down Expand Up @@ -1108,6 +1109,24 @@ def create_dashboard(
dashboard_id: str | None = None,
warehouse_id: str | None = None,
publish: bool = False,
) -> SDKDashboard:
# TODO: remove this method once downstreams are updated
return self.deploy_dashboard(
dashboard_metadata,
parent_path=parent_path,
dashboard_id=dashboard_id,
warehouse_id=warehouse_id,
publish=publish,
)

def deploy_dashboard(
self,
dashboard_metadata: DashboardMetadata,
*,
parent_path: str | None = None,
dashboard_id: str | None = None,
warehouse_id: str | None = None,
publish: bool = False,
) -> SDKDashboard:
"""Create a Lakeview dashboard.

Expand All @@ -1125,6 +1144,11 @@ def create_dashboard(
"""
dashboard_metadata.validate()
serialized_dashboard = json.dumps(dashboard_metadata.as_lakeview().as_dict())
me = self._ws.current_user.me()
if not parent_path:
parent_path = f"/Users/{me.user_name}"
if not dashboard_id:
dashboard_id = self._maybe_discover_dashboard_id(dashboard_metadata, parent_path)
if dashboard_id is not None:
sdk_dashboard = self._ws.lakeview.update(
dashboard_id,
Expand All @@ -1144,6 +1168,14 @@ def create_dashboard(
self._ws.lakeview.publish(sdk_dashboard.dashboard_id, warehouse_id=warehouse_id)
return sdk_dashboard

def _maybe_discover_dashboard_id(self, dashboard_metadata: DashboardMetadata, parent_path: str) -> str | None:
try:
file_path = f"{parent_path}/{dashboard_metadata.display_name}.lvdash.json"
workspace_object = self._ws.workspace.get_status(file_path)
return workspace_object.resource_id
except NotFound:
return None

def _with_better_names(self, dashboard: Dashboard) -> Dashboard:
"""Replace names with human-readable names."""
better_names = {}
Expand Down
Loading