jason810496 commented on code in PR #53216: URL: https://github.com/apache/airflow/pull/53216#discussion_r2204952685
########## airflow-core/src/airflow/api_fastapi/core_api/services/ui/dag_version_service.py: ########## @@ -0,0 +1,169 @@ +# 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. + +from __future__ import annotations + +from typing import Any + +import structlog +from sqlalchemy import select + +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.models.dag_version import DagVersion +from airflow.models.dagrun import DagRun +from airflow.models.taskinstance import TaskInstance + +log = structlog.get_logger(logger_name=__name__) + + +class DagVersionService: + """Service class for managing DAG version operations and comparisons.""" + + def __init__(self, session: SessionDep): + self.session = session + + def detect_mixed_versions(self, dag_id: str, dag_run_ids: list[str]) -> dict[str, dict]: Review Comment: How about adding `DagVersionInfo(TypedDict)` as type annotation for the return type? ########## airflow-core/src/airflow/api_fastapi/core_api/services/ui/dag_version_service.py: ########## @@ -0,0 +1,169 @@ +# 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. + +from __future__ import annotations + +from typing import Any + +import structlog +from sqlalchemy import select + +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.models.dag_version import DagVersion +from airflow.models.dagrun import DagRun +from airflow.models.taskinstance import TaskInstance + +log = structlog.get_logger(logger_name=__name__) + + +class DagVersionService: + """Service class for managing DAG version operations and comparisons.""" + + def __init__(self, session: SessionDep): + self.session = session + + def detect_mixed_versions(self, dag_id: str, dag_run_ids: list[str]) -> dict[str, dict]: + """ + Detect mixed versions within DagRuns. + + Args: + dag_id: The DAG ID to check + dag_run_ids: List of DagRun IDs to analyze + + Returns: + Dictionary mapping run_id to mixed version info + """ + # Single optimized query to get all TaskInstance version info + task_instance_versions = self.session.execute( + select(TaskInstance.run_id, TaskInstance.dag_version_id, DagVersion.version_number) + .join(DagVersion, TaskInstance.dag_version_id == DagVersion.id, isouter=True) + .where(TaskInstance.dag_id == dag_id, TaskInstance.run_id.in_(dag_run_ids)) + ).all() + + # Group by run_id for efficient processing + run_version_map: dict[str, list[dict[str, Any]]] = {} + for run_id, version_id, version_number in task_instance_versions: + if run_id not in run_version_map: + run_version_map[run_id] = [] + if version_id: + run_version_map[run_id].append({"version_id": version_id, "version_number": version_number}) + + # Calculate mixed version info for each DagRun + dag_run_mixed_versions = {} + for run_id, versions in run_version_map.items(): + if not versions: + dag_run_mixed_versions[run_id] = {"has_mixed_versions": False, "latest_version_number": None} + continue + + unique_version_ids = set(v["version_id"] for v in versions) + has_mixed_versions = len(unique_version_ids) > 1 + + # Get the latest version number if mixed versions exist + latest_version_number = None + if has_mixed_versions: + latest_version_number = max( + v["version_number"] for v in versions if v["version_number"] is not None + ) Review Comment: I think we can get the `has_mixed_versions` and `latest_version_number` in on loop. ########## airflow-core/src/airflow/api_fastapi/core_api/services/ui/dag_version_service.py: ########## @@ -0,0 +1,169 @@ +# 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. + +from __future__ import annotations + +from typing import Any + +import structlog +from sqlalchemy import select + +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.models.dag_version import DagVersion +from airflow.models.dagrun import DagRun +from airflow.models.taskinstance import TaskInstance + +log = structlog.get_logger(logger_name=__name__) + + +class DagVersionService: + """Service class for managing DAG version operations and comparisons.""" + + def __init__(self, session: SessionDep): + self.session = session + + def detect_mixed_versions(self, dag_id: str, dag_run_ids: list[str]) -> dict[str, dict]: + """ + Detect mixed versions within DagRuns. + + Args: + dag_id: The DAG ID to check + dag_run_ids: List of DagRun IDs to analyze + + Returns: + Dictionary mapping run_id to mixed version info + """ + # Single optimized query to get all TaskInstance version info + task_instance_versions = self.session.execute( + select(TaskInstance.run_id, TaskInstance.dag_version_id, DagVersion.version_number) + .join(DagVersion, TaskInstance.dag_version_id == DagVersion.id, isouter=True) + .where(TaskInstance.dag_id == dag_id, TaskInstance.run_id.in_(dag_run_ids)) + ).all() + + # Group by run_id for efficient processing + run_version_map: dict[str, list[dict[str, Any]]] = {} + for run_id, version_id, version_number in task_instance_versions: + if run_id not in run_version_map: + run_version_map[run_id] = [] + if version_id: + run_version_map[run_id].append({"version_id": version_id, "version_number": version_number}) + + # Calculate mixed version info for each DagRun + dag_run_mixed_versions = {} + for run_id, versions in run_version_map.items(): + if not versions: + dag_run_mixed_versions[run_id] = {"has_mixed_versions": False, "latest_version_number": None} + continue + + unique_version_ids = set(v["version_id"] for v in versions) + has_mixed_versions = len(unique_version_ids) > 1 + + # Get the latest version number if mixed versions exist + latest_version_number = None + if has_mixed_versions: + latest_version_number = max( + v["version_number"] for v in versions if v["version_number"] is not None + ) + + dag_run_mixed_versions[run_id] = { + "has_mixed_versions": has_mixed_versions, + "latest_version_number": latest_version_number, + } + + return dag_run_mixed_versions + + def detect_version_changes( + self, dag_runs: list[DagRun], mixed_versions_info: dict[str, dict] + ) -> list[dict]: Review Comment: How about adding `TypedDict` here for the return type as well ? -- 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]
