From e5f256f4dc0a10b7da0a0d78525e6e677d733e3b Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 30 Jul 2026 09:57:31 +0800 Subject: [PATCH] feat(webui): report account storage update progress Surface account storage preparation and background history progress through the existing live connection status channel. Clearly distinguish unavailable accounts from accounts that remain fully usable during maintenance. Show processed totals, completion percentage, estimated remaining time, and retry states in both the global notice and account overview. Preserve existing connection update filtering to avoid unnecessary refreshes. --- .../core/src/webui/connection-snapshot.ts | 28 ++++++++ packages/core/src/webui/server.ts | 18 +---- .../tests/webui-connection-diff-loop.test.ts | 65 +++++++++++++++++- .../src/components/layout/main-layout.tsx | 5 +- .../src/components/pages/overview-page.tsx | 67 +++++++++++++++++-- packages/webui/src/router/app-layout.tsx | 35 +++++++++- packages/webui/src/types.ts | 11 +++ 7 files changed, 202 insertions(+), 27 deletions(-) create mode 100644 packages/core/src/webui/connection-snapshot.ts diff --git a/packages/core/src/webui/connection-snapshot.ts b/packages/core/src/webui/connection-snapshot.ts new file mode 100644 index 00000000..3c15cb2e --- /dev/null +++ b/packages/core/src/webui/connection-snapshot.ts @@ -0,0 +1,28 @@ +import type { AccountConnections } from '@snowluma/onebot/manager'; + +export function comparableConnectionSnapshot(snapshot: unknown): unknown { + if (!Array.isArray(snapshot)) return snapshot; + return (snapshot as AccountConnections[]).map((account) => ({ + uin: account.uin, + nickname: account.nickname, + adapters: Array.isArray(account.adapters) + ? account.adapters.map((adapter) => ({ + name: adapter.name, + kind: adapter.kind, + status: adapter.status, + lastErrorAt: adapter.lastErrorAt, + })) + : [], + databaseMigration: account.databaseMigration + ? { + phase: account.databaseMigration.phase, + usable: account.databaseMigration.usable, + processed: account.databaseMigration.processed, + total: account.databaseMigration.total, + progress: account.databaseMigration.progress, + estimatedRemainingSeconds: account.databaseMigration.estimatedRemainingSeconds, + error: account.databaseMigration.error, + } + : undefined, + })); +} diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index be2625dc..b87fb63d 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -49,6 +49,7 @@ import { sseResponse } from './sse-response'; import { bindStateStream } from './state-stream'; import type { StateBus, StateResource } from './state-bus'; import { startConnectionDiffLoop } from './connection-diff-loop'; +import { comparableConnectionSnapshot } from './connection-snapshot'; import { describeTrustProxy, makeClientIpResolver, parseTrustProxy } from './client-ip'; import { findAvailablePort } from './port'; import { @@ -467,22 +468,7 @@ export async function initWebUI( startConnectionDiffLoop({ bus: listener.stateBus, getSnapshot: () => oneBotManager.getConnectionStatuses(), - pickComparable: (snap) => { - if (!Array.isArray(snap)) return snap; - return (snap as Array<{ uin: string; nickname?: string; adapters?: unknown[] }>).map((acc) => ({ - uin: acc.uin, - nickname: acc.nickname, - // Empty fallback when `adapters` is not an array — never the - // raw value, which could re-introduce volatile fields verbatim - // if the snapshot shape ever drifts. - adapters: Array.isArray(acc.adapters) - ? acc.adapters.map((a: unknown) => { - const o = a as { name?: string; kind?: string; status?: string; lastErrorAt?: number }; - return { name: o.name, kind: o.kind, status: o.status, lastErrorAt: o.lastErrorAt }; - }) - : [], - })); - }, + pickComparable: comparableConnectionSnapshot, intervalMs: 500, }); } diff --git a/packages/core/tests/webui-connection-diff-loop.test.ts b/packages/core/tests/webui-connection-diff-loop.test.ts index 3e1dbc47..86acbeb0 100644 --- a/packages/core/tests/webui-connection-diff-loop.test.ts +++ b/packages/core/tests/webui-connection-diff-loop.test.ts @@ -1,6 +1,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { StateBus } from '../src/webui/state-bus'; import { startConnectionDiffLoop } from '../src/webui/connection-diff-loop'; +import { comparableConnectionSnapshot } from '../src/webui/connection-snapshot'; describe('startConnectionDiffLoop', () => { beforeEach(() => { vi.useFakeTimers(); }); @@ -68,6 +69,64 @@ describe('startConnectionDiffLoop', () => { handle.dispose(); }); + it('the connection projector ignores adapter detail changes', () => { + const baseline = [{ + uin: '1', + nickname: 'account', + adapters: [{ + name: 'webhook', + kind: 'httpClient', + status: 'ok', + detail: '上次推送 14:00:00', + }], + }]; + const changed = [{ + uin: '1', + nickname: 'account', + adapters: [{ + name: 'webhook', + kind: 'httpClient', + status: 'ok', + detail: '上次推送 14:00:01', + }], + }]; + + expect(comparableConnectionSnapshot(changed)) + .toEqual(comparableConnectionSnapshot(baseline)); + }); + + it('publishes when database migration progress changes', () => { + const bus = new StateBus(); + const seen: string[] = []; + bus.subscribe((resource) => seen.push(resource)); + let processed = 100; + const handle = startConnectionDiffLoop({ + bus, + getSnapshot: () => [{ + uin: '1', + nickname: 'account', + adapters: [], + databaseMigration: { + phase: 'migrating', + usable: true, + processed, + total: 1000, + progress: processed / 1000, + estimatedRemainingSeconds: 90, + }, + }], + pickComparable: comparableConnectionSnapshot, + intervalMs: 500, + }); + + vi.advanceTimersByTime(500); + processed = 200; + vi.advanceTimersByTime(500); + + expect(seen).toEqual(['connections']); + handle.dispose(); + }); + it('dispose() stops further ticks, no publish even after a change', () => { const bus = new StateBus(); const seen: string[] = []; @@ -103,9 +162,9 @@ describe('startConnectionDiffLoop', () => { uin: acc.uin, adapters: Array.isArray(acc.adapters) ? acc.adapters.map((a: unknown) => { - const o = a as { name?: string; kind?: string; status?: string }; - return { name: o.name, kind: o.kind, status: o.status }; - }) + const o = a as { name?: string; kind?: string; status?: string }; + return { name: o.name, kind: o.kind, status: o.status }; + }) : acc.adapters, })); }, diff --git a/packages/webui/src/components/layout/main-layout.tsx b/packages/webui/src/components/layout/main-layout.tsx index 84e6617e..cdb5fb3e 100644 --- a/packages/webui/src/components/layout/main-layout.tsx +++ b/packages/webui/src/components/layout/main-layout.tsx @@ -15,10 +15,11 @@ import { cn } from '@/lib/utils'; interface MainLayoutProps { status: string; onLogout: () => void; + notice?: ReactNode; children: ReactNode; } -export function MainLayout({ status, onLogout, children }: MainLayoutProps) { +export function MainLayout({ status, onLogout, notice, children }: MainLayoutProps) { const isMobile = !useMediaQuery('(min-width: 768px)'); const { appearance } = useTheme(); const customBg = appearance.background.type !== 'none'; @@ -123,6 +124,8 @@ export function MainLayout({ status, onLogout, children }: MainLayoutProps) { /> )} + {!kiosk && notice} +
diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index f7500d64..743d2570 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -15,7 +15,14 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Skeleton } from '@/components/ui/skeleton'; import { cn, formatBytes, formatUptime } from '@/lib/utils'; import type { AppPath } from '@/router'; -import type { AdapterStatus, AdapterStatusLevel, LogEntry, LogLevel, UiLayoutItem } from '@/types'; +import type { + AccountDatabaseMigration, + AdapterStatus, + AdapterStatusLevel, + LogEntry, + LogLevel, + UiLayoutItem, +} from '@/types'; import { useApi } from '@/lib/api'; import { useAppState } from '@/contexts/AppStateContext'; import { useSession } from '@/contexts/SessionContext'; @@ -875,9 +882,10 @@ function ConnectionsBlock({ config }: { config: ConnectionsConfig }) { {acc.nickname || acc.uin} {acc.uin}
- {acc.adapters.length === 0 ? ( -

未配置任何协议端点

- ) : ( + {acc.databaseMigration && acc.databaseMigration.phase !== 'complete' && ( + + )} + {acc.adapters.length > 0 ? (
{acc.adapters.map((adp) => (
@@ -899,7 +907,9 @@ function ConnectionsBlock({ config }: { config: ConnectionsConfig }) {
))}
- )} + ) : acc.databaseMigration?.usable !== false ? ( +

未配置任何协议端点

+ ) : null} ))} @@ -909,6 +919,53 @@ function ConnectionsBlock({ config }: { config: ConnectionsConfig }) { ); } +function DatabaseMigrationProgress({ migration }: { migration: AccountDatabaseMigration }) { + const percentage = migration.progress === null + ? null + : Math.round(migration.progress * 100); + const title = migration.phase === 'preparing' + ? '正在准备数据库,账号暂不可用' + : migration.phase === 'failed' + ? (migration.usable ? '历史数据迁移失败,账号功能可用' : '数据库准备失败,账号暂不可用') + : '正在后台迁移历史数据,账号功能可用'; + const eta = formatMigrationEta(migration.estimatedRemainingSeconds); + + return ( +
+
+ {title} + {percentage !== null && {percentage}%} +
+ {percentage !== null && } +
+ {migration.total !== null && ( + + 已处理 {migration.processed.toLocaleString()} / {migration.total.toLocaleString()} 条 + + )} + {migration.phase !== 'failed' && {eta}} + {migration.phase === 'failed' && migration.error && {migration.error}} +
+
+ ); +} + +function formatMigrationEta(seconds: number | null): string { + if (seconds === null) return '剩余时间估算中'; + if (seconds <= 0) return '即将完成'; + if (seconds < 60) return `预计剩余 ${seconds} 秒`; + const minutes = Math.ceil(seconds / 60); + if (minutes < 60) return `预计剩余 ${minutes} 分钟`; + const hours = Math.floor(minutes / 60); + const remainder = minutes % 60; + return `预计剩余 ${hours} 小时${remainder > 0 ? ` ${remainder} 分钟` : ''}`; +} + // ─────────────── static widgets (note / link / account) ─────────────── function NoteWidget({ config }: { config: NoteConfig }) { diff --git a/packages/webui/src/router/app-layout.tsx b/packages/webui/src/router/app-layout.tsx index 65a8b1c6..df770481 100644 --- a/packages/webui/src/router/app-layout.tsx +++ b/packages/webui/src/router/app-layout.tsx @@ -1,4 +1,4 @@ -import { Suspense, useCallback, useEffect, useRef, useState } from 'react'; +import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Outlet, useNavigate, useRouterState } from '@tanstack/react-router'; import { useTheme } from '@/contexts/ThemeContext'; import { useApi } from '@/lib/api'; @@ -219,6 +219,37 @@ export function AppLayout() { session.onLogoutComplete(); }, [api, session]); + const migrationNotice = useMemo(() => { + const active = connections.filter((account) => { + const phase = account.databaseMigration?.phase; + return phase && phase !== 'complete'; + }); + if (active.length === 0) return null; + const preparing = active.filter((account) => account.databaseMigration?.phase === 'preparing').length; + const migrating = active.filter((account) => account.databaseMigration?.phase === 'migrating').length; + const failedUsable = active.filter((account) => ( + account.databaseMigration?.phase === 'failed' && account.databaseMigration.usable + )).length; + const failedUnavailable = active.filter((account) => ( + account.databaseMigration?.phase === 'failed' && !account.databaseMigration.usable + )).length; + const parts = [ + ...(preparing > 0 ? [`${preparing} 个账号正在准备数据库,暂不可用`] : []), + ...(migrating > 0 ? [`${migrating} 个账号正在后台迁移,功能可用`] : []), + ...(failedUsable > 0 ? [`${failedUsable} 个可用账号迁移失败,将自动重试`] : []), + ...(failedUnavailable > 0 ? [`${failedUnavailable} 个账号数据库准备失败,暂不可用并将自动重试`] : []), + ]; + return ( +
+ 账号数据库状态: + {parts.join(';')}。请查看概览详情。 +
+ ); + }, [connections]); + return ( - + {/* Routes use `lazyRouteComponent` (router/index.tsx) for code-splitting, which suspends until the chunk is fetched. The chrome (sidebar / top bar) stays mounted across this diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index 6b9ed4d2..33f21fc8 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -38,10 +38,21 @@ export interface AdapterStatus { lastErrorAt?: number; } +export interface AccountDatabaseMigration { + phase: 'preparing' | 'migrating' | 'complete' | 'failed'; + usable: boolean; + processed: number; + total: number | null; + progress: number | null; + estimatedRemainingSeconds: number | null; + error?: string; +} + export interface AccountConnections { uin: string; nickname: string; adapters: AdapterStatus[]; + databaseMigration?: AccountDatabaseMigration; } export interface HookProcessInfo {