Skip to content

Commit

Permalink
Add publish flag to Dashboards.create_dashboard (#233)
Browse files Browse the repository at this point in the history
Resolves #219
  • Loading branch information
JCZuurmond authored Jul 23, 2024
1 parent 9254731 commit 5971fb5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions labs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ commands:
description: |
Overwrite the database in the queries' `FROM` clauses with given value.
Useful when developing with seperated databases, for example, for production and development.
- name: publish
description: Publish the dashboard after creating
- name: no-open
description: Do not open the dashboard in the browser after creating
3 changes: 2 additions & 1 deletion src/databricks/labs/lsql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def create_dashboard(
*,
catalog: str = "",
database: str = "",
publish: bool = False,
no_open: bool = False,
):
"""Create a dashboard from queries"""
Expand All @@ -28,7 +29,7 @@ def create_dashboard(
catalog=catalog or None,
database=database or None,
)
sdk_dashboard = lakeview_dashboards.create_dashboard(dashboard_metadata)
sdk_dashboard = lakeview_dashboards.create_dashboard(dashboard_metadata, publish=publish)
if not no_open:
assert sdk_dashboard.dashboard_id is not None
dashboard_url = lakeview_dashboards.get_url(sdk_dashboard.dashboard_id)
Expand Down
6 changes: 6 additions & 0 deletions src/databricks/labs/lsql/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ def create_dashboard(
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 @@ -913,6 +914,8 @@ def create_dashboard(
The id of the dashboard to update
warehouse_id : str | None (default: None)
The id of the warehouse to use
publish : bool (default: False)
If True, publish the dashboard. If False, save it as a draft.
"""
dashboard_metadata.validate()
serialized_dashboard = json.dumps(dashboard_metadata.as_lakeview().as_dict())
Expand All @@ -930,6 +933,9 @@ def create_dashboard(
serialized_dashboard=serialized_dashboard,
warehouse_id=warehouse_id,
)
if publish:
assert sdk_dashboard.dashboard_id is not None
self._ws.lakeview.publish(sdk_dashboard.dashboard_id, warehouse_id=warehouse_id)
return sdk_dashboard

def deploy_dashboard(self, dashboard: Dashboard, **kwargs) -> SDKDashboard:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sqlglot
import yaml
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.dashboards import Dashboard as SDKDashboard

from databricks.labs.lsql.dashboards import (
BaseHandler,
Expand Down Expand Up @@ -1246,6 +1247,7 @@ def test_dashboards_calls_create_without_dashboard_id():
warehouse_id="warehouse",
)
ws.lakeview.update.assert_not_called()
ws.lakeview.publish.assert_not_called()


def test_dashboards_calls_update_with_dashboard_id():
Expand All @@ -1262,6 +1264,18 @@ def test_dashboards_calls_update_with_dashboard_id():
serialized_dashboard=json.dumps({"pages": [{"displayName": "test", "name": "test"}]}),
warehouse_id="warehouse",
)
ws.lakeview.publish.assert_not_called()


def test_dashboards_calls_publish_with_dashboard_id():
ws = create_autospec(WorkspaceClient)
ws.lakeview.create.return_value = SDKDashboard(dashboard_id="id")
dashboards = Dashboards(ws)
dashboard_metadata = DashboardMetadata("test")

dashboards.create_dashboard(dashboard_metadata, warehouse_id="warehouse", publish=True)

ws.lakeview.publish.assert_called_with("id", warehouse_id="warehouse")


def test_dashboard_raises_value_error_when_creating_dashboard_with_invalid_queries(tmp_path):
Expand Down

0 comments on commit 5971fb5

Please sign in to comment.