alexandrusoare commented on code in PR #40975: URL: https://github.com/apache/superset/pull/40975#discussion_r3552059918
########## superset/mcp_service/dataset/tool/update_dataset_metric.py: ########## @@ -0,0 +1,258 @@ +# 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. + +"""Update a saved metric on a dataset (FastMCP tool).""" + +import difflib +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dataset.schemas import ( + DatasetMetricDetail, + MetricCurrency, + UpdateDatasetMetricRequest, + UpdateDatasetMetricResponse, +) +from superset.mcp_service.utils import ( + escape_llm_context_delimiters, + sanitize_for_llm_context, +) + +logger = logging.getLogger(__name__) + + +def _find_metric(metrics: list[Any], identifier: int | str) -> Any | None: + """Find a saved metric by ID, UUID, or metric_name.""" + if isinstance(identifier, str): + try: + identifier = int(identifier) + except ValueError: + pass + + if isinstance(identifier, int): + return next((m for m in metrics if m.id == identifier), None) + + lowered = identifier.lower() + by_uuid = next( + (m for m in metrics if m.uuid and str(m.uuid).lower() == lowered), None + ) + if by_uuid is not None: + return by_uuid + return next((m for m in metrics if m.metric_name == identifier), None) + + +def _metric_not_found_message(metrics: list[Any], identifier: int | str) -> str: + names = [m.metric_name for m in metrics] + msg = f"Metric '{identifier}' not found on this dataset." + if not names: + return f"{msg} This dataset has no saved metrics." + suggestions = difflib.get_close_matches(str(identifier), names, n=3, cutoff=0.6) + if suggestions: + return f"{msg} Did you mean: {', '.join(suggestions)}?" + return f"{msg} Available metrics: {', '.join(sorted(names))}." Review Comment: What do you think of this? -- 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]
