diff --git a/src/app/api/file.ts b/src/app/api/file.ts index de06894..caceba5 100644 --- a/src/app/api/file.ts +++ b/src/app/api/file.ts @@ -13,9 +13,21 @@ export async function read( await fs.access(filePath); } catch { await fs.mkdir(dirPath, { recursive: true }); - await fs.writeFile(filePath, JSON.stringify(template, null, 2), "utf-8"); + await fs.writeFile(filePath, JSON.stringify(template(), null, 2), "utf-8"); } const content = await fs.readFile(filePath, "utf-8"); return JSON.parse(content); } + +export async function write( + val: T, + fp: string, + ...filePaths: string[] +) { + const filePath = path.join(process.cwd(), fp, ...filePaths); + const dirPath = path.dirname(filePath); + + await fs.mkdir(dirPath, { recursive: true }); + await fs.writeFile(filePath, JSON.stringify(val, null, 2), "utf-8"); +} diff --git a/src/app/api/status/route.ts b/src/app/api/status/route.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/app/overview/page.tsx b/src/app/overview/page.tsx index 325a6b6..719dec7 100644 --- a/src/app/overview/page.tsx +++ b/src/app/overview/page.tsx @@ -16,6 +16,9 @@ import { } from "lucide-react"; import { Progress } from "@/components/ui/progress"; import Link from "next/link"; +import { getLiveNodes, getNodes } from "@/core/client/node"; +import { useEffect, useState } from "react"; +import { Node, NodeLiveData } from "@/lib/types"; const recentDeployments = [ { @@ -86,34 +89,13 @@ const applications = [ }, ]; -const nodes = [ - { - name: "node-01", - ip: "10.0.1.10", - cpu: 72, - memory: 68, - containers: 8, - status: "running" as const, - }, - { - name: "node-02", - ip: "10.0.1.11", - cpu: 45, - memory: 52, - containers: 6, - status: "running" as const, - }, - { - name: "node-03", - ip: "10.0.1.12", - cpu: 89, - memory: 85, - containers: 12, - status: "running" as const, - }, -]; - export default function OverviewPage() { + const [nodes, setNodes] = useState([]); + + useEffect(() => { + getLiveNodes().then(setNodes); + }, []); + return ( {/* Stats Grid */} @@ -246,7 +228,7 @@ export default function OverviewPage() { >
- + {node.name}
@@ -256,20 +238,25 @@ export default function OverviewPage() {
CPU - {node.cpu}% -
- -
- Memory - {node.memory}% + {node.liveData.cpu}%
+
+ Memory + {node.liveData.memory}% +
+
Containers - {node.containers} + + {node.liveData.containers} +
diff --git a/src/core/client/docker.ts b/src/core/client/docker.ts new file mode 100644 index 0000000..60103c8 --- /dev/null +++ b/src/core/client/docker.ts @@ -0,0 +1,12 @@ +"use server"; + +import axios from "axios"; +import { Node } from "@/lib/types"; + +export async function getDocker(node: Node): Promise<{ version: string }> { + return await ( + await axios.get(`http://${node.ip}:3000/api/get-docker`, { + headers: { Authorization: `ApiKey ${node.key}` }, + }) + ).data; +} diff --git a/src/core/client/docker.tsx b/src/core/client/docker.tsx deleted file mode 100644 index dcffbf8..0000000 --- a/src/core/client/docker.tsx +++ /dev/null @@ -1,14 +0,0 @@ -"use server"; - -import axios from "axios"; - -export async function getDocker( - node_ip: string, - key: string -): Promise<{ version: string }> { - return await ( - await axios.get(`http://${node_ip}:3000/api/get-docker`, { - headers: { Authorization: `ApiKey ${key}` }, - }) - ).data; -} diff --git a/src/core/client/node.ts b/src/core/client/node.ts new file mode 100644 index 0000000..aada4c6 --- /dev/null +++ b/src/core/client/node.ts @@ -0,0 +1,32 @@ +"use server"; + +import { read, write } from "@/app/api/file"; +import { Node, NodeLiveData } from "@/lib/types"; +import axios from "axios"; + +export async function getNodes(): Promise { + return await read(() => [], "data", "nodes.json"); +} + +export async function setNodes(nodes: Node[]) { + await write(nodes, "data", "nodes.json"); +} + +export async function getNode(node_id: string): Promise { + const nodes = await read(() => [], "data", "nodes.json"); + return nodes.find((v) => v.node_id === node_id); +} + +export async function getLiveNodes(): Promise { + const nodes = await read(() => [], "data", "nodes.json"); + return await Promise.all(nodes.map(getLiveNode)); +} + +export async function getLiveNode(node: Node): Promise { + const data = await ( + await axios.get(`http://${node.ip}:3000/api/status`, { + headers: { Authorization: `ApiKey ${node.key}` }, + }) + ).data; + return { ...node, liveData: data.liveData }; +} diff --git a/src/core/docker.tsx b/src/core/docker.ts similarity index 100% rename from src/core/docker.tsx rename to src/core/docker.ts diff --git a/src/lib/types.ts b/src/lib/types.ts new file mode 100644 index 0000000..abb3b06 --- /dev/null +++ b/src/lib/types.ts @@ -0,0 +1,20 @@ +export interface Node { + name: string; + node_id: string; + ip: string; + key: string; +} + +export interface NodeLiveData { + name: string; + node_id: string; + ip: string; + key: string; + liveData: { + status: "running" | "pending" | "error"; + cpu: number; + memory: number; + disk: number; + containers: number; + }; +}