Refactor server card metrics display and cycle-traffic logic#2
Merged
Conversation
Split since-boot transfer from billing-cycle usage and source the latter from the panel's /api/v1/service cycle_transfer_stats so it survives agent restarts. Cards now render iconized segmented bars (lucide-react), a dedicated cycle bar with percentage and period range, and reserve fixed slot heights so a mixed fleet stays aligned. Fix the Services page reset date to use the cycle end (to) instead of the internal next_update timestamp, and drop the unused Sparkline component.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
Home-page cards reported a single "total traffic" figure that is really the agent's since-boot counter, so it silently resets whenever an agent restarts and never lines up with a billing cycle. This separates that counter from real per-cycle usage pulled from the panel's
cycle_transfer_statsand renders it as a dedicated bar with percentage and window (e.g. DMIT:507 GB / 1.0 TB (50%),6/5 → 7/5).Files to review
src/lib/cycle.ts(new)/api/v1/serviceand flattenscycle_transfer_statsinto aserverId → usagemap.src/components/ServerCard.tsxsrc/components/UsageBar.tsxsrc/components/ServerRow.tsxsrc/pages/Home.tsxsrc/pages/Services.tsxsrc/types/nezha.tsServerCycleInfoshape.Why
Nezha agents expose cumulative transfer counters that zero out on restart, so the old "total" was neither lifetime nor cyclic. The panel already persists true per-cycle usage through
transfer_*_cyclealert rules and exposes it on/api/v1/service, but that data was only surfaced on the Services page. Server cards are where users actually watch quota, so the cyclic figure belongs there.How
useCycleTransferMapreadscycle_transfer_stats(keyed by alert rule, then by server) and flattens it to one entry per server.Homebuilds the map once and passes each server its slice plus areserveCycleflag; cards without a matching rule still reserve the slot so a mixed fleet stays vertically aligned. The card then draws two distinct rows — since-boot total and the cycle bar — and the segmentedUsageBarcarries the metric icon and color.Reviewer notes
transfer_*_cyclealert rule; everything degrades gracefully to the since-boot total when no rule exists.cycle.to(the cycle end).next_updateis an internal recompute timestamp and was the source of the wrong "resets 6/29" display on a cycle that actually ends 7/5.public_notetraffic strings (e.g.1TB/Month) stay a display-only plan tag — they are free-form and not parsed into the quota bar.lucide-react; removes the unusedSparklinecomponent.Before / after
%+ period rangenext_update→ wrong (6/29)to→ correct (7/5)Tests
pnpm lint,tsc -b, andpnpm buildpass. Verified against a live panel (NEZHA_BACKEND=…): the cycle bar, percentage, and6/5 → 7/5window render on cards with a configured rule, and the Services page shows the correct reset date.