You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "pierrejeambrun (via GitHub)" <gi...@apache.org> on 2023/02/11 18:05:46 UTC

[GitHub] [airflow] pierrejeambrun commented on a diff in pull request #29413: Add a new graph inside of the grid view

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


##########
airflow/www/static/js/dag/details/index.tsx:
##########
@@ -17,23 +17,89 @@
  * under the License.
  */
 
-import React from 'react';
+import React, { useEffect, useState } from 'react';
 import {
   Flex,
-  Box,
   Divider,
+  Tabs,
+  TabList,
+  TabPanel,
+  TabPanels,
+  Tab,
+  Text,
 } from '@chakra-ui/react';
+import { useSearchParams } from 'react-router-dom';
 
 import useSelection from 'src/dag/useSelection';
+import URLSearchParamsWrapper from 'src/utils/URLSearchParamWrapper';
+import { getTask, getMetaValue } from 'src/utils';
+import { useGridData, useTaskInstance } from 'src/api';
 
 import Header from './Header';
 import TaskInstanceContent from './taskInstance';
 import DagRunContent from './dagRun';
 import DagContent from './Dag';
+import Graph from './graph';
+import MappedInstances from './taskInstance/MappedInstances';
+import Logs from './taskInstance/Logs';
+import BackToTaskSummary from './taskInstance/BackToTaskSummary';
 
-const Details = () => {
+const SHOW_GRAPH_PARAM = 'show_graph';
+const dagId = getMetaValue('dag_id')!;
+
+interface Props {
+  openGroupIds: string[];
+  onToggleGroups: (groupIds: string[]) => void;
+}
+
+const Details = ({ openGroupIds, onToggleGroups }: Props) => {
   const { selected: { runId, taskId, mapIndex }, onSelect } = useSelection();
 
+  const [searchParams, setSearchParams] = useSearchParams();
+  const showGraph = !!searchParams.get(SHOW_GRAPH_PARAM);
+  const [tabIndex, setTabIndex] = useState(showGraph ? 1 : 0);
+
+  // TODO: restore preferred tab index logic

Review Comment:
   Do we want to do this in this PR or as a follow up ?



##########
airflow/www/static/js/dag/details/graph/index.tsx:
##########
@@ -0,0 +1,260 @@
+/*!
+ * 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 React, {
+  useRef, useMemo, useState, useEffect,
+} from 'react';
+import Color from 'color';
+import {
+  Box,
+  useTheme,
+  Select,
+  Text,
+} from '@chakra-ui/react';
+import ReactFlow, {
+  ReactFlowProvider,
+  Controls,
+  Background,
+  MiniMap,
+  Node as ReactFlowNode,
+  useReactFlow,
+  ControlButton,
+  Panel,
+} from 'reactflow';
+import { MdCenterFocusStrong } from 'react-icons/md';
+
+import { useGraphData, useGridData } from 'src/api';
+import useOffsetHeight from 'src/utils/useOffsetHeight';
+import useSelection from 'src/dag/useSelection';
+import { getTask } from 'src/utils';
+import type { TaskInstance } from 'src/types';
+import { useGraphLayout } from 'src/utils/graph';
+import type { NodeType } from 'src/datasets/Graph/Node';
+
+import Edge from './Edge';
+import Node, { CustomNodeProps } from './Node';
+
+interface Props {
+  openGroupIds: string[];
+  onToggleGroups: (groupIds: string[]) => void;
+}
+
+const Graph = ({ openGroupIds, onToggleGroups }: Props) => {
+  const graphRef = useRef(null);
+  const { data } = useGraphData();
+  const [arrange, setArrange] = useState(data?.arrange || 'LR');
+
+  useEffect(() => {
+    setArrange(data?.arrange || 'LR');
+  }, [data?.arrange]);
+
+  const { data: graphData } = useGraphLayout({
+    edges: data?.edges, nodes: data?.nodes, openGroupIds, arrange,
+  });
+  const { selected: { runId, taskId } } = useSelection();
+  const { data: { dagRuns, groups } } = useGridData();
+  const { colors } = useTheme();
+  const { setCenter } = useReactFlow();
+  const latestDagRunId = dagRuns[dagRuns.length - 1]?.runId;
+
+  const offsetHeight = useOffsetHeight(graphRef, undefined, 750);
+
+  const nodes: ReactFlowNode<CustomNodeProps>[] = [];
+  const edges = (graphData?.edges || []).map((edge) => ({
+    id: edge.id,
+    source: edge.sources[0],
+    target: edge.targets[0],
+    data: { rest: edge },
+    type: 'custom',
+  }));
+
+  const flattenNodes = (children: NodeType[], parent?: string) => {
+    const parentNode = parent ? { parentNode: parent } : undefined;
+    children.forEach((node) => {
+      let instance: TaskInstance | undefined;
+      const group = getTask({ taskId: node.id, task: groups });
+      if (!node.id.includes('join_id') && runId) {
+        instance = group?.instances.find((ti) => ti.runId === runId);
+      }
+      const isSelected = node.id === taskId && !!instance;
+
+      nodes.push({
+        id: node.id,
+        data: {
+          width: node.width,
+          height: node.height,
+          task: group,
+          instance,
+          isSelected,
+          latestDagRunId,
+          onToggleCollapse: () => {
+            let newGroupIds = [];
+            if (!node.value.isOpen) {
+              newGroupIds = [...openGroupIds, node.value.label];
+            } else {
+              newGroupIds = openGroupIds.filter((g) => g !== node.value.label);
+            }
+            onToggleGroups(newGroupIds);
+          },
+          ...node.value,
+        },
+        type: 'custom',
+        position: {
+          x: node.x || 0,
+          y: node.y || 0,
+        },
+        ...parentNode,
+      });
+      if (node.children) {
+        flattenNodes(node.children, node.id);
+      }
+    });
+  };
+
+  if (graphData?.children) {
+    flattenNodes(graphData.children);
+  }
+
+  const focusNode = () => {
+    const selectedNode = nodes.find((n) => n.id === taskId);
+    if (selectedNode) {
+      setCenter(selectedNode.position.x, selectedNode.position.y, { duration: 1000 });
+    }
+  };
+
+  const nodeTypes = useMemo(() => ({ custom: Node }), []);

Review Comment:
   Instead of using a useMemo can we just take it out of the component and drop the useMemo ? (same for `edgeTypes`)
   ```
   const nodeTypes = { custom: Node };
   const edgeTypes = { custom: Edge };
   ```



##########
airflow/www/static/js/dag/details/graph/index.tsx:
##########
@@ -0,0 +1,260 @@
+/*!
+ * 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 React, {
+  useRef, useMemo, useState, useEffect,
+} from 'react';
+import Color from 'color';
+import {
+  Box,
+  useTheme,
+  Select,
+  Text,
+} from '@chakra-ui/react';
+import ReactFlow, {
+  ReactFlowProvider,
+  Controls,
+  Background,
+  MiniMap,
+  Node as ReactFlowNode,
+  useReactFlow,
+  ControlButton,
+  Panel,
+} from 'reactflow';
+import { MdCenterFocusStrong } from 'react-icons/md';
+
+import { useGraphData, useGridData } from 'src/api';
+import useOffsetHeight from 'src/utils/useOffsetHeight';
+import useSelection from 'src/dag/useSelection';
+import { getTask } from 'src/utils';
+import type { TaskInstance } from 'src/types';
+import { useGraphLayout } from 'src/utils/graph';
+import type { NodeType } from 'src/datasets/Graph/Node';
+
+import Edge from './Edge';
+import Node, { CustomNodeProps } from './Node';
+
+interface Props {
+  openGroupIds: string[];
+  onToggleGroups: (groupIds: string[]) => void;
+}
+
+const Graph = ({ openGroupIds, onToggleGroups }: Props) => {
+  const graphRef = useRef(null);
+  const { data } = useGraphData();
+  const [arrange, setArrange] = useState(data?.arrange || 'LR');
+
+  useEffect(() => {
+    setArrange(data?.arrange || 'LR');
+  }, [data?.arrange]);
+
+  const { data: graphData } = useGraphLayout({
+    edges: data?.edges, nodes: data?.nodes, openGroupIds, arrange,
+  });
+  const { selected: { runId, taskId } } = useSelection();
+  const { data: { dagRuns, groups } } = useGridData();
+  const { colors } = useTheme();
+  const { setCenter } = useReactFlow();
+  const latestDagRunId = dagRuns[dagRuns.length - 1]?.runId;
+
+  const offsetHeight = useOffsetHeight(graphRef, undefined, 750);
+
+  const nodes: ReactFlowNode<CustomNodeProps>[] = [];
+  const edges = (graphData?.edges || []).map((edge) => ({
+    id: edge.id,
+    source: edge.sources[0],
+    target: edge.targets[0],
+    data: { rest: edge },
+    type: 'custom',
+  }));
+
+  const flattenNodes = (children: NodeType[], parent?: string) => {
+    const parentNode = parent ? { parentNode: parent } : undefined;
+    children.forEach((node) => {
+      let instance: TaskInstance | undefined;
+      const group = getTask({ taskId: node.id, task: groups });
+      if (!node.id.includes('join_id') && runId) {
+        instance = group?.instances.find((ti) => ti.runId === runId);
+      }
+      const isSelected = node.id === taskId && !!instance;
+
+      nodes.push({
+        id: node.id,
+        data: {
+          width: node.width,
+          height: node.height,
+          task: group,
+          instance,
+          isSelected,
+          latestDagRunId,
+          onToggleCollapse: () => {
+            let newGroupIds = [];
+            if (!node.value.isOpen) {
+              newGroupIds = [...openGroupIds, node.value.label];
+            } else {
+              newGroupIds = openGroupIds.filter((g) => g !== node.value.label);
+            }
+            onToggleGroups(newGroupIds);
+          },
+          ...node.value,
+        },
+        type: 'custom',
+        position: {
+          x: node.x || 0,
+          y: node.y || 0,
+        },
+        ...parentNode,
+      });
+      if (node.children) {
+        flattenNodes(node.children, node.id);
+      }
+    });
+  };
+
+  if (graphData?.children) {
+    flattenNodes(graphData.children);
+  }
+
+  const focusNode = () => {
+    const selectedNode = nodes.find((n) => n.id === taskId);
+    if (selectedNode) {
+      setCenter(selectedNode.position.x, selectedNode.position.y, { duration: 1000 });
+    }
+  };
+
+  const nodeTypes = useMemo(() => ({ custom: Node }), []);
+  const edgeTypes = useMemo(() => ({ custom: Edge }), []);
+
+  const newEdges = edges.map((e) => {

Review Comment:
   I think we can extract this outside the component, with a function, maybe like `buildGraphEdges(gridDataEdges, taskId,)`. This will make the component a bit smaller and easier to maintain on top. Same for nodeColor and nodeStroke I believe also avoid redefining these functions on each render.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org