safaehar commented on code in PR #77025:
URL: https://github.com/apache/airflow/pull/77025#discussion_r4741050922


##########
airflow-core/src/airflow/ui/src/queries/routePrefetch.ts:
##########
@@ -0,0 +1,256 @@
+/*!
+ * 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 { QueryClient } from "@tanstack/react-query";
+import dayjs from "dayjs";
+import type { LoaderFunctionArgs } from "react-router-dom";
+
+import { ensureUseTaskInstanceServiceGetMappedTaskInstanceData } from 
"openapi/queries/ensureQueryData";
+import {
+  prefetchUseAssetServiceGetAssetEvents,
+  prefetchUseDagRunServiceGetDagRun,
+  prefetchUseDagServiceGetDag,
+  prefetchUseDagServiceGetDagDetails,
+  prefetchUseDagServiceGetDagsUi,
+  prefetchUseDagServiceGetLatestRunInfo,
+  prefetchUseDashboardServiceDagStats,
+  prefetchUseDashboardServiceHistoricalMetrics,
+  prefetchUseGridServiceGetGridRuns,
+  prefetchUseImportErrorServiceGetImportErrors,
+  prefetchUseMonitorServiceGetHealth,
+  prefetchUsePluginServiceGetPlugins,
+  prefetchUsePluginServiceImportErrors,
+  prefetchUsePoolServiceGetPools,
+  prefetchUseTaskInstanceServiceGetLog,
+} from "openapi/queries/prefetch";
+
+import type { DagView } from "src/constants/dagView";
+import {
+  dagRunsLimitKey,
+  DEFAULT_DAG_VIEW_KEY,
+  DEFAULT_LANDING_PAGE_KEY,
+  DEFAULT_TASK_INSTANCE_TAB_KEY,
+} from "src/constants/localStorage";
+import { SearchParamsKeys } from "src/constants/searchParams";
+import { DEFAULT_TASK_INSTANCE_TAB_PATHS, type DefaultTaskInstanceTab } from 
"src/constants/tab";
+import { DEFAULT_LIMIT as DEFAULT_DAG_RUNS_LIMIT, readLimitParam } from 
"src/hooks/useDagRunsLimit";
+
+// Route loaders run in parallel with the root loader (the UI config), so 
these requests start while the app
+// loads instead of after the page's first render. Each one uses the generated 
prefetch helpers, so its query
+// key is the one the page's own query builds and the page renders from the 
cache. The loaders do not wait for
+// their requests and never fail, so they do not delay or break rendering; a 
page whose query ends up with a
+// different key only sends its own request, as it does without the loaders.
+
+// The only URL parameters that change the task instance or log query keys. 
Others, such as the Logs tab's
+// log level and source filters, leave the keys unchanged.
+const ATTEMPT_SEARCH_PARAMS = [
+  SearchParamsKeys.TRY_NUMBER,
+  SearchParamsKeys.REGION_ID,
+  SearchParamsKeys.REGION_INDEX,
+] as const;
+
+const hasAttemptSearchParams = (request: Request) => {
+  const { searchParams } = new URL(request.url);
+
+  return ATTEMPT_SEARCH_PARAMS.some((key) => searchParams.has(key));
+};
+
+// Reads a value the way the pages' `useLocalStorage` hooks do (JSON), falling 
back to their default.
+const readStoredValue = (key: string, fallback: unknown): unknown => {
+  try {
+    const raw = globalThis.localStorage.getItem(key);
+
+    return raw === null ? fallback : (JSON.parse(raw) as unknown);
+  } catch {
+    // Unreadable storage: the pages fall back to their default, and so do the 
loaders.
+    return fallback;
+  }
+};
+
+// The URL parameters that filter the Grid's runs, and so change its query key.
+const GRID_FILTER_SEARCH_PARAMS = [
+  SearchParamsKeys.RUN_AFTER_GTE,
+  SearchParamsKeys.RUN_AFTER_LTE,
+  SearchParamsKeys.RUN_ID_PATTERN,
+  SearchParamsKeys.RUN_TYPE,
+  SearchParamsKeys.STATE,
+  SearchParamsKeys.TRIGGERING_USER_NAME_PATTERN,
+] as const;
+
+// The Grid's first page of runs, as the Grid and DetailsLayout request it: 
`limit` comes from the URL, else
+// the Dag's stored limit, else the default, plus one to detect a next page. 
Skipped when the URL filters the
+// runs, and in the Gantt view, whose limit also depends on the panel width. 
In the graph view, only
+// DetailsLayout requests this page, and only on run and task pages.
+const prefetchGridFirstPage = (
+  queryClient: QueryClient,
+  { dagId, onRunPage, request }: { dagId: string; onRunPage: boolean; request: 
Request },
+) => {
+  const { searchParams } = new URL(request.url);
+  const dagView = readStoredValue(DEFAULT_DAG_VIEW_KEY, "grid") as DagView;
+
+  if (
+    GRID_FILTER_SEARCH_PARAMS.some((key) => searchParams.has(key)) ||
+    !(dagView === "grid" || (dagView === "graph" && onRunPage))
+  ) {
+    return;
+  }
+  const limit =

Review Comment:
   Addressed. readLimitParam now rejects non-finite and non-positive values, so 
both useDagRunsLimit and the route loader fall back to the stored or default 
limit. Tests cover abc, 0, -1, and Infinity, plus the loader’s default query key



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