|
1 | | -import { useState } from "react"; |
| 1 | +import { useEffect, useState } from "react"; |
2 | 2 | import { Sidebar } from "./Sidebar"; |
3 | 3 | import { Header } from "./Header"; |
4 | 4 | import { Overview } from "./Overview"; |
@@ -39,73 +39,128 @@ export function Dashboard() { |
39 | 39 | const [selectedPodName, setSelectedPodName] = useState<string>(""); |
40 | 40 |
|
41 | 41 | // Move pods state to Dashboard level |
42 | | - const [pods, setPods] = useState<Pod[]>([ |
43 | | - { |
44 | | - name: "frontend-app-7d4b8c9f8d-xyz12", |
45 | | - image: "nginx:1.21", |
46 | | - labels: { app: "frontend", version: "v1.2.0" }, |
47 | | - node: "worker-node-1", |
48 | | - status: "Running", |
49 | | - cpuUsage: "45m", |
50 | | - memoryUsage: "128Mi", |
51 | | - age: "2d", |
52 | | - ready: "1/1", |
53 | | - restarts: 0, |
54 | | - ip: "10.244.1.15", |
55 | | - }, |
56 | | - { |
57 | | - name: "frontend-app-7d4b8c9f8d-abc34", |
58 | | - image: "nginx:1.21", |
59 | | - labels: { app: "frontend", version: "v1.2.0" }, |
60 | | - node: "worker-node-2", |
61 | | - status: "Running", |
62 | | - cpuUsage: "38m", |
63 | | - memoryUsage: "115Mi", |
64 | | - age: "2d", |
65 | | - ready: "1/1", |
66 | | - restarts: 0, |
67 | | - ip: "10.244.2.18", |
68 | | - }, |
69 | | - { |
70 | | - name: "backend-api-5f6a7b8c9d-def56", |
71 | | - image: "node:18-alpine", |
72 | | - labels: { app: "backend", tier: "api" }, |
73 | | - node: "worker-node-1", |
74 | | - status: "Running", |
75 | | - cpuUsage: "120m", |
76 | | - memoryUsage: "256Mi", |
77 | | - age: "5d", |
78 | | - ready: "1/1", |
79 | | - restarts: 1, |
80 | | - ip: "10.244.1.22", |
81 | | - }, |
82 | | - { |
83 | | - name: "redis-cache-8e9f0a1b2c-ghi78", |
84 | | - image: "redis:7-alpine", |
85 | | - labels: { app: "redis", role: "cache" }, |
86 | | - node: "worker-node-3", |
87 | | - status: "Running", |
88 | | - cpuUsage: "25m", |
89 | | - memoryUsage: "64Mi", |
90 | | - age: "1d", |
91 | | - ready: "1/1", |
92 | | - restarts: 0, |
93 | | - ip: "10.244.3.9", |
94 | | - }, |
95 | | - { |
96 | | - name: "database-migration-1a2b3c4d5e-jkl90", |
97 | | - image: "postgres:14", |
98 | | - labels: { job: "migration", app: "database" }, |
99 | | - node: "worker-node-2", |
100 | | - status: "Pending", |
101 | | - cpuUsage: "0m", |
102 | | - memoryUsage: "0Mi", |
103 | | - age: "30m", |
104 | | - ready: "0/1", |
105 | | - restarts: 0, |
106 | | - ip: "N/A", |
107 | | - }, |
108 | | - ]); |
| 42 | + |
| 43 | + // Lgsi pod data |
| 44 | + const [podsToUse, setPods] = useState<Pod[]>([]); |
| 45 | + const [podsFetchSuccess, setPodsFetchSuccess] = useState(false); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + const settingserviceApiUrl = import.meta.env.VITE_SETTING_SERVICE_API_URL; |
| 49 | + if (!settingserviceApiUrl) return; |
| 50 | + fetch(`${settingserviceApiUrl}/api/v1/metrics`) |
| 51 | + .then(res => res.json()) |
| 52 | + .then(data => { |
| 53 | + // Filter for containers only |
| 54 | + const containers = Array.isArray(data) |
| 55 | + ? data.filter( |
| 56 | + (item: any) => |
| 57 | + item.component === "container" && |
| 58 | + item.metric_type === "ContainerInfo" && |
| 59 | + item.value && |
| 60 | + item.value.value |
| 61 | + ) |
| 62 | + : []; |
| 63 | + if (containers.length > 0) { |
| 64 | + // Map API data to Pod[] |
| 65 | + const mappedPods = containers.map((item: any, idx: number) => { |
| 66 | + const val = item.value.value; |
| 67 | + return { |
| 68 | + name: (val.names && val.names[0]) || val.id || `pod-${idx}`, |
| 69 | + image: val.image || "", |
| 70 | + labels: item.labels || {}, |
| 71 | + node: (val.state && (val.state.node_name || val.state.hostname)) || (val.config.Hostname) || "", |
| 72 | + status: (val.state && (val.state.status || val.state.Status)) || "", |
| 73 | + cpuUsage: val.stats?.CpuTotalUsage || "", |
| 74 | + memoryUsage: val.stats?.MemoryUsage || "", |
| 75 | + age: item.timestamp || "", |
| 76 | + ready: "", // Not available in this API, leave blank or compute if possible |
| 77 | + restarts: 0, // Not available, default to 0 |
| 78 | + ip: item.labels?.ip || "", |
| 79 | + }; |
| 80 | + }); |
| 81 | + setPods(mappedPods); |
| 82 | + setPodsFetchSuccess(true); |
| 83 | + console.log("✅ Pods API (metrics) fetched:", mappedPods); |
| 84 | + } else { |
| 85 | + setPodsFetchSuccess(false); |
| 86 | + } |
| 87 | + }) |
| 88 | + .catch(e => { |
| 89 | + setPodsFetchSuccess(false); |
| 90 | + console.error("❌ Pods API (metrics) fetch failed:", e); |
| 91 | + }); |
| 92 | + }, []); |
| 93 | + |
| 94 | + |
| 95 | + const pods = podsFetchSuccess && podsToUse.length > 0 |
| 96 | + ? podsToUse |
| 97 | + : [ |
| 98 | + { |
| 99 | + name: "frontend-app-7d4b8c9f8d-xyz12", |
| 100 | + image: "nginx:1.21", |
| 101 | + labels: { app: "frontend", version: "v1.2.0" }, |
| 102 | + node: "worker-node-1", |
| 103 | + status: "Running", |
| 104 | + cpuUsage: "45m", |
| 105 | + memoryUsage: "128Mi", |
| 106 | + age: "2d", |
| 107 | + ready: "1/1", |
| 108 | + restarts: 0, |
| 109 | + ip: "10.244.1.15", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "frontend-app-7d4b8c9f8d-abc34", |
| 113 | + image: "nginx:1.21", |
| 114 | + labels: { app: "frontend", version: "v1.2.0" }, |
| 115 | + node: "worker-node-2", |
| 116 | + status: "Running", |
| 117 | + cpuUsage: "38m", |
| 118 | + memoryUsage: "115Mi", |
| 119 | + age: "2d", |
| 120 | + ready: "1/1", |
| 121 | + restarts: 0, |
| 122 | + ip: "10.244.2.18", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "backend-api-5f6a7b8c9d-def56", |
| 126 | + image: "node:18-alpine", |
| 127 | + labels: { app: "backend", tier: "api" }, |
| 128 | + node: "worker-node-1", |
| 129 | + status: "Running", |
| 130 | + cpuUsage: "120m", |
| 131 | + memoryUsage: "256Mi", |
| 132 | + age: "5d", |
| 133 | + ready: "1/1", |
| 134 | + restarts: 1, |
| 135 | + ip: "10.244.1.22", |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "redis-cache-8e9f0a1b2c-ghi78", |
| 139 | + image: "redis:7-alpine", |
| 140 | + labels: { app: "redis", role: "cache" }, |
| 141 | + node: "worker-node-3", |
| 142 | + status: "Running", |
| 143 | + cpuUsage: "25m", |
| 144 | + memoryUsage: "64Mi", |
| 145 | + age: "1d", |
| 146 | + ready: "1/1", |
| 147 | + restarts: 0, |
| 148 | + ip: "10.244.3.9", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "database-migration-1a2b3c4d5e-jkl90", |
| 152 | + image: "postgres:14", |
| 153 | + labels: { job: "migration", app: "database" }, |
| 154 | + node: "worker-node-2", |
| 155 | + status: "Pending", |
| 156 | + cpuUsage: "0m", |
| 157 | + memoryUsage: "0Mi", |
| 158 | + age: "30m", |
| 159 | + ready: "0/1", |
| 160 | + restarts: 0, |
| 161 | + ip: "N/A", |
| 162 | + }, |
| 163 | + ]; |
109 | 164 |
|
110 | 165 | // Calculate running pods count |
111 | 166 | const runningPodsCount = pods.filter( |
|
0 commit comments