devmadhuu commented on code in PR #9994:
URL: https://github.com/apache/ozone/pull/9994#discussion_r3135710449


##########
hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/containers/containers.tsx:
##########
@@ -175,6 +178,86 @@ const Containers: React.FC<{}> = () => {
     5: mismatchedReplicaContainerData
   }
 
+  // Mapping tab keys to the backend state parameter for CSV export
+  const tabToExportState: Record<string, string> = {
+    '1': 'MISSING',
+    '2': 'UNDER_REPLICATED',
+    '3': 'OVER_REPLICATED',
+    '4': 'MIS_REPLICATED',
+    '5': 'REPLICA_MISMATCH'
+  };
+
+  // Human-readable labels for the export tooltip
+  const tabToLabel: Record<string, string> = {
+    '1': 'Missing',
+    '2': 'Under-Replicated',
+    '3': 'Over-Replicated',
+    '4': 'Mis-Replicated',
+    '5': 'Mismatched Replicas'
+  };
+
+  const handleExportCsv = useCallback(async () => {
+    const state = tabToExportState[selectedTab];
+    const label = tabToLabel[selectedTab];
+
+    const chunkSize = 500000;
+    let prevKey = 0;
+    let part = 1;
+    let totalExported = 0;
+    const hideMessage = message.loading(`Starting export of ${label} 
containers in chunks...`, 0);
+
+    try {
+      while (true) {
+        const currentLimit = exportLimit === -1 ? chunkSize : 
Math.min(chunkSize, exportLimit - totalExported);
+        if (currentLimit <= 0) break;
+
+        const exportUrl = 
`/api/v1/containers/unhealthy/export?state=${state}&limit=${currentLimit}&prevKey=${prevKey}`;
+        const response = await fetch(exportUrl);
+        
+        if (!response.ok) {
+          throw new Error(`Failed to fetch part ${part}`);
+        }
+
+        const text = await response.text();
+
+        // Extract #last_container_id trailer written by the backend as the 
final line.
+        // This avoids parsing CSV rows and is safe regardless of field 
content.
+        const trailerMatch = text.match(/^#last_container_id:(\d+)$/m);
+        const lastContainerId = trailerMatch ? parseInt(trailerMatch[1], 10) : 
0;
+
+        // Strip the trailer comment before saving so the downloaded file is 
clean CSV.
+        const cleanCsv = text.replace(/^#last_container_id:\d+\n?$/m, '');
+        const blob = new Blob([cleanCsv], { type: 'text/csv' });
+        const url = window.URL.createObjectURL(blob);
+        const a = document.createElement('a');
+        a.href = url;
+        a.download = 
`unhealthy_containers_${state.toLowerCase()}_part${part}.csv`;
+        document.body.appendChild(a);
+        a.click();
+        document.body.removeChild(a);
+        window.URL.revokeObjectURL(url);
+
+        totalExported += currentLimit;

Review Comment:
   `currentLimit` is the number of rows requested, not the number returned. On 
the last chunk, the server returns fewer rows than `currentLimit`, but 
`totalExported` increases by the full `currentLimit`. The success msg will 
always show a wrong number.
   
   ```
   Example: 1.3M containers, "Complete" selected → chunks of 500K, 500K, 300K.
   Message shows: "Successfully exported 1,500,000 containers" — wrong by 200K.
   
   ```
   Am I missing something ?



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/persistence/ContainerHealthSchemaManager.java:
##########
@@ -381,6 +382,50 @@ public List<UnhealthyContainerRecord> 
getUnhealthyContainers(
     }
   }
 
+  /**
+   * Returns a streaming cursor over unhealthy container records for a given 
state.
+   * Caller MUST close the cursor.
+   *
+   * Generated SQL example (50,000 MISSING containers, starting after 
container ID 12345):
+   *
+   * SELECT * FROM unhealthy_containers
+   * WHERE container_state = 'MISSING'
+   *   AND container_id > 12345
+   * ORDER BY container_id ASC
+   * LIMIT 50000
+   *
+   * @param state filter by state (required)
+   * @param limit max records to return, -1 = unlimited
+   * @param prevKey previous container ID to skip, for cursor-based pagination
+   * @return Cursor returning UnhealthyContainersRecord
+   */
+  public Cursor<UnhealthyContainersRecord> getUnhealthyContainersCursor(
+      UnHealthyContainerStates state, int limit, long prevKey) {
+    DSLContext dslContext = containerSchemaDefinition.getDSLContext();
+    org.jooq.SelectQuery<UnhealthyContainersRecord> query = 
dslContext.selectFrom(UNHEALTHY_CONTAINERS).getQuery();
+
+    // WHERE container_state = ?
+    
query.addConditions(UNHEALTHY_CONTAINERS.CONTAINER_STATE.eq(state.toString()));
+
+    if (prevKey > 0) {
+      // AND container_id > ?  (cursor-based pagination)
+      query.addConditions(UNHEALTHY_CONTAINERS.CONTAINER_ID.gt(prevKey));
+    }
+
+    // ORDER BY container_id ASC — matches composite index (state, 
container_id),
+    // so Derby walks it in order with no sort step.
+    query.addOrderBy(UNHEALTHY_CONTAINERS.CONTAINER_ID.asc());
+
+    if (limit > 0) {
+      query.addLimit(limit);

Review Comment:
   Can you explain the behavior when limit == 0, Are we silently ignoring ? or 
returning some msg ?



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