A modern and elegant dashboard for network traffic visualization and analysis.
feat: Add `includeSummary` parameter to WebSocket stats, round `useStableTimeRange` to the nearest minute, and refine WebSocket detail subscription conditions.
luolei09b863e4087b
diff --git a/apps/collector/src/modules/websocket/websocket.server.ts b/apps/collector/src/modules/websocket/websocket.server.tsindex 2442f09..4117bc8 100644--- a/apps/collector/src/modules/websocket/websocket.server.ts+++ b/apps/collector/src/modules/websocket/websocket.server.ts@@ -12,6 +12,7 @@ export interface WebSocketMessage {start?: string;end?: string;minPushIntervalMs?: number;+ includeSummary?: boolean;includeTrend?: boolean;trendMinutes?: number;trendBucketMinutes?: number;@@ -87,6 +88,7 @@ interface ClientInfo {backendId: number | null; // null means use active backendrange: ClientRange;minPushIntervalMs: number;+ includeSummary: boolean;lastSentAt: number;trend: ClientTrend;deviceDetail: ClientDeviceDetail;@@ -204,6 +206,7 @@ export class StatsWebSocketServer {backendId: null,range: {},minPushIntervalMs: 0,+ includeSummary: true,lastSentAt: 0,trend: null,deviceDetail: null,@@ -265,6 +268,14 @@ export class StatsWebSocketServer {}}+ if (msg.includeSummary !== undefined) {+ const nextIncludeSummary = msg.includeSummary !== false;+ if (nextIncludeSummary !== clientInfo.includeSummary) {+ clientInfo.includeSummary = nextIncludeSummary;+ changed = true;+ }+ }+if (msg.includeTrend !== undefined ||msg.trendMinutes !== undefined ||@@ -960,7 +971,7 @@ export class StatsWebSocketServer {clientInfo.includeRuleChainFlow,clientInfo.domainsPage,clientInfo.ipsPage,- true,+ clientInfo.includeSummary,);if (!stats) return;@@ -1021,6 +1032,7 @@ export class StatsWebSocketServer {const trendKey = clientInfo.trend? `${clientInfo.trend.minutes}|${clientInfo.trend.bucketMinutes}`: '';+ const includeSummaryKey = clientInfo.includeSummary ? '1' : '0';const deviceDetailKey = clientInfo.deviceDetail? `${clientInfo.deviceDetail.sourceIP}|${clientInfo.deviceDetail.limit}`: '';@@ -1037,7 +1049,7 @@ export class StatsWebSocketServer {const ipsPageKey = clientInfo.ipsPage? `${clientInfo.ipsPage.offset}|${clientInfo.ipsPage.limit}|${clientInfo.ipsPage.sortBy || ''}|${clientInfo.ipsPage.sortOrder || ''}|${clientInfo.ipsPage.search || ''}`: '';- const cacheKey = `${resolvedBackendId}|${clientInfo.range.start || ''}|${clientInfo.range.end || ''}|${trendKey}|${deviceDetailKey}|${proxyDetailKey}|${ruleDetailKey}|${ruleChainFlowKey}|${domainsPageKey}|${ipsPageKey}`;+ const cacheKey = `${resolvedBackendId}|${clientInfo.range.start || ''}|${clientInfo.range.end || ''}|${includeSummaryKey}|${trendKey}|${deviceDetailKey}|${proxyDetailKey}|${ruleDetailKey}|${ruleChainFlowKey}|${domainsPageKey}|${ipsPageKey}`;if (!jsonCache.has(cacheKey)) {jsonCache.set(cacheKey,@@ -1051,6 +1063,7 @@ export class StatsWebSocketServer {clientInfo.includeRuleChainFlow,clientInfo.domainsPage,clientInfo.ipsPage,+ clientInfo.includeSummary,).then((stats) => (stats ? JSON.stringify({ type: 'stats', data: stats, timestamp: ts }) : null)).catch((err) => {diff --git a/apps/collector/src/modules/websocket/websocket.types.ts b/apps/collector/src/modules/websocket/websocket.types.tsindex cf3fe2f..10cf4ac 100644--- a/apps/collector/src/modules/websocket/websocket.types.ts+++ b/apps/collector/src/modules/websocket/websocket.types.ts@@ -10,6 +10,7 @@ export interface WebSocketMessage {start?: string;end?: string;minPushIntervalMs?: number;+ includeSummary?: boolean;// TrendincludeTrend?: boolean;@@ -101,6 +102,7 @@ export interface ClientInfo {backendId: number | null; // null means use active backendrange: ClientRange;minPushIntervalMs: number;+ includeSummary: boolean;lastSentAt: number;trend: ClientTrend;deviceDetail: ClientDeviceDetail;diff --git a/apps/web/app/[locale]/dashboard/components/content/index.tsx b/apps/web/app/[locale]/dashboard/components/content/index.tsxindex 8f53dbf..d70fbce 100644--- a/apps/web/app/[locale]/dashboard/components/content/index.tsx+++ b/apps/web/app/[locale]/dashboard/components/content/index.tsx@@ -267,7 +267,7 @@ const DevicesContent = memo(function DevicesContent({backendStatus: BackendStatus;autoRefresh: boolean;}) {- const stableTimeRange = useStableTimeRange(timeRange);+ const stableTimeRange = useStableTimeRange(timeRange, { roundToMinute: true });const devicesQuery = useQuery({queryKey: getDevicesQueryKey(activeBackendId, 50, stableTimeRange),diff --git a/apps/web/app/[locale]/dashboard/hooks/use-dashboard.ts b/apps/web/app/[locale]/dashboard/hooks/use-dashboard.tsindex 035100a..80fd835 100644--- a/apps/web/app/[locale]/dashboard/hooks/use-dashboard.ts+++ b/apps/web/app/[locale]/dashboard/hooks/use-dashboard.ts@@ -117,8 +117,15 @@ export function useDashboard(): UseDashboardReturn {const [isFirstTime, setIsFirstTime] = useState(false);const [showAboutDialog, setShowAboutDialog] = useState(false);- const stableTimeRange = useStableTimeRange(timeRange);- const isWsSummaryTab = activeTab === "overview" || activeTab === "countries";+ const stableTimeRange = useStableTimeRange(timeRange, {+ roundToMinute: isRollingTimePreset(timePreset),+ });+ const isWsSummaryTab =+ activeTab === "overview" ||+ activeTab === "countries" ||+ activeTab === "proxies" ||+ activeTab === "rules" ||+ activeTab === "devices";// Auth checkconst { showLogin, isLoading: isAuthLoading } = useRequireAuth();@@ -182,6 +189,8 @@ export function useDashboard(): UseDashboardReturn {const wsConnected = wsStatus === "connected";const wsRealtimeActive = wsEnabled && wsConnected;const shouldReducePolling = wsRealtimeActive;+ const fallbackRefetchInterval =+ autoRefresh && isWsSummaryTab && !wsRealtimeActive ? 5000 : false;const hasWsCountries =wsRealtimeActive &&!!wsSummary?.countryStats &&@@ -193,8 +202,10 @@ export function useDashboard(): UseDashboardReturn {const summaryQuery = useQuery({queryKey: getSummaryQueryKey(activeBackendId, stableTimeRange),queryFn: () => api.getSummary(activeBackendId, stableTimeRange),- enabled: !!activeBackendId && !(wsEnabled && wsConnected),+ enabled: !!activeBackendId && isWsSummaryTab && !(wsEnabled && wsConnected),placeholderData: keepPreviousData,+ refetchInterval: fallbackRefetchInterval,+ refetchIntervalInBackground: true,});const countriesQuery = useQuery({@@ -202,6 +213,8 @@ export function useDashboard(): UseDashboardReturn {queryFn: () => api.getCountries(activeBackendId, 50, stableTimeRange),enabled: !!activeBackendId && needsCountries && !hasWsCountries,placeholderData: keepPreviousData,+ refetchInterval: needsCountries ? fallbackRefetchInterval : false,+ refetchIntervalInBackground: true,});const data: StatsSummary | null =diff --git a/apps/web/components/features/devices/interactive-device-stats.tsx b/apps/web/components/features/devices/interactive-device-stats.tsxindex 521956d..cdba1dc 100644--- a/apps/web/components/features/devices/interactive-device-stats.tsx+++ b/apps/web/components/features/devices/interactive-device-stats.tsx@@ -79,7 +79,7 @@ export function InteractiveDeviceStats({const t = useTranslations("devices");const domainsT = useTranslations("domains");const backendT = useTranslations("dashboard");- const detailTimeRange = useStableTimeRange(timeRange);+ const detailTimeRange = useStableTimeRange(timeRange, { roundToMinute: true });const queryClient = useQueryClient();const [selectedDevice, setSelectedDevice] = useState<string | null>(null);@@ -140,6 +140,7 @@ export function InteractiveDeviceStats({backendId: activeBackendId,range: detailTimeRange,minPushIntervalMs: DEVICE_DETAIL_WS_MIN_PUSH_MS,+ includeSummary: false,includeDeviceDetails: wsDetailEnabled,deviceSourceIP: selectedDevice ?? undefined,deviceDetailLimit: 5000,diff --git a/apps/web/components/features/proxies/interactive-proxy-stats.tsx b/apps/web/components/features/proxies/interactive-proxy-stats.tsxindex 1c7cc0f..42ebc46 100644--- a/apps/web/components/features/proxies/interactive-proxy-stats.tsx+++ b/apps/web/components/features/proxies/interactive-proxy-stats.tsx@@ -102,7 +102,7 @@ export function InteractiveProxyStats({const domainsT = useTranslations("domains");const backendT = useTranslations("dashboard");const queryClient = useQueryClient();- const stableTimeRange = useStableTimeRange(timeRange);+ const stableTimeRange = useStableTimeRange(timeRange, { roundToMinute: true });const detailTimeRange = stableTimeRange;const isWindows = useIsWindows();@@ -168,6 +168,7 @@ export function InteractiveProxyStats({backendId: activeBackendId,range: detailTimeRange,minPushIntervalMs: PROXY_DETAIL_WS_MIN_PUSH_MS,+ includeSummary: false,includeProxyDetails: wsDetailEnabled,proxyChain: selectedProxy ?? undefined,proxyDetailLimit: 5000,@@ -196,14 +197,14 @@ export function InteractiveProxyStats({chain: selectedProxy,activeBackendId,range: detailTimeRange,- enabled: !!selectedProxy,+ enabled: !wsDetailEnabled || wsDetailStatus !== "connected",});const { data: proxyIPs = [], isLoading: ipsLoading } = useProxyIPs({chain: selectedProxy,activeBackendId,range: detailTimeRange,- enabled: !!selectedProxy,+ enabled: !wsDetailEnabled || wsDetailStatus !== "connected",});// Since we rely on cache updates from WS, we don't need dedicated state to track "hasWsDetails"diff --git a/apps/web/components/features/rules/interactive-rule-stats.tsx b/apps/web/components/features/rules/interactive-rule-stats.tsxindex 42a0cc9..9f47a26 100644--- a/apps/web/components/features/rules/interactive-rule-stats.tsx+++ b/apps/web/components/features/rules/interactive-rule-stats.tsx@@ -245,10 +245,11 @@ export function InteractiveRuleStats({}, [chartData]);const wsDetailEnabled = autoRefresh && !!activeBackendId && !!selectedRule;- useStatsWebSocket({+ const { status: wsDetailStatus } = useStatsWebSocket({backendId: activeBackendId,range: detailTimeRange,minPushIntervalMs: RULE_DETAIL_WS_MIN_PUSH_MS,+ includeSummary: false,includeRuleDetails: wsDetailEnabled,ruleName: selectedRule ?? undefined,ruleDetailLimit: 5000,@@ -277,14 +278,14 @@ export function InteractiveRuleStats({rule: selectedRule,activeBackendId,range: detailTimeRange,- enabled: !!selectedRule,+ enabled: !wsDetailEnabled || wsDetailStatus !== "connected",});const { data: ruleIPs = [], isLoading: ipsLoading } = useRuleIPs({rule: selectedRule,activeBackendId,range: detailTimeRange,- enabled: !!selectedRule,+ enabled: !wsDetailEnabled || wsDetailStatus !== "connected",});const loading = !!selectedRule && (domainsLoading || ipsLoading) && ruleDomains.length === 0 && ruleIPs.length === 0;diff --git a/apps/web/components/features/rules/rule-chain-flow.tsx b/apps/web/components/features/rules/rule-chain-flow.tsxindex 588335e..25f5add 100644--- a/apps/web/components/features/rules/rule-chain-flow.tsx+++ b/apps/web/components/features/rules/rule-chain-flow.tsx@@ -993,6 +993,7 @@ function UnifiedRuleChainFlowInner({backendId: activeBackendId,range: stableRange,minPushIntervalMs: RULE_CHAIN_FLOW_WS_MIN_PUSH_MS,+ includeSummary: false,includeRuleChainFlow: wsEnabled,trackLastMessage: false,enabled: wsEnabled,diff --git a/apps/web/components/features/stats/table/domain-table.tsx b/apps/web/components/features/stats/table/domain-table.tsxindex 8dd8023..bacbd27 100644--- a/apps/web/components/features/stats/table/domain-table.tsx+++ b/apps/web/components/features/stats/table/domain-table.tsx@@ -48,7 +48,7 @@ export function DomainsTable({onPageSizeChange,}: DomainsTableProps) {const t = useTranslations("domains");- const stableTimeRange = useStableTimeRange(timeRange);+ const stableTimeRange = useStableTimeRange(timeRange, { roundToMinute: true });const [search, setSearch] = useState("");const [debouncedSearch, setDebouncedSearch] = useState("");@@ -81,6 +81,7 @@ export function DomainsTable({backendId: activeBackendId,range: stableTimeRange,minPushIntervalMs: DOMAINS_WS_MIN_PUSH_MS,+ includeSummary: false,includeDomainsPage: wsPageEnabled,domainsPageOffset: pageOffset,domainsPageLimit: pageSize,diff --git a/apps/web/components/features/stats/table/ip-table.tsx b/apps/web/components/features/stats/table/ip-table.tsxindex 2efccad..f2190e7 100644--- a/apps/web/components/features/stats/table/ip-table.tsx+++ b/apps/web/components/features/stats/table/ip-table.tsx@@ -48,7 +48,7 @@ export function IPsTable({onPageSizeChange,}: IPsTableProps) {const t = useTranslations("ips");- const stableTimeRange = useStableTimeRange(timeRange);+ const stableTimeRange = useStableTimeRange(timeRange, { roundToMinute: true });const [search, setSearch] = useState("");const [debouncedSearch, setDebouncedSearch] = useState("");@@ -79,6 +79,7 @@ export function IPsTable({backendId: activeBackendId,range: stableTimeRange,minPushIntervalMs: IPS_WS_MIN_PUSH_MS,+ includeSummary: false,includeIPsPage: wsPageEnabled,ipsPageOffset: pageOffset,ipsPageLimit: pageSize,diff --git a/apps/web/components/overview/index.tsx b/apps/web/components/overview/index.tsxindex 765df5b..f5723ec 100644--- a/apps/web/components/overview/index.tsx+++ b/apps/web/components/overview/index.tsx@@ -213,6 +213,7 @@ export function OverviewTab({backendId: activeBackendId,range: { start: trendQuery.start, end: trendQuery.end },minPushIntervalMs: TREND_WS_MIN_PUSH_MS,+ includeSummary: false,includeTrend: wsTrendEnabled,trendMinutes: trendQuery.minutes,trendBucketMinutes: trendQuery.bucketMinutes,@@ -241,7 +242,7 @@ export function OverviewTab({bucketMinutes: trendQuery.bucketMinutes,range: { start: trendQuery.start, end: trendQuery.end },enabled: !!activeBackendId,- refetchInterval: wsTrendEnabled && wsTrendStatus === "connected" ? 90000 : false,+ refetchInterval: wsTrendEnabled && wsTrendConnected ? 90000 : false,});// Sync trend granularity state@@ -329,4 +330,3 @@ export function OverviewTab({</div>);}-diff --git a/apps/web/lib/websocket.ts b/apps/web/lib/websocket.tsindex ae87479..bf0e7f4 100644--- a/apps/web/lib/websocket.ts+++ b/apps/web/lib/websocket.ts@@ -32,6 +32,7 @@ interface UseStatsWebSocketOptions {backendId?: number;range?: TimeRange;minPushIntervalMs?: number;+ includeSummary?: boolean;includeTrend?: boolean;trendMinutes?: number;trendBucketMinutes?: number;@@ -149,6 +150,7 @@ export function useStatsWebSocket(options: UseStatsWebSocketOptions = {}) {backendId,range,minPushIntervalMs,+ includeSummary,includeTrend,trendMinutes,trendBucketMinutes,@@ -339,6 +341,7 @@ export function useStatsWebSocket(options: UseStatsWebSocketOptions = {}) {start: range?.start,end: range?.end,minPushIntervalMs,+ includeSummary,includeTrend,trendMinutes,trendBucketMinutes,@@ -373,6 +376,7 @@ export function useStatsWebSocket(options: UseStatsWebSocketOptions = {}) {range?.start,range?.end,minPushIntervalMs,+ includeSummary,includeTrend,trendMinutes,trendBucketMinutes,