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


##########
airflow-core/src/airflow/api_fastapi/core_api/datamodels/ui/common.py:
##########
@@ -79,6 +79,8 @@ class GridRunsResponse(BaseModel):
     run_after: datetime
     state: DagRunState | None
     run_type: DagRunType
+    bundle_version: str | None = None

Review Comment:
   is `bundle_version` used? 



##########
airflow-core/src/airflow/ui/src/layouts/Details/Grid/useGridRunsWithVersionFlags.ts:
##########
@@ -0,0 +1,70 @@
+/*!
+ * 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 { useMemo } from "react";
+
+import type { GridRunsResponse } from "openapi/requests";
+import { VersionIndicatorDisplayOptions } from 
"src/constants/showVersionIndicatorOptions";
+
+export type GridRunWithVersionFlags = {
+  isBundleVersionChange: boolean;
+  isDagVersionChange: boolean;
+} & GridRunsResponse;
+
+type UseGridRunsWithVersionFlagsParams = {
+  gridRuns: Array<GridRunsResponse> | undefined;
+  showVersionIndicatorMode?: VersionIndicatorDisplayOptions;
+};
+
+// Hook to calculate version change flags for grid runs.
+export const useGridRunsWithVersionFlags = ({
+  gridRuns,
+  showVersionIndicatorMode,
+}: UseGridRunsWithVersionFlagsParams): Array<GridRunWithVersionFlags> | 
undefined => {
+  const isVersionIndicatorEnabled = showVersionIndicatorMode !== 
VersionIndicatorDisplayOptions.NONE;
+
+  return useMemo(() => {
+    if (!gridRuns) {
+      return undefined;
+    }
+
+    if (!isVersionIndicatorEnabled) {
+      return gridRuns.map((run) => ({ ...run, isBundleVersionChange: false, 
isDagVersionChange: false }));
+    }
+
+    return gridRuns.map((run, index) => {
+      const prevRun = gridRuns[index + 1];

Review Comment:
   ```suggestion
         const nextRun = gridRuns[index + 1];
   ```



##########
airflow-core/src/airflow/ui/src/layouts/Details/Grid/TaskInstancesColumn.tsx:
##########
@@ -90,6 +115,23 @@ export const TaskInstancesColumn = ({ nodes, onCellClick, 
run, virtualItems }: P
           );
         }
 
+        let hasVersionChangeFlag = false;
+
+        if (
+          hasMixedVersions &&
+          (showVersionIndicatorMode === VersionIndicatorDisplayOptions.DAG ||
+            showVersionIndicatorMode === VersionIndicatorDisplayOptions.ALL) &&
+          idx > 0
+        ) {
+          const prevVirtualItem = itemsToRender[idx - 1];
+          const prevNode = prevVirtualItem ? nodes[prevVirtualItem.index] : 
undefined;
+          const prevTaskInstance = prevNode ? taskInstanceMap.get(prevNode.id) 
: undefined;
+
+          hasVersionChangeFlag = Boolean(
+            prevTaskInstance && prevTaskInstance.dag_version_number !== 
taskInstance.dag_version_number,
+          );
+        }
+

Review Comment:
   Why aren't we checking for `BundleVersion` change too ? 
   
   DagVersion could be the same (serialized dag is identical) but the bundle 
version changed (version of the file in the code)



##########
airflow-core/src/airflow/api_fastapi/core_api/routes/ui/grid.py:
##########
@@ -274,17 +274,34 @@ def get_grid_runs(
     triggering_user: QueryDagRunTriggeringUserSearch,
 ) -> list[GridRunsResponse]:
     """Get info about a run for the grid."""
-    # Retrieve, sort the previous DAG Runs
-    base_query = select(
-        DagRun.dag_id,
-        DagRun.run_id,
-        DagRun.queued_at,
-        DagRun.start_date,
-        DagRun.end_date,
-        DagRun.run_after,
-        DagRun.state,
-        DagRun.run_type,
-    ).where(DagRun.dag_id == dag_id)
+    # get the highest dag_version_number from TIs for each run
+    latest_ti_version = (
+        select(
+            TaskInstance.run_id,
+            func.max(DagVersion.version_number).label("version_number"),
+        )
+        .join(DagVersion, TaskInstance.dag_version_id == DagVersion.id)
+        .where(TaskInstance.dag_id == dag_id)
+        .group_by(TaskInstance.run_id)
+        .subquery()
+    )

Review Comment:
   We don't need to go through all that trouble to only retrieve the 'last' 
version. Just return the `DagRun.dag_versions` attribute.
   
   Also this will be consistent for other contributor. People will get confused 
as to why a DagRun has `dag_versions` as a list in the plublic interface, but 
on the grid endpoint here it's only `dag_version` and not a list anymore. 
   
   I find this confusing and it adds a lot of unecessary application code.



##########
airflow-core/src/airflow/ui/src/layouts/Details/Grid/TaskInstancesColumn.tsx:
##########
@@ -32,25 +35,47 @@ type Props = {
   readonly nodes: Array<GridTask>;
   readonly onCellClick?: () => void;
   readonly run: GridRunsResponse;
+  readonly showVersionIndicatorMode?: VersionIndicatorDisplayOptions;
   readonly virtualItems?: Array<VirtualItem>;
 };
 
 const ROW_HEIGHT = 20;
 
-export const TaskInstancesColumn = ({ nodes, onCellClick, run, virtualItems }: 
Props) => {
+export const TaskInstancesColumn = ({
+  nodes,
+  onCellClick,
+  run,
+  showVersionIndicatorMode,
+  virtualItems,
+}: Props) => {
   const { dagId = "", runId } = useParams();
   const { data: gridTISummaries } = useGridTiSummaries({ dagId, runId: 
run.run_id, state: run.state });
   const { hoveredRunId, setHoveredRunId } = useHover();
 
   const itemsToRender =
     virtualItems ?? nodes.map((_, index) => ({ index, size: ROW_HEIGHT, start: 
index * ROW_HEIGHT }));
 
-  const taskInstances = gridTISummaries?.task_instances ?? [];
-  const taskInstanceMap = new Map<string, LightGridTaskInstanceSummary>();
+  const taskInstances = useMemo(
+    () => gridTISummaries?.task_instances ?? [],
+    [gridTISummaries?.task_instances],
+  );
+  const taskInstanceMap = useMemo(() => {
+    const map = new Map<string, LightGridTaskInstanceSummary>();
+
+    for (const ti of taskInstances) {
+      map.set(ti.task_id, ti);
+    }
+
+    return map;
+  }, [taskInstances]);

Review Comment:
   I thought react compiler would memo this for us.



##########
airflow-core/src/airflow/ui/src/constants/showVersionIndicatorOptions.ts:
##########
@@ -0,0 +1,46 @@
+/*!
+ * 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 { createListCollection } from "@chakra-ui/react";
+
+export enum VersionIndicatorDisplayOptions {
+  ALL = "all",
+  BUNDLE = "bundle",
+  DAG = "dag",

Review Comment:
   Nit:
   
   ```suggestion
     BUNDLE_VERSION = "bundle",
     DAG_VERSION = "dag",
   ```



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