Skip to content

Add nice-scale chart axes, dual-axis ping chart with loss overlay, and auth-gated period selector#1

Merged
fl0w1nd merged 2 commits into
mainfrom
feature/nice-scale-charts-and-auth
Jun 12, 2026
Merged

Add nice-scale chart axes, dual-axis ping chart with loss overlay, and auth-gated period selector#1
fl0w1nd merged 2 commits into
mainfrom
feature/nice-scale-charts-and-auth

Conversation

@fl0w1nd

@fl0w1nd fl0w1nd commented Jun 12, 2026

Copy link
Copy Markdown
Owner

TL;DR

Charts had ugly default ticks (e.g. 333.33ms), no loss visualization, and no way to view 7d/30d history. This PR adds a d3-style nice-scale engine for clean axis ticks, dual-axis ping charts (delay + loss overlay), period selection with auth gating, and traffic-limit progress bars on server cards.

Files to review

File Why
src/components/detail/chartScale.ts New — core scale engine: nice step, auto-baseline, loss/delay/speed presets, axis-width budgeting
src/components/detail/PingChart.tsx Major rewrite: LineChart → ComposedChart, dual Y-axes, loss area, period selector, custom tooltip
src/lib/auth.ts NewuseIsLogin() hook, probes /api/v1/profile to gate 7d/30d periods
src/components/ServerCard.tsx Traffic limit progress bar, billing badge redesign, bidirectional traffic
src/components/detail/HistoryChart.tsx Nice-scale Y-axis per metric type (percent/speed/raw), unit label in header
src/components/detail/RealtimeCharts.tsx Nice-scale Y-axis for CPU/memory (fixed 0-100) and speed (dynamic unit)
src/components/detail/ChartTheme.tsx Edge-aware X-axis tick component (EdgeTickX), X_AXIS_PROPS
src/components/Sparkline.tsx Decoupled viewBox from container size, showDot prop
src/components/OsIcon.tsx Brand hex colors from simple-icons instead of fill-current
src/pages/ServerDetail.tsx Truncate long CPU/GPU strings, billing badge style update
src/lib/api.ts fetchProfile, fetchServerMonitor accepts MonitorPeriod
src/types/nezha.ts MonitorPeriod type, ProfileResponse interface
src/lib/i18n.tsx loginToView i18n key (zh/en)
src/index.css Remove recharts focus outlines
mock/server.mjs Period-aware monitor data, deterministic failure windows, auth sim

Why

  • Ticks: recharts defaults produce unreadable values like 333.33ms; d3-style nice numbers (1/2/5×10^n) always produce clean labels and predictable axis widths.
  • Packet loss: the previous ping chart showed delay only; network issues that cause packet loss were invisible. Dual-axis + loss area makes outages immediately visible.
  • Periods: official themes support 7d/30d views gated behind login; this theme had no such capability.
  • Traffic limits: server cards showed raw transfer bytes; no visual indicator of how close a server is to its traffic quota.

How

  • chartScale.ts implements a standalone nice-scale engine: niceStep (sqrt-threshold 1/2/5/10 selector), niceScale (auto-baseline with configurable ratio, min-span guard, headroom), plus domain-specific helpers (PERCENT_SCALE, lossScale, speedScale, delayTickFormatter, axisWidthFor).
  • PingChart switches from LineChart to ComposedChart with two Y-axes: left for delay (P95-trimmed + auto-baseline), right for loss (preset-tier scale). Packet loss is computed per-point using the nezha-dash EWMA algorithm when backend packet_loss is absent.
  • Auth gating: useIsLogin() polls /api/v1/profile every 5min; 7d/30d buttons are disabled and auto-revert to 1d on logout.
  • ServerCard parses trafficVol (e.g. "500GB") into bytes, computes percentage, renders a color-coded 2px progress bar (green → amber → red at 75%/90%).

Reviewer notes

  • chartScale.ts is ~280 lines of pure math with no React deps — easy to unit-test if desired.
  • The packet-loss algorithm in PingChart.tsx is ported verbatim from hamster1963/nezha-dash; the EWMA smoothing and coefficient-of-variation thresholds match upstream.
  • Mock server auth uses document.cookie = "nz-jwt=mock" — documented in a comment in server.mjs.

Visual aids

Before After
Y-axis: 333.33ms, 666.67ms… Y-axis: 200ms, 400ms, 600ms
Ping chart: delay lines only Ping chart: delay lines + loss area overlay
No period selector 1d / 7d / 30d tabs (7d/30d locked for guests)
Card: raw transfer bytes Card: traffic progress bar with percentage
OS icons: monochrome fill-current OS icons: brand hex colors

fl0w1nd added 2 commits June 12, 2026 19:39
…th-gated period selector

- Introduce chartScale.ts: d3-style nice tick calculation, auto-baseline,
  loss/delay/speed presets, axis width budgeting
- Rework PingChart to ComposedChart with delay Y-axis + loss area overlay,
  P95-trimmed scale, and per-monitor packet loss from nezha-dash algorithm
- Add 1d/7d/30d period selector; 7d and 30d gated behind login
  (matching official theme behavior)
- Add useIsLogin hook via /api/v1/profile with periodic refetch
- Apply nice-scale Y-axes to HistoryChart and RealtimeCharts with
  unit labels in chart headers
- Edge-aware X-axis ticks (first left-aligned, last right-aligned)
- ServerCard: traffic limit progress bar with bidirectional traffic
  support, billing badge redesign
- OsIcon: use brand hex colors from simple-icons instead of fill-current
- Sparkline: decouple viewBox from container size, add showDot prop
- ServerDetail: truncate long CPU/GPU strings, billing badge style update
- Mock server: period-aware data, deterministic failure windows, auth
  simulation via cookie
- Use template literal over string concatenation in ServerDetail
- Replace 1.414 with Math.SQRT2 in chartScale
- Reformat inline comments in mock server
- Fix formatting in OsIcon, ServerCard, Sparkline
@fl0w1nd fl0w1nd merged commit 8c513eb into main Jun 12, 2026
2 checks passed
@fl0w1nd fl0w1nd deleted the feature/nice-scale-charts-and-auth branch June 12, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant