guan404ming commented on code in PR #50443:
URL: https://github.com/apache/airflow/pull/50443#discussion_r2086862484


##########
airflow-core/src/airflow/api_fastapi/core_api/services/public/task_instances.py:
##########
@@ -0,0 +1,146 @@
+# 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 fastapi import HTTPException, status
+from pydantic import ValidationError
+from pytest import Session
+from sqlalchemy import select
+
+from airflow.api_fastapi.core_api.datamodels.common import (
+    BulkActionNotOnExistence,
+    BulkActionResponse,
+    BulkBody,
+    BulkCreateAction,
+    BulkDeleteAction,
+    BulkUpdateAction,
+)
+from airflow.api_fastapi.core_api.datamodels.task_instances import 
BulkTaskInstanceBody
+from airflow.api_fastapi.core_api.services.public.common import BulkService
+from airflow.models.taskinstance import TaskInstance
+
+
+class BulkTaskInstanceService(BulkService[BulkTaskInstanceBody]):
+    """Service for handling bulk operations on task instances."""
+
+    def __init__(
+        self, session: Session, request: BulkBody[BulkTaskInstanceBody], 
dag_id: str, dag_run_id: str
+    ):
+        super().__init__(session, request)
+        self.dag_id = dag_id
+        self.dag_run_id = dag_run_id
+
+    def categorize_task_instances(self, task_ids: set) -> tuple[dict, set, 
set]:
+        """
+        Categorize the given task_ids into matched_task_ids and 
not_found_task_ids based on existing task_ids.
+
+        Existed task instances are returned as a dict of {task_id : 
TaskInstance}.
+
+        :param task_ids: set of task_ids
+        :return: tuple of dict of existed task instances, set of matched 
task_ids, set of not found task_ids
+        """
+        existed_task_instances = self.session.execute(
+            select(TaskInstance).filter(TaskInstance.task_id.in_(task_ids))
+        ).scalars()
+        existed_task_instances_dict = {
+            task_instance.task_id: task_instance for task_instance in 
existed_task_instances
+        }
+        matched_task_ids = set(existed_task_instances_dict.keys())
+        not_found_task_ids = task_ids - matched_task_ids
+        return existed_task_instances_dict, matched_task_ids, 
not_found_task_ids
+
+    def handle_bulk_create(
+        self, action: BulkCreateAction[BulkTaskInstanceBody], results: 
BulkActionResponse
+    ) -> None:
+        results.errors.append(
+            {
+                "error": "Task instances bulk create is not supported",
+                "status_code": status.HTTP_405_METHOD_NOT_ALLOWED,
+            }
+        )
+
+    def handle_bulk_update(

Review Comment:
   I've moved the function into the service root level. I also notice that when 
patching in public route it does have some more verification than here. Thus, I 
tried to add them here but I'm not sure if it is necessary. Please let me know 
if I should keep it here or remove it and replace with the attribute setter. 
Thanks!



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

Reply via email to