codeant-ai-for-open-source[bot] commented on code in PR #42786:
URL: https://github.com/apache/superset/pull/42786#discussion_r3718036961


##########
superset-frontend/src/utils/downloadUtils.ts:
##########
@@ -56,16 +77,91 @@ function waitForChartsToLoad(
 }
 
 /**
- * When DASHBOARD_VIRTUALIZATION is enabled, forces all lazy-loaded
- * charts to render and waits for them to finish loading.
- * Returns true if virtualization was active (caller must restore it).
+ * Poll until none of the given row elements contain a `.loading` spinner.
+ * Scoped to just those rows (rather than the whole container, like
+ * waitForChartsToLoad above) so a chart stuck in an earlier batch doesn't
+ * force every later batch to also burn its full timeout re-checking that
+ * same stale spinner. Resolves (doesn't reject) either way; a straggler
+ * here is still caught by the final whole-container check afterwards.
  */
-export async function forceLoadAllCharts(container: Element): Promise<boolean> 
{
+function waitForRowsToLoad(rows: Element[], timeoutMs: number): Promise<void> {
+  return new Promise(resolve => {
+    const startTime = Date.now();
+    const check = () => {
+      const stillLoading = rows.some(row => row.querySelector('.loading'));
+      if (!stillLoading || Date.now() - startTime > timeoutMs) {
+        resolve();
+        return;
+      }
+      setTimeout(check, 500);
+    };
+    setTimeout(check, 1000);
+  });
+}
+
+function getRowElements(container: Element): Element[] {
+  return Array.from(container.querySelectorAll('[data-row-id]'));
+}
+
+function getRowId(row: Element): string | null {
+  return row.getAttribute('data-row-id');
+}
+
+function chunk<T>(items: T[], size: number): T[][] {
+  const batches: T[][] = [];
+  for (let i = 0; i < items.length; i += size) {
+    batches.push(items.slice(i, i + size));
+  }
+  return batches;
+}
+
+/**
+ * When DASHBOARD_VIRTUALIZATION is enabled, forces lazy-loaded charts to
+ * render in small batches (rather than all at once) and waits for them to
+ * finish loading. Returns true if virtualization was active (caller must
+ * restore it).
+ */
+export async function forceLoadAllCharts(
+  container: Element,
+  onProgress?: (progress: ForceLoadProgress) => void,
+): Promise<boolean> {
   const useVirtualization = isFeatureEnabled(
     FeatureFlag.DashboardVirtualization,
   );
   if (useVirtualization) {
-    window.dispatchEvent(new Event(FORCE_IN_VIEW_EVENT));
+    const rowElements = getRowElements(container);
+    const rowBatches = rowElements.length
+      ? chunk(rowElements, FORCE_RENDER_BATCH_SIZE)
+      : [];
+
+    if (rowBatches.length <= 1) {
+      // Nothing to batch (no rows found, or everything fits in one batch):
+      // force everything into view in a single pass, same as before batching.
+      window.dispatchEvent(new Event(FORCE_IN_VIEW_EVENT));
+    } else {
+      addInfoToast(
+        t('Preparing %(count)s charts for export. This may take a moment.', {
+          count: rowElements.length,
+        }),
+      );
+      // eslint-disable-next-line no-restricted-syntax -- batches must be
+      // dispatched sequentially so the query burst is actually staggered.
+      for (const [index, batch] of rowBatches.entries()) {
+        const rowIds = batch
+          .map(getRowId)
+          .filter((id): id is string => id !== null);
+        window.dispatchEvent(
+          new CustomEvent(FORCE_IN_VIEW_EVENT, { detail: { rowIds } }),
+        );
+        // eslint-disable-next-line no-await-in-loop -- see above
+        await waitForRowsToLoad(batch, BATCH_LOAD_TIMEOUT_MS);
+        onProgress?.({
+          loadedBatches: index + 1,
+          totalBatches: rowBatches.length,
+        });

Review Comment:
   **Suggestion:** Each batch can consume the full 10-second timeout when any 
row remains loading, and the subsequent whole-container wait can consume 
another 60 seconds. Because these waits are sequential and there is no overall 
export deadline, a large dashboard with several permanently loading batches can 
block image/PDF generation for many minutes despite the per-batch timeout. Add 
an overall deadline or stop the final wait once the batch timeouts have already 
established that charts are still stalled. [performance]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Dashboard image/PDF export can remain blocked for several minutes.
   - ⚠️ Large dashboards with stalled charts receive no bounded overall export 
deadline.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=77ae3d952edc432ca0868952b682e95a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=77ae3d952edc432ca0868952b682e95a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/utils/downloadUtils.ts
   **Line:** 157:161
   **Comment:**
        *Performance: Each batch can consume the full 10-second timeout when 
any row remains loading, and the subsequent whole-container wait can consume 
another 60 seconds. Because these waits are sequential and there is no overall 
export deadline, a large dashboard with several permanently loading batches can 
block image/PDF generation for many minutes despite the per-batch timeout. Add 
an overall deadline or stop the final wait once the batch timeouts have already 
established that charts are still stalled.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42786&comment_hash=a3ff2a6c05087dadff3c342b968a6400f22c51a7c66fc1d70bec8c4ecece2a7e&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42786&comment_hash=a3ff2a6c05087dadff3c342b968a6400f22c51a7c66fc1d70bec8c4ecece2a7e&reaction=dislike'>👎</a>



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to