pierrejeambrun commented on code in PR #67095: URL: https://github.com/apache/airflow/pull/67095#discussion_r3279664186
########## airflow-core/src/airflow/ui/src/queries/useBulkDeleteDagRuns.ts: ########## @@ -0,0 +1,97 @@ +/*! + * 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. + */ +import { useQueryClient } from "@tanstack/react-query"; +import { useState } from "react"; +import { useTranslation } from "react-i18next"; + +import { + useDagRunServiceBulkDagRuns, + useDagRunServiceGetDagRunsKey, + useTaskInstanceServiceGetTaskInstancesKey, +} from "openapi/queries"; +import type { BulkActionResponse, BulkBody_BulkDAGRunBody_, BulkResponse } from "openapi/requests/types.gen"; +import { toaster } from "src/components/ui"; + +type Props = { + readonly clearSelections: VoidFunction; + readonly onSuccessConfirm: VoidFunction; +}; + +const handleActionResult = ( + actionResult: BulkActionResponse, + setError: (error: unknown) => void, + onSuccess: (count: number, keys: Array<string>) => void, +) => { + const { errors, success } = actionResult; + + if (Array.isArray(errors) && errors.length > 0) { + const apiError = errors[0] as { error: string }; + + setError({ body: { detail: apiError.error } }); + } else if (Array.isArray(success) && success.length > 0) { + onSuccess(success.length, success); + } +}; + +export const useBulkDeleteDagRuns = ({ clearSelections, onSuccessConfirm }: Props) => { + const queryClient = useQueryClient(); + const [error, setError] = useState<unknown>(undefined); Review Comment: We do this because bulk endpoints can partially succeed. Answer '200' but with the errors field containing the errors. So we need to handle both. Plain error response status code != 2xx that's handled by react query. And response partially succeeded, `errors` field is not empty and response is 200. ``` class BulkActionResponse(BaseModel): """ Serializer for individual bulk action responses. Represents the outcome of a single bulk operation (create, update, or delete). The response includes a list of successful keys and any errors encountered during the operation. This structure helps users understand which key actions succeeded and which failed. """ success: list[str] = Field( default=[], description="A list of unique id/key representing successful operations." ) errors: list[dict[str, Any]] = Field( default=[], description="A list of errors encountered during the operation, each containing details about the issue.", ) ``` -- 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]
