Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

feat: persist agent configuration and policy state to the database for retrieval across restarts.

luoleic47f7f7a86f1
diff --git a/apps/collector/src/database/schema.ts b/apps/collector/src/database/schema.ts
index 4df009c..cd7591d 100644
--- a/apps/collector/src/database/schema.ts
+++ b/apps/collector/src/database/schema.ts
@@ -414,6 +414,17 @@ export const SCHEMA = {
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`,
+
+ // Agent config snapshot - persists agent configuration across restarts
+ AGENT_SNAPSHOTS: `
+ CREATE TABLE IF NOT EXISTS agent_snapshots (
+ backend_id INTEGER PRIMARY KEY,
+ config_json TEXT NOT NULL,
+ policy_state_json TEXT,
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
+ FOREIGN KEY (backend_id) REFERENCES backend_configs(id) ON DELETE CASCADE
+ );
+ `,
} as const;
// Index definitions
@@ -527,6 +538,7 @@ export function getAllSchemaStatements(): string[] {
SCHEMA.RULE_IP_TRAFFIC,
SCHEMA.BACKEND_CONFIGS,
SCHEMA.AGENT_HEARTBEATS,
+ SCHEMA.AGENT_SNAPSHOTS,
SCHEMA.APP_CONFIG,
SCHEMA.SURGE_POLICY_CACHE,
SCHEMA.AUTH_CONFIG,
diff --git a/apps/collector/src/index.ts b/apps/collector/src/index.ts
index f1b33a0..56d0646 100644
--- a/apps/collector/src/index.ts
+++ b/apps/collector/src/index.ts
@@ -60,6 +60,9 @@ async function main() {
console.log('[Main] Initializing database at:', DB_PATH);
db = new StatsDatabase(DB_PATH);
+ // Connect realtimeStore to database for agent config persistence
+ realtimeStore.setDatabase(db);
+
clickHouseCompareService = new ClickHouseCompareService(db);
clickHouseCompareService.start();
diff --git a/apps/collector/src/modules/db/db.ts b/apps/collector/src/modules/db/db.ts
index 3aff61d..0530948 100644
--- a/apps/collector/src/modules/db/db.ts
+++ b/apps/collector/src/modules/db/db.ts
@@ -1068,6 +1068,68 @@ export class StatsDatabase {
getSurgePolicyCacheLastUpdate(backendId: number) { return this.repos.surge.getSurgePolicyCacheLastUpdate(backendId); }
clearSurgePolicyCache(backendId: number) { this.repos.surge.clearSurgePolicyCache(backendId); }
+ // ==================== Agent Config Snapshot (persist across restarts) ====================
+ saveAgentSnapshot(
+ backendId: number,
+ config: { rules: unknown[]; proxies: Record<string, unknown>; providers: Record<string, unknown>; timestamp: number; hash: string },
+ policyState?: { proxies: Record<string, unknown>; providers: Record<string, unknown>; timestamp: number },
+ ): void {
+ const stmt = this.db.prepare(`
+ INSERT INTO agent_snapshots (backend_id, config_json, policy_state_json, updated_at)
+ VALUES (?, ?, ?, CURRENT_TIMESTAMP)
+ ON CONFLICT(backend_id) DO UPDATE SET
+ config_json = excluded.config_json,
+ policy_state_json = excluded.policy_state_json,
+ updated_at = CURRENT_TIMESTAMP
+ `);
+ stmt.run(backendId, JSON.stringify(config), policyState ? JSON.stringify(policyState) : null);
+ }
+
+ loadAgentSnapshot(backendId: number): { config: unknown; policyState?: unknown } | undefined {
+ const stmt = this.db.prepare(`
+ SELECT config_json, policy_state_json
+ FROM agent_snapshots
+ WHERE backend_id = ?
+ LIMIT 1
+ `);
+ const row = stmt.get(backendId) as { config_json: string; policy_state_json: string | null } | undefined;
+ if (!row) return undefined;
+ try {
+ return {
+ config: JSON.parse(row.config_json),
+ policyState: row.policy_state_json ? JSON.parse(row.policy_state_json) : undefined,
+ };
+ } catch {
+ return undefined;
+ }
+ }
+
+ loadAllAgentSnapshots(): Array<{ backendId: number; config: unknown; policyState?: unknown }> {
+ const stmt = this.db.prepare(`
+ SELECT backend_id, config_json, policy_state_json
+ FROM agent_snapshots
+ `);
+ const rows = stmt.all() as Array<{ backend_id: number; config_json: string; policy_state_json: string | null }>;
+ const results: Array<{ backendId: number; config: unknown; policyState?: unknown }> = [];
+ for (const row of rows) {
+ try {
+ results.push({
+ backendId: row.backend_id,
+ config: JSON.parse(row.config_json),
+ policyState: row.policy_state_json ? JSON.parse(row.policy_state_json) : undefined,
+ });
+ } catch {
+ // Skip invalid JSON rows
+ }
+ }
+ return results;
+ }
+
+ deleteAgentSnapshot(backendId: number): void {
+ const stmt = this.db.prepare('DELETE FROM agent_snapshots WHERE backend_id = ?');
+ stmt.run(backendId);
+ }
+
close() {
this.db.close();
}
diff --git a/apps/collector/src/modules/realtime/realtime.store.ts b/apps/collector/src/modules/realtime/realtime.store.ts
index abc4def..0698191 100644
--- a/apps/collector/src/modules/realtime/realtime.store.ts
+++ b/apps/collector/src/modules/realtime/realtime.store.ts
@@ -8,6 +8,7 @@ import type {
StatsSummary,
TrafficTrendPoint,
} from '@neko-master/shared';
+import type { StatsDatabase } from '../db/db.js';
type SummaryDelta = {
upload: number;
@@ -243,12 +244,51 @@ export class RealtimeStore {
private static readonly MAX_RULE_CHAIN_ENTRIES = 50_000;
private static readonly MAX_DEVICE_DETAIL_ENTRIES = 10_000;
+ private db: StatsDatabase | null = null;
+
constructor(maxMinutes = parseInt(process.env.REALTIME_MAX_MINUTES || '180', 10)) {
this.maxMinutes = Number.isFinite(maxMinutes) ? Math.max(30, maxMinutes) : 180;
}
+ // Set database reference for persistence (call once during initialization)
+ setDatabase(db: StatsDatabase): void {
+ this.db = db;
+ // Load existing snapshots from database
+ this.loadAgentSnapshotsFromDB();
+ }
+
+ // Load all agent snapshots from database into memory
+ private loadAgentSnapshotsFromDB(): void {
+ if (!this.db) return;
+ try {
+ const snapshots = this.db.loadAllAgentSnapshots();
+ for (const snap of snapshots) {
+ if (snap.config) {
+ this.agentConfigByBackend.set(snap.backendId, snap.config as AgentGatewayConfig);
+ }
+ if (snap.policyState) {
+ this.agentPolicyStateByBackend.set(snap.backendId, snap.policyState as AgentPolicyState);
+ }
+ }
+ if (snapshots.length > 0) {
+ console.info(`[RealtimeStore] Loaded ${snapshots.length} agent snapshot(s) from database`);
+ }
+ } catch (err) {
+ console.warn('[RealtimeStore] Failed to load agent snapshots:', err);
+ }
+ }
+
setAgentConfig(backendId: number, config: AgentGatewayConfig): void {
this.agentConfigByBackend.set(backendId, config);
+ // Persist to database if available
+ if (this.db) {
+ try {
+ const policyState = this.agentPolicyStateByBackend.get(backendId);
+ this.db.saveAgentSnapshot(backendId, config, policyState);
+ } catch (err) {
+ console.warn('[RealtimeStore] Failed to save agent config snapshot:', err);
+ }
+ }
}
getAgentConfig(backendId: number): AgentGatewayConfig | undefined {
@@ -257,6 +297,17 @@ export class RealtimeStore {
setAgentPolicyState(backendId: number, state: AgentPolicyState): void {
this.agentPolicyStateByBackend.set(backendId, state);
+ // Persist to database if available
+ if (this.db) {
+ try {
+ const config = this.agentConfigByBackend.get(backendId);
+ if (config) {
+ this.db.saveAgentSnapshot(backendId, config, state);
+ }
+ } catch (err) {
+ console.warn('[RealtimeStore] Failed to save agent policy state snapshot:', err);
+ }
+ }
}
getAgentPolicyState(backendId: number): AgentPolicyState | undefined {