pierrejeambrun commented on code in PR #46296: URL: https://github.com/apache/airflow/pull/46296#discussion_r1937442409
########## airflow/ui/src/utils/query.ts: ########## @@ -0,0 +1,67 @@ +/*! + * 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 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"; + +export const isStatePending = (state?: TaskInstanceState | null) => + state === "deferred" || + state === "scheduled" || + state === "running" || + state === "up_for_reschedule" || + state === "up_for_retry" || + state === "queued" || + state === "restarting" || + !Boolean(state); + +export type PartialQueryKey = { baseKey: string; options?: Record<string, unknown> }; + +export const doQueriesMatch = (query: Query, queriesToMatch: Array<PartialQueryKey>) => { + const [baseKey, options] = query.queryKey; + + const matchedKey = queriesToMatch.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; +}; Review Comment: I'm not sure I get this piece. Can you explain what this is achieving on top of the invalidation API from react-query. For instance this kind of matching can be done with the auto-generated functions: ``` { baseKey: useDagRunServiceGetDagRunsKey, options: { dagIds: [dagId] } }, { baseKey: useTaskInstanceServiceGetTaskInstancesKey, options: { dagId, dagRunId: "~" } ``` ########## airflow/ui/src/pages/Dag/Dag.tsx: ########## @@ -42,20 +44,46 @@ export const Dag = () => { dagId, }); + const refetchInterval = useAutoRefresh({ dagId }); + // TODO: replace with with a list dag runs by dag id request const { data: runsData, error: runsError, isLoading: isLoadingRuns, } = useDagsServiceRecentDagRuns({ dagIds: [dagId] }, undefined, { enabled: Boolean(dagId), + refetchInterval: (query) => + query.state.data?.dags + .find((recentDag) => recentDag.dag_id === dagId) + ?.latest_dag_runs.some((run) => isStatePending(run.state)) + ? refetchInterval + : false, }); - const runs = runsData?.dags.find((dagWithRuns) => dagWithRuns.dag_id === dagId)?.latest_dag_runs ?? []; + const dagWithRuns = runsData?.dags.find((recentDag) => recentDag.dag_id === dagId); return ( <DetailsLayout dag={dag} error={error ?? runsError} isLoading={isLoading || isLoadingRuns} tabs={tabs}> - <Header dag={dag} dagId={dagId} latestRun={runs[0]} /> + <Header + dag={ + dag + ? ({ + ...dag, + // We only need to refresh latest runs and next run + latest_dag_runs: dagWithRuns?.latest_dag_runs, + next_dagrun: dagWithRuns?.next_dagrun, + next_dagrun_create_after: dagWithRuns?.next_dagrun_create_after, + next_dagrun_data_interval_end: dagWithRuns?.next_dagrun_data_interval_end, + next_dagrun_data_interval_start: dagWithRuns?.next_dagrun_data_interval_start, + } as DAGDetailsResponse & DAGWithLatestDagRunsResponse) + : undefined + } + dagId={dagId} Review Comment: Can't we retrive the dagId from the dag object in the other props ? ########## airflow/ui/src/pages/Dag/Dag.tsx: ########## @@ -42,20 +44,46 @@ export const Dag = () => { dagId, }); + const refetchInterval = useAutoRefresh({ dagId }); + // TODO: replace with with a list dag runs by dag id request const { data: runsData, error: runsError, isLoading: isLoadingRuns, } = useDagsServiceRecentDagRuns({ dagIds: [dagId] }, undefined, { enabled: Boolean(dagId), + refetchInterval: (query) => + query.state.data?.dags + .find((recentDag) => recentDag.dag_id === dagId) + ?.latest_dag_runs.some((run) => isStatePending(run.state)) + ? refetchInterval + : false, }); - const runs = runsData?.dags.find((dagWithRuns) => dagWithRuns.dag_id === dagId)?.latest_dag_runs ?? []; + const dagWithRuns = runsData?.dags.find((recentDag) => recentDag.dag_id === dagId); return ( <DetailsLayout dag={dag} error={error ?? runsError} isLoading={isLoading || isLoadingRuns} tabs={tabs}> - <Header dag={dag} dagId={dagId} latestRun={runs[0]} /> + <Header + dag={ + dag + ? ({ + ...dag, + // We only need to refresh latest runs and next run + latest_dag_runs: dagWithRuns?.latest_dag_runs, + next_dagrun: dagWithRuns?.next_dagrun, + next_dagrun_create_after: dagWithRuns?.next_dagrun_create_after, + next_dagrun_data_interval_end: dagWithRuns?.next_dagrun_data_interval_end, + next_dagrun_data_interval_start: dagWithRuns?.next_dagrun_data_interval_start, + } as DAGDetailsResponse & DAGWithLatestDagRunsResponse) + : undefined + } Review Comment: Not sure I like how we merged `DAGDetailsResponse` and `DAGWithLatestDagRunsResponse` into one single object. -- 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]
