bito-code-review[bot] commented on code in PR #44797:
URL: https://github.com/apache/superset/pull/44797#discussion_r4139228733


##########
superset/mcp_service/dashboard/tool/get_dashboard_layout.py:
##########
@@ -39,13 +39,135 @@
     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
+    selected_ids = {tab.id for tab in tabs}

Review Comment:
   <!-- Bito Reply -->
   The removal of the `selected_ids` initializer is correct, as the variable 
was unused in the execution paths where it was previously defined. Your 
assessment that the DFS traversal in `_extract_layout_from_position` and the 
subsequent tree parsing in `_tab_summaries` prevent the cycle and missing-depth 
issues suggested by the reviewer is accurate. The logic remains robust for the 
current call path, and the changes are safe to proceed with.



-- 
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]

Reply via email to