Skip to content

Commit

Permalink
Fix bug in query invalidation and remove custom predicate logic (#46485)
Browse files Browse the repository at this point in the history
* Fix bug in query invalidation and remove custom predicate logic

* Address PR feedback

* Only use key fn when we have props to pass
  • Loading branch information
bbovenzi authored Feb 6, 2025
1 parent aaaea35 commit fd6cbed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
17 changes: 8 additions & 9 deletions airflow/ui/src/queries/useTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.",
Expand Down
22 changes: 0 additions & 22 deletions airflow/ui/src/utils/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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(
Expand Down

0 comments on commit fd6cbed

Please sign in to comment.