You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by bb...@apache.org on 2025/02/06 18:29:02 UTC

(airflow) branch main updated: Fix bug in query invalidation and remove custom predicate logic (#46485)

This is an automated email from the ASF dual-hosted git repository.

bbovenzi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new fd6cbed0802 Fix bug in query invalidation and remove custom predicate logic (#46485)
fd6cbed0802 is described below

commit fd6cbed0802fcf7840ec3d587fc4e4d2db1a05a1
Author: Brent Bovenzi <br...@astronomer.io>
AuthorDate: Thu Feb 6 13:28:54 2025 -0500

    Fix bug in query invalidation and remove custom predicate logic (#46485)
    
    * Fix bug in query invalidation and remove custom predicate logic
    
    * Address PR feedback
    
    * Only use key fn when we have props to pass
---
 airflow/ui/src/queries/useTrigger.ts | 17 ++++++++---------
 airflow/ui/src/utils/query.ts        | 22 ----------------------
 2 files changed, 8 insertions(+), 31 deletions(-)

diff --git a/airflow/ui/src/queries/useTrigger.ts b/airflow/ui/src/queries/useTrigger.ts
index 0a6e6f492ab..2c56bda6695 100644
--- a/airflow/ui/src/queries/useTrigger.ts
+++ b/airflow/ui/src/queries/useTrigger.ts
@@ -20,15 +20,14 @@ import { useQueryClient } from "@tanstack/react-query";
 import { useState } from "react";
 
 import {
-  useDagRunServiceGetDagRunsKey,
+  UseDagRunServiceGetDagRunsKeyFn,
   useDagRunServiceTriggerDagRun,
   useDagServiceGetDagsKey,
   useDagsServiceRecentDagRunsKey,
-  useTaskInstanceServiceGetTaskInstancesKey,
+  UseTaskInstanceServiceGetTaskInstancesKeyFn,
 } from "openapi/queries";
 import type { DagRunTriggerParams } from "src/components/TriggerDag/TriggerDAGForm";
 import { toaster } from "src/components/ui";
-import { doQueryKeysMatch, type PartialQueryKey } from "src/utils";
 
 export const useTrigger = ({ dagId, onSuccessConfirm }: { dagId: string; onSuccessConfirm: () => void }) => {
   const queryClient = useQueryClient();
@@ -37,14 +36,14 @@ export const useTrigger = ({ dagId, onSuccessConfirm }: { dagId: string; onSucce
   const [dateValidationError, setDateValidationError] = useState<unknown>(undefined);
 
   const onSuccess = async () => {
-    const queryKeys: Array<PartialQueryKey> = [
-      { baseKey: useDagServiceGetDagsKey },
-      { baseKey: useDagsServiceRecentDagRunsKey },
-      { baseKey: useDagRunServiceGetDagRunsKey, options: { dagIds: [dagId] } },
-      { baseKey: useTaskInstanceServiceGetTaskInstancesKey, options: { dagId, dagRunId: "~" } },
+    const queryKeys = [
+      [useDagServiceGetDagsKey],
+      [useDagsServiceRecentDagRunsKey],
+      UseDagRunServiceGetDagRunsKeyFn({ dagId }, [{ dagId }]),
+      UseTaskInstanceServiceGetTaskInstancesKeyFn({ dagId, dagRunId: "~" }, [{ dagId, dagRunId: "~" }]),
     ];
 
-    await queryClient.invalidateQueries({ predicate: (query) => doQueryKeysMatch(query, queryKeys) });
+    await Promise.all(queryKeys.map((key) => queryClient.invalidateQueries({ queryKey: key })));
 
     toaster.create({
       description: "DAG run has been successfully triggered.",
diff --git a/airflow/ui/src/utils/query.ts b/airflow/ui/src/utils/query.ts
index 415d1a5c9fa..095229d2dac 100644
--- a/airflow/ui/src/utils/query.ts
+++ b/airflow/ui/src/utils/query.ts
@@ -16,8 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-import type { Query } from "@tanstack/react-query";
-
 import { useDagServiceGetDagDetails } from "openapi/queries";
 import type { TaskInstanceState } from "openapi/requests/types.gen";
 import { useConfig } from "src/queries/useConfig";
@@ -32,26 +30,6 @@ export const isStatePending = (state?: TaskInstanceState | null) =>
   state === "restarting" ||
   !Boolean(state);
 
-export type PartialQueryKey = { baseKey: string; options?: Record<string, unknown> };
-
-// This allows us to specify what query key values we actually care about and ignore the rest
-// ex: match everything with this dagId and dagRunId but ignore anything related to pagination
-export const doQueryKeysMatch = (query: Query, queryKeysToMatch: Array<PartialQueryKey>) => {
-  const [baseKey, options] = query.queryKey;
-
-  const matchedKey = queryKeysToMatch.find((qk) => qk.baseKey === baseKey);
-
-  if (!matchedKey) {
-    return false;
-  }
-
-  return matchedKey.options
-    ? Object.entries(matchedKey.options).every(
-        ([key, value]) => typeof options === "object" && (options as Record<string, unknown>)[key] === value,
-      )
-    : true;
-};
-
 export const useAutoRefresh = ({ dagId, isPaused }: { dagId?: string; isPaused?: boolean }) => {
   const autoRefreshInterval = useConfig("auto_refresh_interval") as number | undefined;
   const { data: dag } = useDagServiceGetDagDetails(