From b351b08601d52689b06cf11193e2d5610a4564ce Mon Sep 17 00:00:00 2001 From: Hamed Date: Thu, 3 Dec 2026 01:18:05 +0330 Subject: [PATCH] feat(dashboard): persist one overview time range (#7277) Consolidate the six overview range selectors into a single translated action-bar control beside Xray Metrics. Persist the validated selection per browser origin and panel base path with a safe 2m fallback, including unavailable storage. Restore the preference before history fetching and retain live status sampling while viewing longer ranges. Avoid overlapping delayed live-history seeds, track sampled scope epochs, use separate scoped/unscoped boundaries and reject stale responses. Cover scope transitions, clock skew bounds, storage failures and range propagation. Update English and Persian documentation without changing schema or backend API contracts. Validation: all 12 CI jobs and Docs CI passed on 2d3992766b277974f2cb4b4b44af1a6a456653dd; frontend 265 files / 2,330 tests, including 12 global-range, 11 ranged-history and 8 container-history cases. Browser restart and responsive/RTL checks reported by author, not independently rerun. Persistence is browser-local; arbitrary clock skew cannot guarantee complete live history. Closes #7276 --- .../docs/en/operations/system-monitoring.mdx | 2 +- .../docs/fa/operations/system-monitoring.mdx | 2 +- frontend/src/pages/index/ConnectionsCard.tsx | 9 - frontend/src/pages/index/IndexPage.css | 19 +- frontend/src/pages/index/IndexPage.tsx | 35 ++- .../src/pages/index/OverviewActionBar.tsx | 23 +- .../src/pages/index/OverviewRangeSelect.tsx | 33 +-- frontend/src/pages/index/ThroughputCard.tsx | 9 - frontend/src/pages/index/VitalTile.tsx | 11 - .../src/pages/index/useOverviewHistory.ts | 111 +++++---- frontend/src/test/container-history.test.tsx | 6 +- .../src/test/network-traffic-status.test.tsx | 2 - .../src/test/overview-global-range.test.tsx | 150 ++++++++++++ .../src/test/overview-ranged-history.test.tsx | 230 +++++++++++++++++- .../src/test/overview-stop-confirm.test.tsx | 2 + .../overview-update-tag-keyboard.test.tsx | 2 + 16 files changed, 527 insertions(+), 119 deletions(-) create mode 100644 frontend/src/test/overview-global-range.test.tsx diff --git a/docs/content/docs/en/operations/system-monitoring.mdx b/docs/content/docs/en/operations/system-monitoring.mdx index d80b2791f..83c0b2ac2 100644 --- a/docs/content/docs/en/operations/system-monitoring.mdx +++ b/docs/content/docs/en/operations/system-monitoring.mdx @@ -8,7 +8,7 @@ The overview page combines a current server snapshot with short time-series char ## Choose a chart window -Each overview chart has the same time-range selector in its upper-right corner. The default is **2m**. The choices are **2m**, **5m**, **30m**, **1h**, **6h**, and **12h**. Changing any selector changes the window on every overview chart. +Use **Time range** beside **Xray Metrics** to change the window on every overview chart. The choices are **2m**, **5m**, **30m**, **1h**, **6h**, and **12h**. The default is **2m**. This browser remembers your selection for the panel across reloads and panel restarts. The number of chart points stays capped at 60. Short windows use finer samples; longer windows use averages. At **2m** the charts are live: every status poll, once every 2 seconds, adds a point. A longer window is read from the server's stored history and refreshed once per point, at most once a minute. diff --git a/docs/content/docs/fa/operations/system-monitoring.mdx b/docs/content/docs/fa/operations/system-monitoring.mdx index 2082e9c59..2e3d46d0f 100644 --- a/docs/content/docs/fa/operations/system-monitoring.mdx +++ b/docs/content/docs/fa/operations/system-monitoring.mdx @@ -8,7 +8,7 @@ icon: ChartNoAxesCombined ## انتخاب بازهٔ نمودار -هر نمودار نمای کلی یک سلکت بازهٔ زمانی در گوشهٔ بالای سمت راست دارد. مقدار پیش‌فرض **2m** است. گزینه‌ها **2m**، **5m**، **30m**، **1h**، **6h** و **12h** هستند. تغییر بازه در هر نمودار، بازهٔ همهٔ نمودارهای نمای کلی را تغییر می‌دهد. +از گزینهٔ **بازهٔ زمانی** کنار **شاخص‌های Xray** برای تغییر بازهٔ همهٔ نمودارهای نمای کلی استفاده کنید. گزینه‌ها **2m**، **5m**، **30m**، **1h**، **6h** و **12h** هستند. مقدار پیش‌فرض **2m** است. مرورگر انتخاب شما را برای این پنل پس از بارگیری مجدد صفحه و راه‌اندازی دوبارهٔ پنل حفظ می‌کند. تعداد نقطه‌های نمودار حداکثر ۶۰ نقطه است. بازه‌های کوتاه‌تر از نمونه‌های دقیق‌تر و بازه‌های بلندتر از میانگین نمونه‌ها استفاده می‌کنند. در بازهٔ **2m** نمودارها زنده‌اند: هر بار دریافت وضعیت، یعنی هر ۲ ثانیه، یک نقطه اضافه می‌کند. بازه‌های بلندتر از تاریخچهٔ ذخیره‌شده در سرور خوانده می‌شوند و به‌ازای هر نقطه یک بار، و حداکثر دقیقه‌ای یک بار، به‌روزرسانی می‌شوند. diff --git a/frontend/src/pages/index/ConnectionsCard.tsx b/frontend/src/pages/index/ConnectionsCard.tsx index 128e9b4ab..35d4d3140 100644 --- a/frontend/src/pages/index/ConnectionsCard.tsx +++ b/frontend/src/pages/index/ConnectionsCard.tsx @@ -4,8 +4,6 @@ import { Card, theme } from 'antd'; import { Sparkline } from '@/components/viz'; import type { Status } from '@/models/status'; -import OverviewRangeSelect from './OverviewRangeSelect'; -import type { OverviewRange } from './OverviewRangeSelect'; interface ConnectionsCardProps { status: Status; @@ -13,8 +11,6 @@ interface ConnectionsCardProps { udp: number[]; labels: string[]; isMobile: boolean; - range: OverviewRange; - onRangeChange: (range: OverviewRange) => void; } export default function ConnectionsCard({ @@ -23,8 +19,6 @@ export default function ConnectionsCard({ udp, labels, isMobile, - range, - onRangeChange, }: ConnectionsCardProps) { const { t } = useTranslation(); const { token } = theme.useToken(); @@ -49,9 +43,6 @@ export default function ConnectionsCard({ -
- -
diff --git a/frontend/src/pages/index/IndexPage.css b/frontend/src/pages/index/IndexPage.css index 22f689698..7e07bab85 100644 --- a/frontend/src/pages/index/IndexPage.css +++ b/frontend/src/pages/index/IndexPage.css @@ -133,6 +133,12 @@ } } +.ov-bar-range { + display: inline-flex; + align-items: center; + gap: 4px; +} + .ov-bar-sep { width: 1px; height: 20px; @@ -284,12 +290,13 @@ margin-top: 6px; } -.ov-range-toolbar { - position: absolute; - z-index: 1; - top: 14px; - right: var(--ov-pad); - display: flex; +.ov-range-control { + display: inline-flex; + align-items: center; + gap: 8px; + white-space: nowrap; + font-size: 12px; + color: var(--ov-label); } .ov-range-select { diff --git a/frontend/src/pages/index/IndexPage.tsx b/frontend/src/pages/index/IndexPage.tsx index 63fda3f39..9311889c3 100644 --- a/frontend/src/pages/index/IndexPage.tsx +++ b/frontend/src/pages/index/IndexPage.tsx @@ -30,6 +30,7 @@ import ThroughputCard from './ThroughputCard'; import ConnectionsCard from './ConnectionsCard'; import SystemStrip from './SystemStrip'; import { mean, peak, useOverviewHistory } from './useOverviewHistory'; +import { OVERVIEW_RANGES } from './OverviewRangeSelect'; import type { OverviewRange } from './OverviewRangeSelect'; import type { PanelUpdateInfo } from './PanelUpdateModal'; const JsonEditor = lazy(() => import('@/components/form/JsonEditor')); @@ -52,6 +53,15 @@ function metricFoot(label: string, value: string) { ); } +function readHistoryRange(key: string): OverviewRange { + try { + const saved = localStorage.getItem(key); + return OVERVIEW_RANGES.find((range) => String(range) === saved) ?? 2; + } catch { + return 2; + } +} + export default function IndexPage() { const { t } = useTranslation(); const { isDark, isUltra, antdThemeConfig } = useTheme(); @@ -86,7 +96,16 @@ export default function IndexPage() { const [loading, setLoading] = useState(false); const [loadingTip, setLoadingTip] = useState(t('loading')); - const [historyRange, setHistoryRange] = useState(2); + const historyRangeKey = '3x-ui:overview-range:' + (basePath || '/'); + const [historyRange, setHistoryRange] = useState(() => readHistoryRange(historyRangeKey)); + const changeHistoryRange = (range: OverviewRange) => { + setHistoryRange(range); + try { + localStorage.setItem(historyRangeKey, String(range)); + } catch { + // A blocked browser store should still allow changes for this visit. + } + }; const history = useOverviewHistory(status, fetched && !fetchError, historyRange); useEffect(() => { @@ -213,6 +232,8 @@ export default function IndexPage() { panelVersion={displayVersion} latestVersion={panelUpdateInfo.latestVersion} updateAvailable={panelUpdateInfo.updateAvailable} + historyRange={historyRange} + onHistoryRangeChange={changeHistoryRange} onStopXray={stopXray} onRestartXray={restartXray} onOpenLogs={() => setLogsOpen(true)} @@ -259,8 +280,6 @@ export default function IndexPage() { )} data={history.series.cpu} isMobile={isMobile} - range={historyRange} - onRangeChange={setHistoryRange} /> } @@ -278,8 +297,6 @@ export default function IndexPage() { )} data={history.series.mem} isMobile={isMobile} - range={historyRange} - onRangeChange={setHistoryRange} /> } @@ -298,8 +315,6 @@ export default function IndexPage() { )} data={history.series.swap} isMobile={isMobile} - range={historyRange} - onRangeChange={setHistoryRange} /> } @@ -317,8 +332,6 @@ export default function IndexPage() { )} data={history.series.diskUsage} isMobile={isMobile} - range={historyRange} - onRangeChange={setHistoryRange} />
@@ -329,8 +342,6 @@ export default function IndexPage() { down={history.series.netDown} labels={history.labels} isMobile={isMobile} - range={historyRange} - onRangeChange={setHistoryRange} />
diff --git a/frontend/src/pages/index/OverviewActionBar.tsx b/frontend/src/pages/index/OverviewActionBar.tsx index 9a5dbab7f..b7de16cab 100644 --- a/frontend/src/pages/index/OverviewActionBar.tsx +++ b/frontend/src/pages/index/OverviewActionBar.tsx @@ -19,6 +19,7 @@ import { import { formatPanelVersion } from '@/lib/panel-version'; import { activateOnKey } from '@/utils/a11y'; import type { Status } from '@/models/status'; +import OverviewRangeSelect, { type OverviewRange } from './OverviewRangeSelect'; interface OverviewActionBarProps { status: Status; @@ -27,6 +28,8 @@ interface OverviewActionBarProps { panelVersion: string; latestVersion: string; updateAvailable: boolean; + historyRange: OverviewRange; + onHistoryRangeChange: (range: OverviewRange) => void; onStopXray: () => void | Promise; onRestartXray: () => void; onOpenLogs: () => void; @@ -64,6 +67,8 @@ export default function OverviewActionBar({ panelVersion, latestVersion, updateAvailable, + historyRange, + onHistoryRangeChange, onStopXray, onRestartXray, onOpenLogs, @@ -158,12 +163,6 @@ export default function OverviewActionBar({ text: t('pages.index.systemHistoryTitle'), onClick: onOpenSystemHistory, }, - { - key: 'metrics', - icon: , - text: t('pages.index.xrayMetricsTitle'), - onClick: onOpenXrayMetrics, - }, ], ]; @@ -241,6 +240,18 @@ export default function OverviewActionBar({ ))} ))} +
+ + +
); diff --git a/frontend/src/pages/index/OverviewRangeSelect.tsx b/frontend/src/pages/index/OverviewRangeSelect.tsx index a9878cee2..cfc25467c 100644 --- a/frontend/src/pages/index/OverviewRangeSelect.tsx +++ b/frontend/src/pages/index/OverviewRangeSelect.tsx @@ -9,26 +9,27 @@ interface OverviewRangeSelectProps { onChange: (value: OverviewRange) => void; } -const OPTIONS = [ - { value: 2, label: '2m' }, - { value: 5, label: '5m' }, - { value: 30, label: '30m' }, - { value: 60, label: '1h' }, - { value: 360, label: '6h' }, - { value: 720, label: '12h' }, -]; +const OPTIONS = OVERVIEW_RANGES.map((value) => ({ + value, + label: value < 60 ? String(value) + 'm' : String(value / 60) + 'h', +})); export default function OverviewRangeSelect({ value, onChange }: OverviewRangeSelectProps) { const { t } = useTranslation(); + const label = t('pages.index.timeRange'); return ( -