aminghadersohi commented on code in PR #44797:
URL: https://github.com/apache/superset/pull/44797#discussion_r4161011809
##########
superset/mcp_service/dashboard/schemas.py:
##########
@@ -371,6 +389,8 @@ def _require_identifier_or_permalink(self) ->
"GetDashboardLayoutRequest":
)
if identifier_is_blank and permalink_is_blank:
raise ValueError("Provide identifier or permalink_key")
+ if self.untabbed_only and (self.tabs_only or self.tab is not None):
+ raise ValueError("untabbed_only cannot be combined with tab or
tabs_only")
Review Comment:
Fixed in 6d14e75dd12c6e6446047bb12f28384e0f9e2c33: scope exclusivity is
checked in `_scope_layout` and blank selectors in `_resolve_tab`, returning
actionable `DashboardError` payloads rather than generic Pydantic validation
text. Client-level regression tests cover both cases.
##########
superset/mcp_service/dashboard/tool/get_dashboard_layout.py:
##########
@@ -39,13 +39,134 @@
dashboard_layout_serializer,
DashboardError,
DashboardLayout,
+ DashboardLayoutScope,
+ DashboardTab,
+ DashboardTabSummary,
GetDashboardLayoutRequest,
)
from superset.mcp_service.mcp_core import ModelGetInfoCore
logger = logging.getLogger(__name__)
+def _resolve_tab(
+ tabs: list[DashboardTab], selector: str
+) -> DashboardTab | DashboardError:
+ """Match a tab by ID first, then by exact title."""
+ if not tabs:
+ return DashboardError.create(
+ "This dashboard has no tabs. Omit tab to get the full layout.",
+ "tab_not_found",
+ )
+ matches = [tab for tab in tabs if tab.id == selector] or [
+ tab for tab in tabs if tab.name == selector
+ ]
+ if not matches:
+ return DashboardError.create(
+ "Tab not found. Use tabs_only=true to discover tab IDs and
titles.",
+ "tab_not_found",
+ )
+ if len(matches) > 1:
+ return DashboardError.create(
+ "Multiple tabs have that title. Use tabs_only=true to discover "
+ "their IDs, then pass a unique tab ID.",
+ "ambiguous_tab",
+ )
+ return matches[0]
+
+
+def _subtree_ids(tab_id: str, children: dict[str | None, list[str]]) ->
set[str]:
+ """Return a tab ID and the IDs of every tab nested under it."""
+ selected_ids: set[str] = set()
+ pending = [tab_id]
+ while pending:
+ current = pending.pop()
+ if current not in selected_ids:
+ selected_ids.add(current)
+ pending.extend(children.get(current, []))
+ return selected_ids
+
+
+def _tab_summaries(
+ tabs: list[DashboardTab], children: dict[str | None, list[str]]
+) -> list[DashboardTabSummary]:
+ """Summarize tabs with absolute depths from the full dashboard tree."""
+ # The parser only emits reachable tabs and assigns each tab a single
+ # enclosing parent, so walking from the top-level tabs reaches every tab.
+ depths: dict[str, int] = {}
+ stack = [(tab_id, 0) for tab_id in children.get(None, [])]
+ while stack:
+ tab_id, depth = stack.pop()
+ depths[tab_id] = depth
+ stack.extend((child, depth + 1) for child in children.get(tab_id, []))
+ return [
+ DashboardTabSummary(
+ id=tab.id,
+ name=tab.name,
+ parent_tab_id=tab.parent_tab_id,
+ depth=depths[tab.id],
+ chart_count=len(tab.chart_ids),
+ )
+ for tab in tabs
+ ]
+
+
+def _scope_layout(
+ layout: DashboardLayout, request: GetDashboardLayoutRequest
+) -> DashboardLayout | DashboardError:
+ """Project the parsed layout without changing permalink or ancestry
context.
+
+ Scoping only ever removes tabs and chart placements from the parsed layout,
+ so a scoped response never contains a chart the full layout would not.
+ """
+ if not request.tabs_only and request.tab is None and not
request.untabbed_only:
+ return layout
+
+ if request.untabbed_only:
+ return layout.model_copy(
+ update={
+ "tabs": [],
+ "charts": [chart for chart in layout.charts if chart.tab_id is
None],
+ "scope": DashboardLayoutScope(untabbed_only=True),
+ }
+ )
+
+ tabs = layout.tabs
+ children: dict[str | None, list[str]] = {}
+ for tab in tabs:
+ children.setdefault(tab.parent_tab_id, []).append(tab.id)
+
+ selected_tab_id: str | None = None
+ if request.tab is not None:
+ selected = _resolve_tab(tabs, request.tab)
+ if isinstance(selected, DashboardError):
+ return selected
+ selected_tab_id = selected.id
+ selected_ids = _subtree_ids(selected.id, children)
+ tabs = [tab for tab in tabs if tab.id in selected_ids]
+
+ scope = DashboardLayoutScope(tabs_only=request.tabs_only,
tab_id=selected_tab_id)
+ if request.tabs_only:
Review Comment:
Fixed in 6d14e75dd12c6e6446047bb12f28384e0f9e2c33: `tabs_only=true` on a
dashboard without tabs returns `tab_not_found` with guidance to use
`untabbed_only=true` or request the full layout. Client tests cover flat,
missing, and malformed layouts.
##########
superset/mcp_service/dashboard/tool/get_dashboard_layout.py:
##########
@@ -39,13 +39,134 @@
dashboard_layout_serializer,
DashboardError,
DashboardLayout,
+ DashboardLayoutScope,
+ DashboardTab,
+ DashboardTabSummary,
GetDashboardLayoutRequest,
)
from superset.mcp_service.mcp_core import ModelGetInfoCore
logger = logging.getLogger(__name__)
+def _resolve_tab(
+ tabs: list[DashboardTab], selector: str
+) -> DashboardTab | DashboardError:
+ """Match a tab by ID first, then by exact title."""
+ if not tabs:
+ return DashboardError.create(
+ "This dashboard has no tabs. Omit tab to get the full layout.",
+ "tab_not_found",
+ )
+ matches = [tab for tab in tabs if tab.id == selector] or [
+ tab for tab in tabs if tab.name == selector
+ ]
+ if not matches:
+ return DashboardError.create(
+ "Tab not found. Use tabs_only=true to discover tab IDs and
titles.",
+ "tab_not_found",
+ )
+ if len(matches) > 1:
+ return DashboardError.create(
+ "Multiple tabs have that title. Use tabs_only=true to discover "
+ "their IDs, then pass a unique tab ID.",
+ "ambiguous_tab",
+ )
+ return matches[0]
+
+
+def _subtree_ids(tab_id: str, children: dict[str | None, list[str]]) ->
set[str]:
+ """Return a tab ID and the IDs of every tab nested under it."""
+ selected_ids: set[str] = set()
+ pending = [tab_id]
+ while pending:
+ current = pending.pop()
+ if current not in selected_ids:
+ selected_ids.add(current)
+ pending.extend(children.get(current, []))
+ return selected_ids
+
+
+def _tab_summaries(
+ tabs: list[DashboardTab], children: dict[str | None, list[str]]
+) -> list[DashboardTabSummary]:
+ """Summarize tabs with absolute depths from the full dashboard tree."""
+ # The parser only emits reachable tabs and assigns each tab a single
+ # enclosing parent, so walking from the top-level tabs reaches every tab.
+ depths: dict[str, int] = {}
+ stack = [(tab_id, 0) for tab_id in children.get(None, [])]
+ while stack:
+ tab_id, depth = stack.pop()
+ depths[tab_id] = depth
+ stack.extend((child, depth + 1) for child in children.get(tab_id, []))
+ return [
+ DashboardTabSummary(
+ id=tab.id,
+ name=tab.name,
+ parent_tab_id=tab.parent_tab_id,
+ depth=depths[tab.id],
+ chart_count=len(tab.chart_ids),
+ )
+ for tab in tabs
+ ]
+
+
+def _scope_layout(
+ layout: DashboardLayout, request: GetDashboardLayoutRequest
+) -> DashboardLayout | DashboardError:
+ """Project the parsed layout without changing permalink or ancestry
context.
+
+ Scoping only ever removes tabs and chart placements from the parsed layout,
+ so a scoped response never contains a chart the full layout would not.
+ """
+ if not request.tabs_only and request.tab is None and not
request.untabbed_only:
+ return layout
+
+ if request.untabbed_only:
+ return layout.model_copy(
+ update={
+ "tabs": [],
+ "charts": [chart for chart in layout.charts if chart.tab_id is
None],
+ "scope": DashboardLayoutScope(untabbed_only=True),
+ }
+ )
+
+ tabs = layout.tabs
+ children: dict[str | None, list[str]] = {}
+ for tab in tabs:
+ children.setdefault(tab.parent_tab_id, []).append(tab.id)
+
+ selected_tab_id: str | None = None
+ if request.tab is not None:
+ selected = _resolve_tab(tabs, request.tab)
+ if isinstance(selected, DashboardError):
+ return selected
+ selected_tab_id = selected.id
+ selected_ids = _subtree_ids(selected.id, children)
+ tabs = [tab for tab in tabs if tab.id in selected_ids]
+
+ scope = DashboardLayoutScope(tabs_only=request.tabs_only,
tab_id=selected_tab_id)
+ if request.tabs_only:
+ return layout.model_copy(
+ update={
+ "tabs": [],
+ "tab_tree": _tab_summaries(tabs, children),
+ "charts": [],
+ "scope": scope,
+ }
+ )
+
+ return layout.model_copy(
+ update={
+ "tabs": tabs,
+ "charts": [
+ chart for chart in layout.charts if chart.tab_id in
selected_ids
Review Comment:
Fixed in 6d14e75dd12c6e6446047bb12f28384e0f9e2c33: `selected_ids` is derived
unconditionally from the final tab list immediately before the chart
projection, while subtree filtering uses a separate `subtree_ids` variable.
This removes the implicit binding dependency without reintroducing the dead
initializer from the resolved earlier thread.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]