xvega commented on code in PR #62369:
URL: https://github.com/apache/airflow/pull/62369#discussion_r2927463659


##########
airflow-core/src/airflow/ui/src/queries/useGridTISummaries.ts:
##########
@@ -16,45 +16,110 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-import { useGridServiceGetGridTiSummaries } from "openapi/queries";
-import type { TaskInstanceState } from "openapi/requests";
+import { useEffect, useState } from "react";
+
+import { OpenAPI } from "openapi/requests/core/OpenAPI";
+import type { GridTISummaries, TaskInstanceState } from "openapi/requests";
 import { isStatePending, useAutoRefresh } from "src/utils";
 
-export const useGridTiSummaries = ({
+/**
+ * Streams TI summaries for all grid runs over a single HTTP connection 
(NDJSON).
+ *
+ * The server emits one JSON line per Dag run as soon as that run's task
+ * instances have been computed, so the grid renders each column progressively
+ * rather than waiting for the entire payload.  This eliminates the N+1 request
+ * pattern without loading all runs into one large query.
+ *
+ * Auto-refreshes while any run is still in a pending state.
+ */
+export const useGridTiSummariesStream = ({
   dagId,
-  enabled,
-  isSelected,
-  runId,
-  state,
+  runIds,
+  states,
 }: {
   dagId: string;
-  enabled?: boolean;
-  isSelected?: boolean;
-  runId: string;
-  state?: TaskInstanceState | null | undefined;
+  runIds: Array<string>;
+  states?: Array<TaskInstanceState | null | undefined>;
 }) => {
+  const [summariesByRunId, setSummariesByRunId] = useState<Map<string, 
GridTISummaries>>(new Map());
+  const [isStreaming, setIsStreaming] = useState(false);
+  const [refreshTick, setRefreshTick] = useState(0);
+
   const baseRefetchInterval = useAutoRefresh({ dagId });
-  const slowRefreshMultiplier = 5;
-  const refetchInterval =
-    typeof baseRefetchInterval === "number"
-      ? baseRefetchInterval * (isSelected ? 1 : slowRefreshMultiplier)
-      : baseRefetchInterval;
-
-  const { data: gridTiSummaries, ...rest } = useGridServiceGetGridTiSummaries(
-    {
-      dagId,
-      runId,
-    },
-    undefined,
-    {
-      enabled: Boolean(runId) && Boolean(dagId) && enabled,
-      placeholderData: (prev) => prev,
-      refetchInterval: (query) =>
-        ((state !== undefined && isStatePending(state)) ||
-          query.state.data?.task_instances.some((ti) => 
isStatePending(ti.state))) &&
-        refetchInterval,
-    },
-  );
-
-  return { data: gridTiSummaries, ...rest };
+  const hasActiveRuns = states?.some((s) => s !== undefined && 
isStatePending(s)) ?? false;
+
+  // Stable key so the effect only re-fires when the run list actually changes.
+  const runIdsKey = runIds.join(",");
+
+  // Stream (or re-stream) whenever the run list or refresh tick changes.
+  useEffect(() => {

Review Comment:
   For genuine streaming (reading chunks as they arrive), useEffect is the 
right tool, it's synchronizing with an external system per React's own 
[docs](https://react.dev/learn/synchronizing-with-effects). 
   
   > Effects let you run some code after rendering so that you can synchronize 
your component with some system outside of React.
   
   I'm reading a ReadableStream chunk by chunk and updating state incrementally 
as data arrives. That's a genuine external side effect with no cleaner React 
primitive for it.
   
   The alternatives either require a separate external store abstraction 
(pushing the complexity elsewhere) or sacrificing progressive rendering 
entirely by buffering the full response with axios. 



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