Copilot commented on code in PR #341:
URL: 
https://github.com/apache/kvrocks-controller/pull/341#discussion_r2284997084


##########
webui/src/app/ui/migrationDialog.tsx:
##########
@@ -0,0 +1,484 @@
+/*
+ * 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.
+ */
+
+"use client";
+
+import React, { useState } from "react";
+import {
+    Dialog,
+    DialogTitle,
+    DialogContent,
+    DialogActions,
+    Button,
+    Typography,
+    FormControl,
+    FormLabel,
+    RadioGroup,
+    FormControlLabel,
+    Radio,
+    Box,
+    Chip,
+    CircularProgress,
+    Alert,
+    Snackbar,
+    TextField,
+    Switch,
+    alpha,
+    useTheme,
+} from "@mui/material";
+import MoveUpIcon from "@mui/icons-material/MoveUp";
+import StorageIcon from "@mui/icons-material/Storage";
+import DeviceHubIcon from "@mui/icons-material/DeviceHub";
+import { migrateSlot } from "@/app/lib/api";
+
+interface Shard {
+    index: number;
+    nodes: any[];

Review Comment:
   The `nodes` property uses `any[]` type which provides no type safety. 
Consider defining a proper interface for node objects to improve type safety 
and code maintainability.
   ```suggestion
   // Define the Node interface. Update properties as needed to match actual 
node structure.
   interface Node {
       // Example properties; replace with actual node properties as 
appropriate.
       id: string;
       address: string;
       role: string;
       [key: string]: any; // Remove or refine as you learn more about node 
structure.
   }
   
   interface Shard {
       index: number;
       nodes: Node[];
   ```



##########
webui/src/app/namespaces/[namespace]/clusters/[cluster]/page.tsx:
##########
@@ -181,6 +194,63 @@ export default function Cluster({ params }: { params: { 
namespace: string; clust
         fetchData();
     }, [namespace, cluster, router]);
 
+    const refreshShardData = async () => {

Review Comment:
   The `refreshShardData` function duplicates the shard data processing logic 
from the main `fetchData` function. Consider extracting the common processing 
logic into a shared helper function to reduce code duplication.



##########
webui/src/app/ui/migrationDialog.tsx:
##########
@@ -0,0 +1,484 @@
+/*
+ * 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.
+ */
+
+"use client";
+
+import React, { useState } from "react";
+import {
+    Dialog,
+    DialogTitle,
+    DialogContent,
+    DialogActions,
+    Button,
+    Typography,
+    FormControl,
+    FormLabel,
+    RadioGroup,
+    FormControlLabel,
+    Radio,
+    Box,
+    Chip,
+    CircularProgress,
+    Alert,
+    Snackbar,
+    TextField,
+    Switch,
+    alpha,
+    useTheme,
+} from "@mui/material";
+import MoveUpIcon from "@mui/icons-material/MoveUp";
+import StorageIcon from "@mui/icons-material/Storage";
+import DeviceHubIcon from "@mui/icons-material/DeviceHub";
+import { migrateSlot } from "@/app/lib/api";
+
+interface Shard {
+    index: number;
+    nodes: any[];
+    slotRanges: string[];
+    migratingSlot: string;
+    importingSlot: string;
+    targetShardIndex: number;
+    nodeCount: number;
+    hasSlots: boolean;
+    hasMigration: boolean;
+    hasImporting: boolean;
+}
+
+interface MigrationDialogProps {
+    open: boolean;
+    onClose: () => void;
+    namespace: string;
+    cluster: string;
+    shards: Shard[];
+    onSuccess: () => void;
+}
+
+export const MigrationDialog: React.FC<MigrationDialogProps> = ({
+    open,
+    onClose,
+    namespace,
+    cluster,
+    shards,
+    onSuccess,
+}) => {
+    const [targetShardIndex, setTargetShardIndex] = useState<number>(-1);
+    const [slotNumber, setSlotNumber] = useState<string>("");
+    const [slotOnly, setSlotOnly] = useState<boolean>(false);
+    const [loading, setLoading] = useState(false);
+    const [error, setError] = useState<string>("");
+    const theme = useTheme();
+
+    const availableTargetShards = shards.filter((shard) => shard.hasSlots);
+
+    const validateSlotInput = (input: string): { isValid: boolean; error?: 
string } => {
+        if (!input.trim()) {
+            return { isValid: false, error: "Please enter a slot number or 
range" };
+        }
+
+        // Check if it's a range (contains dash)
+        if (input.includes("-")) {
+            const parts = input.split("-");
+            if (parts.length !== 2) {
+                return {
+                    isValid: false,
+                    error: "Invalid range format. Use format: start-end (e.g., 
100-200)",
+                };
+            }
+
+            const start = parseInt(parts[0].trim());
+            const end = parseInt(parts[1].trim());
+
+            if (isNaN(start) || isNaN(end)) {
+                return {
+                    isValid: false,
+                    error: "Both start and end of range must be valid numbers",
+                };
+            }
+
+            if (start < 0 || end > 16383 || start > 16383 || end < 0) {
+                return { isValid: false, error: "Slot numbers must be between 
0 and 16383" };
+            }
+
+            if (start > end) {
+                return {
+                    isValid: false,
+                    error: "Start slot must be less than or equal to end slot",
+                };
+            }
+
+            return { isValid: true };
+        } else {
+            const slot = parseInt(input.trim());
+            if (isNaN(slot) || slot < 0 || slot > 16383) {
+                return { isValid: false, error: "Slot number must be between 0 
and 16383" };
+            }
+            return { isValid: true };
+        }
+    };
+
+    const handleMigration = async () => {
+        if (targetShardIndex === -1 || !slotNumber.trim()) {
+            setError("Please select a target shard and enter a slot number or 
range");
+            return;
+        }
+
+        // Validate slot input
+        const validation = validateSlotInput(slotNumber);
+        if (!validation.isValid) {
+            setError(validation.error || "Invalid slot input");
+            return;
+        }
+
+        setLoading(true);
+        setError("");
+
+        try {
+            const result = await migrateSlot(
+                namespace,
+                cluster,
+                targetShardIndex,
+                slotNumber.trim(),
+                slotOnly
+            );
+
+            if (result) {
+                setError(result);
+            } else {
+                onSuccess();
+                onClose();
+                resetForm();
+            }
+        } catch (err) {
+            setError("An unexpected error occurred during migration");

Review Comment:
   The generic error message doesn't provide useful information to users. 
Consider logging the actual error details and providing a more specific error 
message based on the error type.
   ```suggestion
               // Log the actual error for debugging
               console.error("Migration error:", err);
               // Show a more specific error message if available
               if (err && typeof err === "object" && "message" in err && typeof 
(err as any).message === "string") {
                   setError((err as any).message);
               } else {
                   setError("An unexpected error occurred during migration");
               }
   ```



##########
webui/src/app/ui/migrationDialog.tsx:
##########
@@ -0,0 +1,484 @@
+/*
+ * 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.
+ */
+
+"use client";
+
+import React, { useState } from "react";
+import {
+    Dialog,
+    DialogTitle,
+    DialogContent,
+    DialogActions,
+    Button,
+    Typography,
+    FormControl,
+    FormLabel,
+    RadioGroup,
+    FormControlLabel,
+    Radio,
+    Box,
+    Chip,
+    CircularProgress,
+    Alert,
+    Snackbar,
+    TextField,
+    Switch,
+    alpha,
+    useTheme,
+} from "@mui/material";
+import MoveUpIcon from "@mui/icons-material/MoveUp";
+import StorageIcon from "@mui/icons-material/Storage";
+import DeviceHubIcon from "@mui/icons-material/DeviceHub";
+import { migrateSlot } from "@/app/lib/api";
+
+interface Shard {
+    index: number;
+    nodes: any[];
+    slotRanges: string[];
+    migratingSlot: string;
+    importingSlot: string;
+    targetShardIndex: number;
+    nodeCount: number;
+    hasSlots: boolean;
+    hasMigration: boolean;
+    hasImporting: boolean;
+}
+
+interface MigrationDialogProps {
+    open: boolean;
+    onClose: () => void;
+    namespace: string;
+    cluster: string;
+    shards: Shard[];
+    onSuccess: () => void;
+}
+
+export const MigrationDialog: React.FC<MigrationDialogProps> = ({
+    open,
+    onClose,
+    namespace,
+    cluster,
+    shards,
+    onSuccess,
+}) => {
+    const [targetShardIndex, setTargetShardIndex] = useState<number>(-1);
+    const [slotNumber, setSlotNumber] = useState<string>("");
+    const [slotOnly, setSlotOnly] = useState<boolean>(false);
+    const [loading, setLoading] = useState(false);
+    const [error, setError] = useState<string>("");
+    const theme = useTheme();
+
+    const availableTargetShards = shards.filter((shard) => shard.hasSlots);
+
+    const validateSlotInput = (input: string): { isValid: boolean; error?: 
string } => {
+        if (!input.trim()) {
+            return { isValid: false, error: "Please enter a slot number or 
range" };
+        }
+
+        // Check if it's a range (contains dash)
+        if (input.includes("-")) {
+            const parts = input.split("-");
+            if (parts.length !== 2) {
+                return {
+                    isValid: false,
+                    error: "Invalid range format. Use format: start-end (e.g., 
100-200)",
+                };
+            }
+
+            const start = parseInt(parts[0].trim());
+            const end = parseInt(parts[1].trim());
+
+            if (isNaN(start) || isNaN(end)) {
+                return {
+                    isValid: false,
+                    error: "Both start and end of range must be valid numbers",
+                };
+            }
+
+            if (start < 0 || end > 16383 || start > 16383 || end < 0) {

Review Comment:
   The condition `start > 16383 || end < 0` is redundant as it's already 
covered by `start < 0 || end > 16383`. This can be simplified to improve 
readability.
   ```suggestion
               if (start < 0 || end > 16383) {
   ```



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