diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index 02c7e939..daea3c8d 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -22,12 +22,14 @@ import { reconcileLayoutItems, useLayout } from '@/contexts/LayoutContext'; import { useMediaQuery } from '@/hooks/use-media-query'; import { CONFIGURABLE_WIDGETS, GRID_COLS, MOBILE_WIDGET_IDS, mobileHeightOf, - parseAlertsConfig, parseSessionsConfig, widgetLabel, - type AlertsConfig, type SessionsConfig, + parseAlertsConfig, parseConnectionsConfig, parseHostConfig, parseSessionsConfig, widgetLabel, + type AlertsConfig, type ConnectionsConfig, type HostConfig, type SessionsConfig, } from '@/lib/dashboard-layout'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { DashboardGrid, type GridCoord } from '@/components/pages/dashboard-grid'; -import { AlertsConfigForm, SessionsConfigForm } from '@/components/pages/widget-config-forms'; +import { + AlertsConfigForm, ConnectionsConfigForm, HostConfigForm, SessionsConfigForm, +} from '@/components/pages/widget-config-forms'; function qqAvatarUrl(uin: string) { return `/avatar/${encodeURIComponent(uin)}`; @@ -36,9 +38,9 @@ function qqAvatarUrl(uin: string) { function renderWidget(block: UiLayoutItem): ReactNode { if (block.id.startsWith('stat:')) return ; switch (block.id) { - case 'connections': return ; + case 'connections': return ; case 'alerts': return ; - case 'host': return ; + case 'host': return ; case 'sessions': return ; default: return null; } @@ -180,6 +182,8 @@ export function OverviewPage() { {configBlock?.id === 'alerts' && setBlockConfig('alerts', c)} />} {configBlock?.id === 'sessions' && setBlockConfig('sessions', c)} />} + {configBlock?.id === 'host' && setBlockConfig('host', c)} />} + {configBlock?.id === 'connections' && setBlockConfig('connections', c)} />} @@ -385,8 +389,11 @@ function StatTileWidget({ id }: { id: string }) { // ─────────────── host resources ─────────────── -function HostBlock() { +const HOST_GRID_COLS: Record = { 1: 'lg:grid-cols-1', 2: 'lg:grid-cols-2', 3: 'lg:grid-cols-3' }; + +function HostBlock({ config }: { config: HostConfig }) { const { systemInfo, refreshSystem } = useAppState(); + const panelCount = [config.cpu, config.memory, config.runtime].filter(Boolean).length; return ( @@ -402,84 +409,90 @@ function HostBlock() { 刷新 - + {/* CPU */} -
-
-
- - CPU 使用率 + {config.cpu && ( +
+
+
+ + CPU 使用率 +
+ + {systemInfo ? `${systemInfo.cpu.average.toFixed(1)}%` : '—'} +
- - {systemInfo ? `${systemInfo.cpu.average.toFixed(1)}%` : '—'} - -
- -

+ +

负载: {systemInfo ? systemInfo.cpu.loadAvg.map((v) => v.toFixed(2)).join(' / ') : '—'} -

- {systemInfo && systemInfo.cpu.perCore.length > 0 && ( -
- {systemInfo.cpu.perCore.map((p, i) => ( -
-
-
- ))} -
- )} -
+

+ {systemInfo && systemInfo.cpu.perCore.length > 0 && ( +
+ {systemInfo.cpu.perCore.map((p, i) => ( +
+
+
+ ))} +
+ )} +
+ )} {/* Memory */} -
-
-
- - 内存使用 + {config.memory && ( +
+
+
+ + 内存使用 +
+ + {systemInfo ? `${systemInfo.memory.usagePercent.toFixed(1)}%` : '—'} + +
+ +
+ 已用 {systemInfo ? formatBytes(systemInfo.memory.used) : '—'} + 共 {systemInfo ? formatBytes(systemInfo.memory.total) : '—'}
- - {systemInfo ? `${systemInfo.memory.usagePercent.toFixed(1)}%` : '—'} -
- -
- 已用 {systemInfo ? formatBytes(systemInfo.memory.used) : '—'} - 共 {systemInfo ? formatBytes(systemInfo.memory.total) : '—'} -
-
+ )} {/* Runtime */} -
-
-
- - 运行进程 + {config.runtime && ( +
+
+
+ + 运行进程 +
+ + {systemInfo ? `PID ${systemInfo.runtime.pid}` : '—'} +
- - {systemInfo ? `PID ${systemInfo.runtime.pid}` : '—'} - + {!systemInfo ? ( +
+ + +
+ ) : ( +
+
+ RSS + {formatBytes(systemInfo.runtime.rss)} +
+
+ 堆内存 + + {formatBytes(systemInfo.runtime.heapUsed)} / {formatBytes(systemInfo.runtime.heapTotal)} + +
+
+ 外部内存 + {formatBytes(systemInfo.runtime.external)} +
+
+ )}
- {!systemInfo ? ( -
- - -
- ) : ( -
-
- RSS - {formatBytes(systemInfo.runtime.rss)} -
-
- 堆内存 - - {formatBytes(systemInfo.runtime.heapUsed)} / {formatBytes(systemInfo.runtime.heapTotal)} - -
-
- 外部内存 - {formatBytes(systemInfo.runtime.external)} -
-
- )} -
+ )} ); @@ -561,25 +574,50 @@ const ADAPTER_KIND_LABEL: Record = { httpServer: 'HTTP 服务端', httpClient: 'HTTP 上报', wsServer: 'WS 服务端', wsClient: 'WS 客户端', }; -function ConnectionsBlock() { +// Worst-status-first ordering (down → warn → disabled → ok). +const CONN_STATUS_RANK: Record = { down: 0, warn: 1, disabled: 2, ok: 3 }; + +function ConnectionsBlock({ config }: { config: ConnectionsConfig }) { const { connections } = useAppState(); + const list = useMemo(() => { + const f = config.filter.trim().toLowerCase(); + // Optionally drop healthy adapters, then accounts left with none. + let arr = connections.map((acc) => ({ + ...acc, + adapters: config.onlyIssues ? acc.adapters.filter((a) => a.status !== 'ok') : acc.adapters, + })); + if (config.onlyIssues) arr = arr.filter((acc) => acc.adapters.length > 0); + if (f) arr = arr.filter((acc) => (acc.nickname ?? '').toLowerCase().includes(f) || acc.uin.includes(f)); + if (config.sort === 'name') { + arr = [...arr].sort((a, b) => (a.nickname || a.uin).localeCompare(b.nickname || b.uin)); + } else if (config.sort === 'status') { + const worst = (acc: typeof arr[number]) => + acc.adapters.reduce((m, a) => Math.min(m, CONN_STATUS_RANK[a.status]), 99); + arr = [...arr].sort((a, b) => worst(a) - worst(b)); + } + return arr; + }, [connections, config.filter, config.onlyIssues, config.sort]); + + const filtered = config.filter.trim() !== '' || config.onlyIssues; return ( OneBot 连接 - 各账号协议端点的实时连接状态 + + 各账号协议端点的实时连接状态{config.onlyIssues ? ' · 仅异常' : ''}{config.filter.trim() ? ` · 筛选:${config.filter.trim()}` : ''} + - {connections.length === 0 ? ( + {list.length === 0 ? (
-

暂无已接入的账号实例

+

{filtered ? '无匹配的连接' : '暂无已接入的账号实例'}

) : (
- {connections.map((acc) => ( + {list.map((acc) => (
{acc.nickname || acc.uin} diff --git a/packages/webui/src/components/pages/widget-config-forms.tsx b/packages/webui/src/components/pages/widget-config-forms.tsx index bc53d222..719bf4b0 100644 --- a/packages/webui/src/components/pages/widget-config-forms.tsx +++ b/packages/webui/src/components/pages/widget-config-forms.tsx @@ -1,6 +1,9 @@ import { Input } from '@/components/ui/input'; +import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { cn } from '@/lib/utils'; -import { ALL_LOG_LEVELS, parseAlertsConfig, parseSessionsConfig } from '@/lib/dashboard-layout'; +import { + ALL_LOG_LEVELS, parseAlertsConfig, parseConnectionsConfig, parseHostConfig, parseSessionsConfig, +} from '@/lib/dashboard-layout'; // Per-widget config forms, shown inside a dialog opened from a dashboard // widget's edit-overlay gear. `config` is the widget's opaque block.config; @@ -83,3 +86,72 @@ export function SessionsConfigForm({ config, onChange }: FormProps) {
); } + +const HOST_PANELS: { key: 'cpu' | 'memory' | 'runtime'; label: string }[] = [ + { key: 'cpu', label: 'CPU 使用率' }, + { key: 'memory', label: '内存使用' }, + { key: 'runtime', label: '运行进程' }, +]; + +export function HostConfigForm({ config, onChange }: FormProps) { + const c = parseHostConfig(config); + return ( +
+ 显示的指标 + {HOST_PANELS.map((p) => ( + + ))} +
+ ); +} + +const CONN_SORTS: { key: 'default' | 'name' | 'status'; label: string }[] = [ + { key: 'default', label: '默认' }, + { key: 'name', label: '名称' }, + { key: 'status', label: '异常优先' }, +]; + +export function ConnectionsConfigForm({ config, onChange }: FormProps) { + const c = parseConnectionsConfig(config); + return ( +
+
+ 排序 +
+ {CONN_SORTS.map((s) => ( + + ))} +
+
+ + +
+ ); +} diff --git a/packages/webui/src/lib/dashboard-layout.ts b/packages/webui/src/lib/dashboard-layout.ts index caccd75d..6ac574bd 100644 --- a/packages/webui/src/lib/dashboard-layout.ts +++ b/packages/webui/src/lib/dashboard-layout.ts @@ -123,12 +123,18 @@ export const ALL_LOG_LEVELS: LogLevel[] = ['trace', 'debug', 'info', 'success', export interface AlertsConfig { count: number; levels: LogLevel[] } export interface SessionsConfig { sort: 'recent' | 'uin' | 'nickname'; filter: string } +/** Host widget: which resource sub-panels to show. */ +export interface HostConfig { cpu: boolean; memory: boolean; runtime: boolean } +/** Connections widget: filter + sort + issues-only. */ +export interface ConnectionsConfig { filter: string; onlyIssues: boolean; sort: 'default' | 'name' | 'status' } export const DEFAULT_ALERTS_CONFIG: AlertsConfig = { count: 5, levels: ['warn', 'error'] }; export const DEFAULT_SESSIONS_CONFIG: SessionsConfig = { sort: 'recent', filter: '' }; +export const DEFAULT_HOST_CONFIG: HostConfig = { cpu: true, memory: true, runtime: true }; +export const DEFAULT_CONNECTIONS_CONFIG: ConnectionsConfig = { filter: '', onlyIssues: false, sort: 'default' }; /** Widgets that expose a config form (gear) in the layout editor. */ -export const CONFIGURABLE_WIDGETS = new Set(['alerts', 'sessions']); +export const CONFIGURABLE_WIDGETS = new Set(['alerts', 'sessions', 'host', 'connections']); export function parseAlertsConfig(config: Record | undefined): AlertsConfig { const c = config ?? {}; @@ -149,3 +155,27 @@ export function parseSessionsConfig(config: Record | undefined) const filter = typeof c.filter === 'string' ? c.filter.slice(0, 100) : ''; return { sort, filter }; } + +export function parseHostConfig(config: Record | undefined): HostConfig { + const c = config ?? {}; + const bool = (v: unknown, d: boolean) => (typeof v === 'boolean' ? v : d); + const cfg = { + cpu: bool(c.cpu, true), + memory: bool(c.memory, true), + runtime: bool(c.runtime, true), + }; + // Never let every panel be hidden — that leaves an empty card; fall back to all. + return cfg.cpu || cfg.memory || cfg.runtime ? cfg : { ...DEFAULT_HOST_CONFIG }; +} + +export function parseConnectionsConfig(config: Record | undefined): ConnectionsConfig { + const c = config ?? {}; + const sort = c.sort === 'name' || c.sort === 'status' || c.sort === 'default' + ? c.sort + : DEFAULT_CONNECTIONS_CONFIG.sort; + return { + filter: typeof c.filter === 'string' ? c.filter.slice(0, 100) : '', + onlyIssues: typeof c.onlyIssues === 'boolean' ? c.onlyIssues : false, + sort, + }; +}