Enhance OS detection and update StatTile for distribution display (#115)

* Add function to detect operating system distribution

* Add 'distro' property to system types

* Update subtext in StatTile to show distro instead of platform

* Add Linux distribution detection and architecture normalization

* Add archLabel to SystemInfo interface

* Update subtext to use archLabel instead of arch

* Add distro summarization function and update StatTile

* Refactor distro summary to use tooltip for details

* Refactor host system info detection functions
This commit is contained in:
stla authored and GitHub committed 2026-06-21 19:45:23 +08:00
1 parent 127f2cd2d6
commit e2415210cd
3 files changed
+93 -2

No files matched your search

+75 -1
View File
@@ -6,6 +6,7 @@ import { loadOneBotConfig, saveOneBotConfig } from '@snowluma/onebot/config';
import type { OneBotManager } from '@snowluma/onebot/manager';
import type { OneBotConfig, JsonObject as OneBotJsonObject } from '@snowluma/onebot/types';
import { readRuntimeConfig, updateRuntimeConfig, resolveRuntimeEnvOverrides } from '@snowluma/common/runtime';
import { execSync } from 'child_process';
import { randomBytes } from 'crypto';
import { chmodSync, copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'fs';
import { createServer as createHttpsServer } from 'https';
@@ -118,6 +119,78 @@ async function fetchQqAvatar(uin: string): Promise<{ body: Uint8Array; contentTy
return { body, contentType };
}
// ── Host system info (cached at module level, invariant over process lifetime) ──
function detectDistro(): string {
try {
for (const f of ['/etc/os-release', '/usr/lib/os-release']) {
if (!existsSync(f)) continue;
const raw = readFileSync(f, 'utf8');
const get = (k: string) => { const m = raw.match(new RegExp(`^${k}=("?)(.+?)\\1$`, 'm')); return m?.[2] ?? null; };
const pretty = get('PRETTY_NAME') || get('NAME');
const ver = get('VERSION_ID');
if (pretty) return ver && !pretty.includes(ver) ? `${pretty} ${ver}` : pretty;
}
} catch { /* fall through */ }
if (os.platform() === 'win32') {
try {
const productName = execSync(
'reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" /v ProductName',
{ encoding: 'utf8', timeout: 3000, stdio: 'pipe' },
);
const m = productName.match(/ProductName\s+REG_SZ\s+(.+)/);
let name = m ? m[1].trim() : `Windows ${os.release()}`;
// ProductName is still "Windows 10 Pro" on Windows 11 — check build number.
try {
const buildOut = execSync(
'reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" /v CurrentBuildNumber',
{ encoding: 'utf8', timeout: 3000, stdio: 'pipe' },
);
const bm = buildOut.match(/CurrentBuildNumber\s+REG_SZ\s+(\d+)/);
if (bm && parseInt(bm[1], 10) >= 22000) {
name = name.replace(/^Windows 10/, 'Windows 11');
}
} catch { /* keep name as-is */ }
return name;
} catch { /* fall through */ }
return `Windows ${os.release()}`;
}
// Linux fallback files
for (const [path, prefix] of [
['/etc/alpine-release', 'Alpine Linux '],
['/etc/redhat-release', ''],
['/etc/debian_version', 'Debian '],
] as [string, string][]) {
try {
if (existsSync(path)) return prefix + readFileSync(path, 'utf8').trim();
} catch { /* try next */ }
}
return os.platform();
}
function normalizeArch(arch: string): string {
const map: Record<string, string> = {
loong64: 'LoongArch',
riscv64: 'RISC-V',
mips: 'MIPS',
mipsel: 'MIPS (LE)',
arm: 'ARM',
arm64: 'ARM64',
x64: 'x86_64',
ia32: 'x86',
s390: 'S/390',
s390x: 'S/390x',
ppc: 'PowerPC',
ppc64: 'PowerPC64',
};
return map[arch] ?? arch;
}
const CACHED_DISTRO = detectDistro();
const CACHED_ARCH_LABEL = normalizeArch(os.arch());
export async function initWebUI(
desiredPort: number = 5099,
oneBotManager: OneBotManager,
@@ -413,7 +486,6 @@ export async function initWebUI(
return c.json(await getUpdateInfo(force));
});
// Host system info
let lastCpuTimes: { idle: number; total: number }[] | null = null;
function sampleCpuLoad(): number[] {
const cpus = os.cpus();
@@ -448,7 +520,9 @@ export async function initWebUI(
hostname: os.hostname(),
platform: os.platform(),
arch: os.arch(),
archLabel: CACHED_ARCH_LABEL,
release: os.release(),
distro: CACHED_DISTRO,
uptime: os.uptime(),
processUptime: process.uptime(),
nodeVersion: process.version,
@@ -10,6 +10,7 @@ import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Progress } from '@/components/ui/progress';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Skeleton } from '@/components/ui/skeleton';
import { cn, formatBytes, formatUptime } from '@/lib/utils';
@@ -379,6 +380,20 @@ function DropPlaceholder({ onAdd }: { onAdd: (id: string, x: number, y: number)
// ─────────────── stat tiles (one widget each) ───────────────
function shortDistro(distro: string): string {
let s = distro;
s = s.replace(/^Debian GNU\/Linux /, 'Debian ');
s = s.replace(/^Red Hat Enterprise Linux /, 'RHEL ');
s = s.replace(/^CentOS (?:Linux )?release /, 'CentOS ');
s = s.replace(/^SUSE Linux Enterprise (?:Server |Desktop )?/, 'SUSE ');
s = s.replace(/^Kali GNU\/Linux /, 'Kali ');
s = s.replace(/^Windows Server /, 'Win Svr ');
s = s.replace(/^Windows (\d+)/, 'Win $1');
s = s.replace(/\s*\([^)]+\)/g, '');
s = s.replace(/\s{2,}/g, ' ');
return s.trim();
}
function StatTile({
icon, label, value, subtext, accent = false, to,
}: {
@@ -461,7 +476,7 @@ function StatTileWidget({ id }: { id: string }) {
icon={<Server className="size-5" />}
label="主机名"
value={systemInfo?.hostname ?? '—'}
subtext={systemInfo ? `${systemInfo.platform} · ${systemInfo.arch}` : '加载中'}
subtext={systemInfo ? <Tooltip><TooltipTrigger className="truncate text-left">{shortDistro(systemInfo.distro)} · {systemInfo.archLabel}</TooltipTrigger><TooltipContent>{systemInfo.distro} · {systemInfo.archLabel}</TooltipContent></Tooltip> : '加载中'}
/>
);
case 'stat:uptime':
+2
View File
@@ -203,7 +203,9 @@ export interface SystemInfo {
hostname: string;
platform: string;
arch: string;
archLabel: string;
release: string;
distro: string;
uptime: number;
processUptime: number;
nodeVersion: string;