pierrejeambrun commented on code in PR #67242:
URL: https://github.com/apache/airflow/pull/67242#discussion_r3493081312


##########
airflow-core/src/airflow/api_fastapi/core_api/routes/ui/dags.py:
##########
@@ -284,3 +288,59 @@ def get_latest_run_info(dag_id: str, session: SessionDep) 
-> DAGRunLightResponse
     latest_run_info = session.execute(latest_run_info_select).one_or_none()
 
     return DAGRunLightResponse(**latest_run_info._mapping) if latest_run_info 
else None
+
+
+@dags_router.get(
+    "/run_state_counts",
+    dependencies=[
+        Depends(requires_access_dag(method="GET")),
+        Depends(requires_access_dag(method="GET", 
access_entity=DagAccessEntity.RUN)),
+    ],
+    operation_id="get_dag_run_state_counts_ui",
+)
+def get_dag_run_state_counts(
+    session: SessionDep,
+    readable_dags_filter: ReadableDagsFilterDep,
+    dag_ids: Annotated[list[str], Query(min_length=1)],

Review Comment:
   `dag_ids` is unbounded here, and the query below builds a `UNION ALL` with 
one branch per Dag (times four states). The page only sends the current page 
today, so this is fine in practice, but could we add a `max_length` (roughly 
the max page size) so the union width cannot blow up if this endpoint is called 
with a large list?



##########
airflow-core/src/airflow/api_fastapi/core_api/routes/ui/dags.py:
##########
@@ -284,3 +288,59 @@ def get_latest_run_info(dag_id: str, session: SessionDep) 
-> DAGRunLightResponse
     latest_run_info = session.execute(latest_run_info_select).one_or_none()
 
     return DAGRunLightResponse(**latest_run_info._mapping) if latest_run_info 
else None
+
+
+@dags_router.get(
+    "/run_state_counts",
+    dependencies=[
+        Depends(requires_access_dag(method="GET")),
+        Depends(requires_access_dag(method="GET", 
access_entity=DagAccessEntity.RUN)),
+    ],
+    operation_id="get_dag_run_state_counts_ui",
+)
+def get_dag_run_state_counts(
+    session: SessionDep,
+    readable_dags_filter: ReadableDagsFilterDep,
+    dag_ids: Annotated[list[str], Query(min_length=1)],
+    run_after_gte: datetime | None = None,
+) -> DAGsRunStateCountsCollectionResponse:
+    """Return per-Dag DagRun state counts (zero-filled) for the Dag list 
page."""
+    permitted_dag_ids = readable_dags_filter.value or set()
+    requested_dag_ids = list(set(dag_ids) & permitted_dag_ids)
+    counts_by_dag: dict[str, dict[DagRunState, int]] = {
+        dag_id: {state: 0 for state in DagRunState} for dag_id in 
requested_dag_ids
+    }
+
+    if requested_dag_ids:
+        # One capped union per state, not a single (dag, state) union: keeping 
every
+        # branch on the same state lets the planner pick a uniform, efficient 
per-branch
+        # plan. A mixed-state union misplans and scans whole partitions 
(orders of
+        # magnitude slower). Each branch reads at most STATE_COUNT_CAP rows; 
the UI
+        # shows "N+" once a count reaches the cap.
+        for state in DagRunState:

Review Comment:
   The PR description says this is "a single `GROUP BY (dag_id, state)` query", 
but here it is four per-state `UNION ALL`s of capped subqueries. Could you 
update the description to match? It would also help to spell out why this is 
preferred over a plain `GROUP BY (dag_id, state)` (simpler, with exact counts), 
since the "mixed-state GROUP BY misplans" reasoning is not obvious; a quick 
benchmark would make the case.



##########
airflow-core/src/airflow/ui/src/pages/DagsList/DagsList.tsx:
##########
@@ -179,19 +205,32 @@ const {
   PAUSED,
 }: SearchParamsKeysType = SearchParamsKeys;
 
-const cardDef: CardDef<DAGWithLatestDagRunsResponse> = {
-  card: ({ row }) => <DagCard dag={row} />,
+const createCardDef = (runStateContext: RunStateCountsContext): 
CardDef<DAGWithLatestDagRunsResponse> => ({
+  card: ({ row }) => (
+    <DagCard
+      dag={row}
+      runStateCounts={runStateContext.countsByDag[row.dag_id]}
+      runStateCountsLoading={runStateContext.isLoading}
+      stateCountLimit={runStateContext.stateCountLimit}
+    />
+  ),
   meta: {
-    customSkeleton: <Skeleton height="120px" width="100%" />,
+    customSkeleton: <Skeleton height="140px" width="100%" />,
   },
-};
+});
+
+const DEFAULT_HOURS = "24";
 
 export const DagsList = () => {
   const { t: translate } = useTranslation();
   const [searchParams, setSearchParams] = useSearchParams();
   const [display, setDisplay] = useLocalStorage<"card" | 
"table">(DAGS_LIST_DISPLAY_KEY, "card");
   const dagRunsLimit = display === "card" ? 14 : 1;
 
+  const now = dayjs();

Review Comment:
   `now` is recomputed on every render but is only read by the two `useState` 
initialisers (first render only). A lazy initialiser (`useState(() => ...)`) 
avoids the per-render work.



##########
airflow-core/src/airflow/ui/src/pages/DagsList/DagRunStateCounts.tsx:
##########
@@ -0,0 +1,87 @@
+/*!
+ * 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 { HStack, Skeleton } from "@chakra-ui/react";
+import { useTranslation } from "react-i18next";
+
+import type { DagRunState } from "openapi/requests/types.gen";
+import { StateBadge } from "src/components/StateBadge";
+import { RouterLink, Tooltip } from "src/components/ui";
+import { SearchParamsKeys } from "src/constants/searchParams";
+
+const DISPLAYED_STATES: ReadonlyArray<DagRunState> = ["success", "failed", 
"running", "queued"];
+
+type Props = {
+  readonly compact?: boolean;
+  readonly counts: Record<string, number> | undefined;
+  readonly dagId: string;
+  readonly isLoading: boolean;
+  readonly stateCountLimit: number | undefined;
+};
+
+export const DagRunStateCounts = ({ compact = false, counts, dagId, isLoading, 
stateCountLimit }: Props) => {
+  const { t: translate } = useTranslation(["dags", "common"]);
+  const gap = compact ? 0.5 : 1;
+  const fontSize = compact ? "xs" : "sm";
+
+  if (isLoading || counts === undefined) {
+    // Render skeletons sized to a typical badge so the row doesn't reflow when
+    // counts arrive. Rendering zeros while loading would mislead the operator.
+    return (
+      <HStack
+        aria-label={translate("runStateCounts.loading")}
+        data-testid={`run-state-counts-loading-${dagId}`}
+        gap={gap}
+      >
+        {DISPLAYED_STATES.map((state) => (
+          <Skeleton borderRadius="full" height="22px" key={state} width="44px" 
/>
+        ))}
+      </HStack>
+    );
+  }
+
+  return (
+    <HStack data-testid={`run-state-counts-${dagId}`} gap={gap}>
+      {DISPLAYED_STATES.map((state) => {
+        const count = counts[state] ?? 0;
+        // A count that reached the API cap is only a lower bound; suffix it 
with "+".
+        const isCapped = stateCountLimit !== undefined && count >= 
stateCountLimit;
+        const suffix = isCapped ? "+" : "";
+        const translatedState = translate(`common:states.${state}` as const);
+        const tooltipContent = translate("runStateCounts.tooltip", {
+          formattedCount: `${count}${suffix}`,
+          state: translatedState,
+        });
+
+        return (
+          <Tooltip content={tooltipContent} key={state}>
+            <RouterLink
+              aria-label={tooltipContent}
+              data-testid={`run-state-count-${state}-${dagId}`}
+              to={`/dags/${dagId}/runs?${SearchParamsKeys.STATE}=${state}`}
+            >
+              <StateBadge fontSize={fontSize} opacity={count === 0 ? 0.4 : 1} 
state={state}>
+                {`${count}${suffix}`}

Review Comment:
   The description mentions large counts in compact notation (1.2K / 1.5M), but 
this renders the raw capped count plus `+` (for example `1000+`). Did the 
compact formatting get dropped, or should the description be updated?



##########
airflow-core/src/airflow/ui/src/pages/DagsList/DagsList.tsx:
##########
@@ -268,6 +305,22 @@ export const DagsList = () => {
     tagsMatchMode: selectedMatchMode,
   });
 
+  const { data: runStateCountsData, isLoading: runStateCountsLoading } = 
useDagRunStateCounts({
+    dagIds: data?.dags.map((dag) => dag.dag_id) ?? [],
+    dags: data?.dags,
+    startDate,
+  });
+  const runStateContext: RunStateCountsContext = {
+    countsByDag: Object.fromEntries(
+      (runStateCountsData?.dags ?? []).map((entry) => [entry.dag_id, 
entry.state_counts]),
+    ),
+    isLoading: runStateCountsLoading,
+    stateCountLimit: runStateCountsData?.state_count_limit,
+  };
+
+  const columns = createColumns(translate, runStateContext);
+  const cardDef = createCardDef(runStateContext);

Review Comment:
   `cardDef` is now rebuilt on every render (it used to be a module-level 
constant). Probably fine with the React Compiler, but could you confirm 
`DataTable` does not re-render off its identity?



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