diff --git a/dashboard/src/lib/api.ts b/dashboard/src/lib/api.ts
index bce2d1b..d18bb3a 100644
--- a/dashboard/src/lib/api.ts
+++ b/dashboard/src/lib/api.ts
@@ -1,38 +1,13 @@
-const STORAGE_KEY = 'workstation.apiBaseUrl'
-
function envApiBaseUrl(): string {
return import.meta.env.VITE_API_BASE_URL ?? ''
}
-export function normalizeApiBaseUrl(url: string): string {
+function normalizeApiBaseUrl(url: string): string {
return url.trim().replace(/\/+$/, '')
}
-export function getApiBaseUrl(): string {
- try {
- const saved = localStorage.getItem(STORAGE_KEY)
- if (saved !== null && saved !== '') return normalizeApiBaseUrl(saved)
- } catch {
- // localStorage 不可用时退回环境变量
- }
- return envApiBaseUrl()
-}
-
-export function setApiBaseUrl(url: string): void {
- const normalized = normalizeApiBaseUrl(url)
- try {
- if (normalized === '') {
- localStorage.removeItem(STORAGE_KEY)
- } else {
- localStorage.setItem(STORAGE_KEY, normalized)
- }
- } catch {
- // 忽略写入失败
- }
-}
-
export function resolveApiUrl(path: string): string {
- const base = normalizeApiBaseUrl(getApiBaseUrl())
+ const base = normalizeApiBaseUrl(envApiBaseUrl())
return `${base}${path}`
}
diff --git a/dashboard/src/pages/Home.tsx b/dashboard/src/pages/Home.tsx
index 4ed3402..c3be64a 100644
--- a/dashboard/src/pages/Home.tsx
+++ b/dashboard/src/pages/Home.tsx
@@ -9,6 +9,8 @@ interface CpuStats {
}
interface MemoryStats {
+ total: number
+ used: number
usagePercent: number
}
@@ -26,6 +28,8 @@ interface SystemStats {
os: OsStats
}
+const REFRESH_INTERVAL = 5000
+
function formatUptime(seconds: number) {
const d = Math.floor(seconds / 86400)
const h = Math.floor((seconds % 86400) / 3600)
@@ -35,6 +39,14 @@ function formatUptime(seconds: number) {
return `${m} 分钟`
}
+function formatBytes(bytes: number) {
+ if (!bytes || bytes <= 0) return '0 B'
+ const units = ['B', 'KB', 'MB', 'GB', 'TB']
+ const i = Math.min(units.length - 1, Math.floor(Math.log(bytes) / Math.log(1024)))
+ const value = bytes / 1024 ** i
+ return `${value >= 10 ? Math.round(value) : value.toFixed(1)} ${units[i]}`
+}
+
function UsageBar({ percent }: { percent: number }) {
return (
@@ -78,15 +90,31 @@ const iconProps = {
export default function Home() {
const [stats, setStats] = useState
(null)
const [error, setError] = useState(false)
+ const [lastUpdated, setLastUpdated] = useState(null)
useEffect(() => {
- apiFetch('/api/system/stats')
- .then((res) => {
+ let cancelled = false
+
+ async function load() {
+ try {
+ const res = await apiFetch('/api/system/stats')
if (!res.ok) throw new Error('Failed to fetch stats')
- return res.json()
- })
- .then((data: SystemStats) => setStats(data))
- .catch(() => setError(true))
+ const data: SystemStats = await res.json()
+ if (cancelled) return
+ setStats(data)
+ setError(false)
+ setLastUpdated(new Date())
+ } catch {
+ if (!cancelled) setError(true)
+ }
+ }
+
+ load()
+ const timer = setInterval(load, REFRESH_INTERVAL)
+ return () => {
+ cancelled = true
+ clearInterval(timer)
+ }
}, [])
return (
@@ -107,92 +135,99 @@ export default function Home() {
-
系统状态
-
- {error
- ? '无法获取系统信息,请确认后端服务已启动,或在「设置」中配置服务器 API 地址'
- : stats
- ? '实时运行状态一览'
- : '正在获取系统信息…'}
-
+
+
+
系统状态
+
+ {error
+ ? '无法获取系统信息,请确认后端服务已启动'
+ : stats
+ ? `实时运行状态一览,每 ${REFRESH_INTERVAL / 1000} 秒自动刷新`
+ : '正在获取系统信息…'}
+
+
+ {stats && lastUpdated && (
+
+ 最后更新于 {lastUpdated.toLocaleTimeString()}
+
+ )}
+
{stats ? (
-
-
-
-
- }
- label="CPU 占用率"
- >
-
- {stats.cpu.usagePercent}%
-
-
-
-
-
-
-
- }
- label="内存占用率"
- >
-
- {stats.memory.usagePercent}%
-
-
-
-
-
-
-
- }
- label="系统与运行环境"
- >
-
+
+
+
{stats.os.name} {stats.os.version} · {stats.os.arch}
-
- Java {stats.os.javaVersion}
-
-
-
-
-
- }
- label="开机时间"
- >
-
- {formatUptime(stats.os.uptimeSeconds)}
-
+ Java {stats.os.javaVersion}
+
+
+
+
+
+
+ }
+ label="CPU 占用率"
+ >
+
+ {stats.cpu.usagePercent}%
+
+
+
+ {stats.cpu.model || '未知型号'} · {stats.cpu.cores} 核
+
+
+
+
+
+
+ }
+ label="内存占用率"
+ >
+
+ {stats.memory.usagePercent}%
+
+
+
+ {formatBytes(stats.memory.used)} / {formatBytes(stats.memory.total)}
+
+
+
+
+
+
+ }
+ label="开机时间"
+ >
+
+ {formatUptime(stats.os.uptimeSeconds)}
+
+ 持续运行
+
+
) : (
- {error ? '无法获取系统信息,请确认后端服务已启动,或在「设置」中配置服务器 API 地址' : '正在获取系统信息…'}
+ {error ? '无法获取系统信息,请确认后端服务已启动' : '正在获取系统信息…'}
)}
diff --git a/dashboard/src/pages/Settings.tsx b/dashboard/src/pages/Settings.tsx
index 73e0cd0..167cf61 100644
--- a/dashboard/src/pages/Settings.tsx
+++ b/dashboard/src/pages/Settings.tsx
@@ -1,99 +1,14 @@
-import { useState } from 'react'
-import { getApiBaseUrl, normalizeApiBaseUrl, resolveApiUrl, setApiBaseUrl } from '../lib/api'
-
-type TestStatus = 'idle' | 'testing' | 'ok' | 'fail'
-
export default function Settings() {
- const [value, setValue] = useState(getApiBaseUrl())
- const [saved, setSaved] = useState(getApiBaseUrl())
- const [status, setStatus] = useState
('idle')
- const [message, setMessage] = useState('')
-
- const dirty = normalizeApiBaseUrl(value) !== saved
-
- function handleSave() {
- setApiBaseUrl(value)
- setSaved(getApiBaseUrl())
- setStatus('idle')
- setMessage('')
- }
-
- async function handleTest() {
- setStatus('testing')
- setMessage('')
- const base = normalizeApiBaseUrl(value)
- const oldBase = getApiBaseUrl()
- if (base !== oldBase) setApiBaseUrl(base)
- try {
- const res = await fetch(resolveApiUrl('/api/system/stats'))
- if (!res.ok) throw new Error(`HTTP ${res.status}`)
- const data = await res.json()
- const label = (data.os?.name as string | undefined) ?? '后端'
- setStatus('ok')
- setMessage(`${label} 连接正常`)
- } catch {
- setStatus('fail')
- setMessage('连接失败,请检查地址是否正确以及后端服务是否已启动')
- } finally {
- if (base !== oldBase) setApiBaseUrl(oldBase)
- }
- }
-
return (
设置
系统偏好与账号配置
-
-
服务器 API 地址
-
- 配置前端连接后端服务的 API 基础地址,留空则使用环境变量或本地代理
+
+
⚙️
+
+ 暂无设置项
-
-
-
-
- 当前生效地址:{saved === '' ? '默认(本地代理)' : saved}
-
-
- {message && (
-
- {message}
-
- )}
-
-
-
-
-
)