betodealmeida commented on code in PR #35478:
URL: https://github.com/apache/superset/pull/35478#discussion_r2429509638


##########
superset-frontend/src/components/StreamingExportModal/useStreamingExport.ts:
##########
@@ -0,0 +1,366 @@
+/**
+ * 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 { useState, useCallback, useRef, useEffect } from 'react';
+import { SupersetClient } from '@superset-ui/core';
+import { ExportStatus, StreamingProgress } from './StreamingExportModal';
+
+interface UseStreamingExportOptions {
+  onComplete?: (downloadUrl: string, filename: string) => void;
+  onError?: (error: string) => void;
+}
+
+interface StreamingExportPayload {
+  [key: string]: unknown;
+}
+
+interface StreamingExportParams {
+  url: string;
+  payload: StreamingExportPayload;
+  filename?: string;
+  exportType: 'csv' | 'xlsx';
+  expectedRows?: number;
+}
+
+const NEWLINE_BYTE = 10; // '\n' character code
+
+const createFetchRequest = async (
+  _url: string,
+  payload: StreamingExportPayload,
+  filename: string,
+  _exportType: string,
+  expectedRows: number | undefined,
+  signal: AbortSignal,
+): Promise<RequestInit> => {
+  const headers: Record<string, string> = {
+    'Content-Type': 'application/x-www-form-urlencoded',
+  };
+
+  // Get CSRF token using SupersetClient
+  const csrfToken = await SupersetClient.getCSRFToken();
+  if (csrfToken) {
+    headers['X-CSRFToken'] = csrfToken;
+  }
+
+  // Build form data - if payload has client_id, it's SQL Lab export
+  // Otherwise it's a chart export with form_data
+  const formParams: Record<string, string> = {
+    filename,
+    expected_rows: expectedRows?.toString() || '',
+  };
+
+  if ('client_id' in payload) {
+    // SQL Lab export - pass client_id directly
+    formParams.client_id = String(payload.client_id);
+  } else {
+    // Chart export - wrap payload in form_data
+    formParams.form_data = JSON.stringify(payload);
+  }
+
+  return {
+    method: 'POST',
+    headers,
+    body: new URLSearchParams(formParams),
+    signal,
+    credentials: 'same-origin',
+  };
+};
+
+const countNewlines = (value: Uint8Array): number =>
+  value.filter(byte => byte === NEWLINE_BYTE).length;
+
+const createBlob = (
+  chunks: Uint8Array[],
+  receivedLength: number,
+  exportType: string,
+): Blob => {
+  const completeData = new Uint8Array(receivedLength);
+  let position = 0;
+  for (const chunk of chunks) {
+    completeData.set(chunk, position);
+    position += chunk.length;
+  }
+
+  const mimeType =
+    exportType === 'csv'
+      ? 'text/csv;charset=utf-8'
+      : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
+
+  return new Blob([completeData], { type: mimeType });
+};
+
+export const useStreamingExport = (options: UseStreamingExportOptions = {}) => 
{
+  const [progress, setProgress] = useState<StreamingProgress>({
+    rowsProcessed: 0,
+    totalRows: undefined,
+    totalSize: 0,
+    speed: 0,
+    mbPerSecond: 0,
+    elapsedTime: 0,
+    status: ExportStatus.STREAMING,
+  });
+  const [retryCount, setRetryCount] = useState(0);
+  const abortControllerRef = useRef<AbortController | null>(null);
+  const lastExportParamsRef = useRef<StreamingExportParams | null>(null);
+  const currentBlobUrlRef = useRef<string | null>(null);
+  const isExportingRef = useRef(false);
+
+  const updateProgress = useCallback((updates: Partial<StreamingProgress>) => {
+    setProgress(prev => ({ ...prev, ...updates }));
+  }, []);
+
+  const executeExport = useCallback(
+    async (params: StreamingExportParams) => {
+      const { url, payload, filename, exportType, expectedRows } = params;
+
+      abortControllerRef.current = new AbortController();
+
+      updateProgress({
+        rowsProcessed: 0,
+        totalRows: expectedRows,
+        totalSize: 0,
+        speed: 0,
+        mbPerSecond: 0,
+        elapsedTime: 0,
+        status: ExportStatus.STREAMING,
+        filename,
+      });
+
+      try {
+        const defaultFilename = `export.${exportType}`;
+        const finalFilename = filename || defaultFilename;
+
+        const fetchOptions = await createFetchRequest(
+          url,
+          payload,
+          finalFilename,
+          exportType,
+          expectedRows,
+          abortControllerRef.current.signal,
+        );
+
+        const response = await fetch(url, fetchOptions);
+
+        if (!response.ok) {
+          throw new Error(
+            `Export failed: ${response.status} ${response.statusText}`,
+          );
+        }
+
+        if (!response.body) {
+          throw new Error('Response body is not available for streaming');
+        }
+
+        const reader = response.body.getReader();
+        const chunks: Uint8Array[] = [];
+        let receivedLength = 0;
+        let rowsProcessed = 0;
+        let hasError = false;
+
+        // eslint-disable-next-line no-constant-condition
+        while (true) {
+          // eslint-disable-next-line no-await-in-loop
+          const { done, value } = await reader.read();
+

Review Comment:
   I think this makes sense here, since the reads can't be done in parallel. If 
that's the case [it should be fine to disable the 
rule](https://eslint.org/docs/latest/rules/no-await-in-loop#when-not-to-use-it).



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