codeant-ai-for-open-source[bot] commented on code in PR #39922: URL: https://github.com/apache/superset/pull/39922#discussion_r3398598417
########## superset/mcp_service/chart/plugins/mixed_timeseries.py: ########## @@ -0,0 +1,170 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Mixed timeseries chart type plugin.""" + +from __future__ import annotations + +from typing import Any + +from superset.mcp_service.chart.chart_utils import ( + _mixed_timeseries_what, + _summarize_filters, + map_mixed_timeseries_config, +) +from superset.mcp_service.chart.plugin import BaseChartPlugin +from superset.mcp_service.chart.schemas import ColumnRef, MixedTimeseriesChartConfig +from superset.mcp_service.chart.validation.dataset_validator import DatasetValidator +from superset.mcp_service.common.error_schemas import ChartGenerationError + + +class MixedTimeseriesChartPlugin(BaseChartPlugin): + """Plugin for mixed_timeseries chart type.""" + + chart_type = "mixed_timeseries" + display_name = "Mixed Timeseries" + native_viz_types = { + "mixed_timeseries": "Mixed Timeseries Chart", + } + + def pre_validate( + self, + config: dict[str, Any], + ) -> ChartGenerationError | None: + missing_fields = [] + + if "x" not in config and "x_axis" not in config: + missing_fields.append("'x' (X-axis temporal column)") + if not config.get("y") and not config.get("metrics"): + missing_fields.append("'y' (primary Y-axis metrics)") + if not config.get("y_secondary") and not config.get("metrics_b"): + missing_fields.append("'y_secondary' (secondary Y-axis metrics)") + + if missing_fields: + return ChartGenerationError( + error_type="missing_mixed_timeseries_fields", + message=( + f"Mixed timeseries chart missing required fields: " + f"{', '.join(missing_fields)}" + ), + details=( + "Mixed timeseries charts require an x-axis, primary metrics, " + "and secondary metrics" + ), + suggestions=[ + "Add 'x' field: {'name': 'date_column'}", + "Add 'y' field: [{'name': 'revenue', 'aggregate': 'SUM'}]", + "Add 'y_secondary': [{'name': 'orders', 'aggregate': 'COUNT'}]", + "Optional: 'primary_kind' and 'secondary_kind' for chart types", + ], + error_code="MISSING_MIXED_TIMESERIES_FIELDS", + ) + + for field_name in ["y", "y_secondary"]: + if not isinstance(config.get(field_name, []), list): + return ChartGenerationError( + error_type=f"invalid_{field_name}_format", + message=f"'{field_name}' must be a list of metrics", + details=( + f"The '{field_name}' field must be an array of metric " + "specifications" + ), + suggestions=[ + f"Wrap in array: '{field_name}': " + "[{'name': 'col', 'aggregate': 'SUM'}]", + ], + error_code=f"INVALID_{field_name.upper()}_FORMAT", + ) + + return None + + def extract_column_refs(self, config: Any) -> list[ColumnRef]: + if not isinstance(config, MixedTimeseriesChartConfig): + return [] + refs: list[ColumnRef] = [config.x] + refs.extend(config.y) + refs.extend(config.y_secondary) + if config.group_by: + refs.extend(config.group_by) + if config.group_by_secondary: + refs.extend(config.group_by_secondary) + if config.filters: + for f in config.filters: + refs.append(ColumnRef(name=f.column)) + return refs + + def to_form_data( + self, config: Any, dataset_id: int | str | None = None + ) -> dict[str, Any]: + return map_mixed_timeseries_config(config, dataset_id=dataset_id) + + def generate_name(self, config: Any, dataset_name: str | None = None) -> str: Review Comment: **Suggestion:** Add a short docstring describing how the chart title is generated and how filter context is incorporated. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The method is newly added and lacks a docstring, so it violates the inline documentation requirement for new functions. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=a69d0d1a76dd40e0a1d46707560cc1bb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=a69d0d1a76dd40e0a1d46707560cc1bb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/mcp_service/chart/plugins/mixed_timeseries.py **Line:** 115:115 **Comment:** *Custom Rule: Add a short docstring describing how the chart title is generated and how filter context is incorporated. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39922&comment_hash=0dab44aa4ac7d2cf9f72b99d3e3cf5dc382535b4b89762b92b1953f325f18f6a&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39922&comment_hash=0dab44aa4ac7d2cf9f72b99d3e3cf5dc382535b4b89762b92b1953f325f18f6a&reaction=dislike'>👎</a> ########## superset/mcp_service/chart/registry.py: ########## @@ -0,0 +1,279 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +ChartTypeRegistry — central registry mapping chart_type strings to plugins. + +Replaces the four previously-scattered dispatch locations: + - schema_validator.py: chart_type_validators dict + - dataset_validator.py: isinstance branches in _extract_column_references() + - chart_utils.py: if/elif chain in map_config_to_form_data() + - dataset_validator.py: isinstance branches in normalize_column_names() + +Usage:: + + from superset.mcp_service.chart.registry import get_registry + + plugin = get_registry().get("xy") + if plugin is None: + raise ValueError("Unknown chart type: xy") + form_data = plugin.to_form_data(config, dataset_id) +""" + +from __future__ import annotations + +import logging +import sys +import threading +from collections.abc import Callable, Iterable +from dataclasses import dataclass, field +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from superset.mcp_service.chart.plugin import ChartTypePlugin + +logger = logging.getLogger(__name__) + +_REGISTRY: dict[str, "ChartTypePlugin"] = {} +_plugins_loaded = False +_plugins_load_failed = False +_plugins_lock = threading.RLock() + +# --------------------------------------------------------------------------- +# Plugin filter — replaced atomically by configure() at app startup. +# Default: all registered plugins visible (no disabled set, no callable). +# --------------------------------------------------------------------------- + +PluginEnabledFunc = Callable[[str], bool] + + +@dataclass(frozen=True) +class _PluginFilterConfig: + disabled_plugins: frozenset[str] = field(default_factory=frozenset) + enabled_func: PluginEnabledFunc | None = None + + +_filter_config: _PluginFilterConfig = _PluginFilterConfig() + + +def _ensure_plugins_loaded() -> None: + """Lazily import the plugins package to populate _REGISTRY. + + Called before every registry lookup so the registry is always populated, + even when callers (tests, chart_utils, validators) import this module + directly without first importing app.py. + """ + global _plugins_loaded, _plugins_load_failed + if _plugins_loaded or _plugins_load_failed: + return + with _plugins_lock: + if not _plugins_loaded and not _plugins_load_failed: + registry_before_import = dict(_REGISTRY) + try: + import superset.mcp_service.chart.plugins # noqa: F401 + + _plugins_loaded = True + except Exception: # noqa: BLE001 — plugin import may raise anything + _REGISTRY.clear() + _REGISTRY.update(registry_before_import) + _plugins_load_failed = True + logger.exception( + "Failed to load built-in chart type plugins; " + "further lookups will return None" + ) + + +def configure( + disabled: Iterable[str] | None = None, + enabled_func: PluginEnabledFunc | None = None, +) -> None: + """Set runtime plugin filters. Called once during app initialization. + + Replaces the filter config atomically with a single assignment so concurrent + readers always observe a consistent (disabled_plugins, enabled_func) pair. + + Args: + disabled: chart_type strings to suppress. Accepts any iterable (set, + frozenset, list, tuple). Ignored when enabled_func is provided. + enabled_func: callable(chart_type) -> bool. When set, overrides + ``disabled``. Must be cheap and in-process — no network I/O per + call. On exception the registry fails *closed* (plugin hidden). + """ + global _filter_config + + if enabled_func is not None and not callable(enabled_func): + raise TypeError("enabled_func must be callable or None") + + new_config = _PluginFilterConfig( + disabled_plugins=frozenset(disabled or ()), + enabled_func=enabled_func, + ) + _filter_config = new_config + + if new_config.disabled_plugins: + logger.info( + "MCP chart plugins disabled: %s", sorted(new_config.disabled_plugins) + ) + if new_config.enabled_func is not None: + logger.info( + "MCP chart plugin dynamic filter configured: %r", new_config.enabled_func + ) + + +def _is_plugin_enabled(chart_type: str) -> bool: + """Return True if the plugin is currently enabled (not filtered out).""" + config = _filter_config # read once — atomic reference in CPython + if config.enabled_func is not None: + try: + return bool(config.enabled_func(chart_type)) + except Exception: # noqa: BLE001 — operator-supplied callable may raise anything + logger.warning( + "MCP_CHART_PLUGIN_ENABLED_FUNC raised for chart_type=%r; " + "failing closed (plugin hidden)", + chart_type, + exc_info=True, + ) + return False + return chart_type not in config.disabled_plugins + + +def register(plugin: "ChartTypePlugin") -> None: + """Register a chart type plugin in the global registry.""" + if not plugin.chart_type: + raise ValueError(f"{type(plugin).__name__} must define a non-empty chart_type") + with _plugins_lock: + if plugin.chart_type in _REGISTRY: + logger.warning( + "Overwriting existing plugin for chart_type=%r", plugin.chart_type + ) + for existing in _REGISTRY.values(): + if existing.chart_type == plugin.chart_type: + continue + colliding = plugin.native_viz_types.keys() & existing.native_viz_types + if colliding: + # display_name_for_viz_type() resolves to the first plugin in + # iteration order, making the later registration unreachable. + logger.warning( + "Plugin %r declares native_viz_types %s already claimed by " + "plugin %r; viz_type display-name lookups will resolve to " + "the earlier registration", + plugin.chart_type, + sorted(colliding), + existing.chart_type, + ) + _REGISTRY[plugin.chart_type] = plugin + logger.debug("Registered chart plugin: %r", plugin.chart_type) + + +def get(chart_type: str) -> "ChartTypePlugin | None": + """Return the plugin for chart_type, or None if unknown or disabled.""" + _ensure_plugins_loaded() + if chart_type not in _REGISTRY or not _is_plugin_enabled(chart_type): + return None + return _REGISTRY[chart_type] + + +def all_types() -> list[str]: + """Return enabled registered chart type strings in insertion order.""" + _ensure_plugins_loaded() + return [ct for ct in _REGISTRY if _is_plugin_enabled(ct)] + + +def is_registered(chart_type: str) -> bool: + """Return True if chart_type has a registered plugin, regardless of enabled state. + + Use this to distinguish an unknown chart type from a disabled one. + Use is_enabled() to check whether the plugin is currently available. + """ + _ensure_plugins_loaded() + return chart_type in _REGISTRY + + +def is_enabled(chart_type: str) -> bool: + """Return True if chart_type is registered AND currently enabled.""" + _ensure_plugins_loaded() + return chart_type in _REGISTRY and _is_plugin_enabled(chart_type) + + +def display_name_for_viz_type(viz_type: str) -> str | None: + """Return the user-facing display name for a Superset-internal viz_type. + + Searches every registered plugin's ``native_viz_types`` mapping. + Returns None if no plugin recognises the viz_type. + + Example:: + + display_name_for_viz_type("echarts_timeseries_line") # "Line Chart" + display_name_for_viz_type("pivot_table_v2") # "Pivot Table" + display_name_for_viz_type("unknown_type") # None + """ + _ensure_plugins_loaded() + for plugin in _REGISTRY.values(): + name = plugin.native_viz_types.get(viz_type) + if name is not None: + return name + return None + + +def _reset_for_testing() -> None: + """Reset all registry state to defaults. + + Only for use in tests that need a clean slate. Calling this in production + will discard all registered plugins and any runtime filter configuration. + + **Caller responsibility**: This function pops ``superset.mcp_service.chart.plugins`` + from ``sys.modules`` and directly assigns module globals (``_REGISTRY``, + ``_plugins_loaded``, etc.). Direct global assignment is NOT automatically + reverted by pytest's ``monkeypatch`` fixture. Callers must either use + ``monkeypatch.setattr`` for each global, or call ``_reset_for_testing()`` again + in teardown to restore the clean state. See ``test_registry.py`` for the + recommended ``monkeypatch.setattr`` isolation pattern. + """ + global _REGISTRY, _plugins_loaded, _plugins_load_failed, _filter_config + with _plugins_lock: + _REGISTRY = {} + _plugins_loaded = False + _plugins_load_failed = False + _filter_config = _PluginFilterConfig() + sys.modules.pop("superset.mcp_service.chart.plugins", None) + + +class _RegistryProxy: + """Thin proxy exposing registry functions as instance methods.""" + + def get(self, chart_type: str) -> "ChartTypePlugin | None": + return get(chart_type) + + def all_types(self) -> list[str]: + return all_types() + + def is_registered(self, chart_type: str) -> bool: + return is_registered(chart_type) + + def is_enabled(self, chart_type: str) -> bool: + return is_enabled(chart_type) + + def display_name_for_viz_type(self, viz_type: str) -> str | None: + return display_name_for_viz_type(viz_type) Review Comment: **Suggestion:** Add concise docstrings to each newly added proxy method so all new functions are documented inline. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> These are newly added methods inside a new class and none of them have docstrings. The custom rule explicitly requires new Python functions and classes to include docstrings, so this is a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=73ab49fd5b894187ac947f8e8cef0efc&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=73ab49fd5b894187ac947f8e8cef0efc&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/mcp_service/chart/registry.py **Line:** 258:271 **Comment:** *Custom Rule: Add concise docstrings to each newly added proxy method so all new functions are documented inline. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39922&comment_hash=5f530751bd7ea66fa2d1456897abb2a742e1ddcb3cf56b11ec3a2f40b186695a&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39922&comment_hash=5f530751bd7ea66fa2d1456897abb2a742e1ddcb3cf56b11ec3a2f40b186695a&reaction=dislike'>👎</a> -- 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]
