diff --git a/src/pages/ClusterDetail.tsx b/src/pages/ClusterDetail.tsx index 6ae6f6a..60d2292 100644 --- a/src/pages/ClusterDetail.tsx +++ b/src/pages/ClusterDetail.tsx @@ -11,12 +11,12 @@ import { Shield, Users, TrendingUp, - AlertTriangle, CheckCircle, Clock, Server, Zap } from 'lucide-react' +import { useEffect, useState } from 'react' interface Cluster { id: string @@ -26,39 +26,43 @@ interface Cluster { version: string alerts: string endpoint: string + health: health +} +interface health { + NodesHealthy : number, + NodesTotal : number, + PodsRunning : number, + PodsTotal : number, + Alerts : number, + Warnings : number, + Status :string } -function getCluster(id: string): Cluster | null { - const raw = localStorage.getItem('clusters') - const list: Cluster[] = raw ? JSON.parse(raw) : [] - console.log('Looking for cluster with ID:', id) - console.log('Available clusters:', list) - return list.find((c) => c.id === id) || null -} +async function getCluster(id: string): Promise { + try { + const res = await fetch("http://localhost:8082/cluster_stats?cluster_name=" + id, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `${localStorage.getItem('auth:token') || ''}` + } + }); -function getAllClusters(): Cluster[] { - const raw = localStorage.getItem('clusters') - return raw ? JSON.parse(raw) : [] -} - -function createTestCluster() { - const testCluster: Cluster = { - id: 'test-cluster-1', - name: 'test-cluster', - clusterId: 'test123', - status: 'Healthy', - version: 'v1.28.0', - alerts: '0', - endpoint: 'https://test-cluster.example.com' + if (res.ok) { + const data = await res.json(); + console.log('Cluster data:', data); + return data; // Return the cluster data + } else { + console.error("Error fetching cluster data"); + return null; + } + } catch (e) { + console.error("Error fetching cluster:", e); + return null; } - - const existingClusters = getAllClusters() - const updatedClusters = [...existingClusters, testCluster] - localStorage.setItem('clusters', JSON.stringify(updatedClusters)) - console.log('Created test cluster:', testCluster) - window.location.reload() } + const resourceTypes = [ { name: 'Namespaces', icon: Box, count: 4, color: 'bg-blue-500' }, { name: 'Nodes', icon: Cpu, count: 3, color: 'bg-green-500' }, @@ -102,48 +106,28 @@ const clusterStats = { } export default function ClusterDetail() { - const params = useParams<{ id: string }>() - const cluster = params.id ? getCluster(params.id) : null - const allClusters = getAllClusters() + const [cluster, setCluster] = useState(null); + const [loading, setLoading] = useState(true); + const params = useParams<{ id: string }>(); + + useEffect(() => { + const fetchCluster = async () => { + if (params.id) { + const fetchedCluster = await getCluster(params.id); + setCluster(fetchedCluster); + setLoading(false); + } + }; + fetchCluster(); + }, [params.id]); + + // Add a loading state check + if (loading) { + return
Loading...
; // You can customize the loading screen as needed + } if (!cluster) { - return ( -
-
-
Cluster not found
-
The requested cluster could not be located.
-
Debug: Looking for ID: {params.id}
- - {/* Debug Information */} -
-
Available Clusters:
- {allClusters.length === 0 ? ( -
No clusters found in localStorage
- ) : ( -
- {allClusters.map((c) => ( -
- ID: {c.id} | Name: {c.name} | ClusterID: {c.clusterId} -
- ))} -
- )} -
- -
- - - Back to clusters - -
-
-
- ) + return
Error: Cluster not found.
; } return ( @@ -151,9 +135,9 @@ export default function ClusterDetail() { {/* Header */}
-

{cluster.name}

+

{params.id}

- Cluster ID: {cluster.clusterId} + Cluster ID: {params.id} Status: {cluster.status} @@ -188,7 +172,7 @@ export default function ClusterDetail() {
CPU - {clusterStats.resourceUsage.cpu.used}/{clusterStats.resourceUsage.cpu.total} {clusterStats.resourceUsage.cpu.unit} + {cluster.resourceUsage.cpu.used}/{clusterStats.resourceUsage.cpu.total} {clusterStats.resourceUsage.cpu.unit}