[PR #7031] Show each client's lifetime traffic, change history and per-node usage from the client editor #6587

Open
opened 2026-11-02 04:09:53 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7031
Author: @Zaltapar
Created: 10/30/2026
Status: 🔄 Open

Base: main ← Head: feat/client-usage-history


📝 Commits (2)

  • 628a949 feat(clients): usage history and change log behind the edit modal's button
  • 3e69167 fix(clients): close the review gaps in the usage-history archiving

📊 Changes

43 files changed (+2923 additions, -32 deletions)

View changed files

📝 docs/content/docs/en/reference/api/clients.mdx (+31 -1)
📝 docs/public/openapi.json (+258 -0)
📝 frontend/public/openapi.json (+258 -0)
➕ frontend/src/api/queries/useClientUsageHistoryQuery.ts (+30 -0)
📝 frontend/src/api/queryKeys.ts (+1 -0)
➕ frontend/src/components/clients/ClientUsageHistoryModal.tsx (+266 -0)
📝 frontend/src/generated/examples.ts (+59 -0)
📝 frontend/src/generated/schemas.ts (+176 -0)
📝 frontend/src/generated/types.ts (+37 -0)
📝 frontend/src/generated/zod.ts (+41 -0)
➕ frontend/src/lib/clients/usage-history.ts (+80 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+15 -0)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+19 -0)
➕ frontend/src/test/client-usage-history-modal.test.tsx (+113 -0)
📝 internal/database/db.go (+2 -0)
📝 internal/database/migrate_data.go (+2 -0)
➕ internal/database/model/client_change_log.go (+13 -0)
➕ internal/database/model/client_traffic_window.go (+12 -0)
📝 internal/web/controller/client.go (+11 -0)
📝 internal/web/service/client_bulk.go (+65 -2)

...and 23 more files

📄 Description

Summary

A new button in the client edit section opens the client's full usage & change history:

  1. Lifetime traffic — total upload/download the client has used across its whole life, not just the current cycle. Every reset, auto-renewal, rename or deletion archives the counters it closes, so lifetime = archived windows + the live counters.
  2. All changes to the client's info — one row per changed field (name, sub ID, expiry/renewals, data limit, enable/disable, renewal plan, IP/HWID limits, ad tag, group), with old → new values and the source (panel, Telegram bot, auto-renew), plus the rename and deletion events. A no-op save logs nothing.
  3. Usage on each node, upload vs download separated — the per-node counters since the last reset, with the node's display name.

Why

Renewing or resetting a client currently zeroed client_traffics.up/down and the used amount was gone — a reseller could not answer "how much has this client ever used, on which node, and who changed its limits when". The panel already tracks per-node counters (node_client_traffics); the missing pieces were the archived closed windows and a metadata change log, surfaced somewhere the operator already looks (the client editor).

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

Changes

New tables (auto-migrated; empty on upgrade):

  • client_traffic_windows — one row per closed counter window: email, at, up, down, reason (reset | renew | deleted | renamed), indexed by email+time.
  • client_change_logs — one row per changed field: email, at, field, old_value, new_value, source (panel | tgbot | auto-renew), indexed by email+time.

Where windows are archived (same transaction as the zeroing/deletion, so nothing is lost or double-counted):

  • Single-client reset, bulk reset, reset-inbound, reset-all (the all variant archives every row with real usage) — reason: reset
  • Auto-renewal (autoRenewClients) archives the closed quota window and logs the expiry change — reason: renew, source auto-renew
  • Client deletion (both delete helpers) archives the counters and logs a deleted event — reason: deleted. The final row cleanup of a delete re-checks for any counter row that no per-inbound path archived (a client shared with an inbound outside the delete, or detached from every inbound) and archives it there too, so every dropped counter lands in exactly one window — never zero, never two.
  • A rename that discards the old email's row archives it — reason: renamed
  • A rename also re-keys the whole history onto the new email (guarded by rows-affected so the multi-inbound fan-out logs the rename exactly once), so the history follows the client instead of being stranded under the old name.

Where changes are logged:

  • Client editor save / POST /clients/update/:email — diffs the old record against the saved one and logs only the fields that actually changed (panel).
  • The Telegram bot's renew setters (set expiry / set data limit / set IP limit) and the enable toggles — log the field they write (tgbot).
  • Bulk adjust (expiry/total deltas) and bulk enable/disable — log per client, only for values that flipped (panel).

Read side: GET /clients/usageHistory/:email returns lifetime totals (SUM over all archived windows + live row), the open window's counters, the closed windows (newest first, capped at 2000 rows — the totals still count every row), the per-node up/down joined with node names, and the change log (newest first, capped at 500). Documented in the API-docs registry with a generated ClientUsageHistory response schema; the generated OpenAPI spec, its docs copy, and the docs reference page are regenerated. Unknown future fields/reasons render raw instead of blank.

UI: a labeled Usage & change history button in the edit modal's footer (edit mode, client with an email) opens a nested modal with the lifetime totals (upload/download + current cycle), a per-node table with separate ↑/↓ columns, the closed-cycles table, and the change log with human labels for every field in all 14 locales. Fetching uses the house react-query pattern (per-email cache, enabled only while the modal is open).

How was this tested?

  • New Go tests (client_usage_history_test.go): a single-client reset archives a reset window and lifetime = archived + live; bulk reset archives only rows with real usage; an auto-renewal archives a renew window and logs the expiry change under auto-renew; deletion archives a deleted window and logs the event; deleting an attached client archives its counters exactly once (the per-inbound path handles them, the final cleanup sees nothing left); deleting a detached client archives the surviving counters in the final cleanup; a bulk delete archives one deleted window + one event per email; a rename archives the discarded counters under renamed without a deletion event; an editor save logs exactly the changed fields and a no-op save logs nothing; bulk enable/disable logs only the flipped clients; a rename migrates every window and log row to the new email and records the rename once; an empty client reads back all zeros with empty collections; a node's counters come back separated into upload/download with the node's name; the endpoint's JSON shape carries all eight top-level keys.
  • New frontend tests: the modal renders the lifetime totals, the per-node table and the change log from a mocked fetch, renders the closed-cycles table with the renew reason, and shows the empty state.
  • Verified three times on this head: (1) the full go-test suite over every package; (2) a -race run over the touched packages plus the new tests twice; (3) both Postgres CI gate test sets against a UTF8 Postgres 17 instance (CI uses 16). The pinned golangci-lint v2.15.0, gofumpt/goimports, and the frontend lint/format/typecheck/vitest gates pass as well. Two local-only caveats, both reproduced on clean main so they are not from this PR: a small set of frontend locale tests time out on this Windows box, and one -race test binary fails to load (STATUS_ENTRYPOINT_NOT_FOUND).

Breaking changes

None. The two new tables are additive (auto-migrated, empty on upgrade); existing clients read back as "no history yet" until their next reset/renewal/edit. Archived windows only cover usage closed after the upgrade — pre-existing client_traffics.up/down is not back-filled, so a long-lived panel's lifetime total starts from the upgrade point.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/7031 **Author:** [@Zaltapar](https://github.com/Zaltapar) **Created:** 10/30/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/client-usage-history` --- ### 📝 Commits (2) - [`628a949`](https://github.com/MHSanaei/3x-ui/commit/628a949f186332584649cde5ed38436802241677) feat(clients): usage history and change log behind the edit modal's button - [`3e69167`](https://github.com/MHSanaei/3x-ui/commit/3e69167efee1366620f524f7b059450c9a721949) fix(clients): close the review gaps in the usage-history archiving ### 📊 Changes **43 files changed** (+2923 additions, -32 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/reference/api/clients.mdx` (+31 -1) 📝 `docs/public/openapi.json` (+258 -0) 📝 `frontend/public/openapi.json` (+258 -0) ➕ `frontend/src/api/queries/useClientUsageHistoryQuery.ts` (+30 -0) 📝 `frontend/src/api/queryKeys.ts` (+1 -0) ➕ `frontend/src/components/clients/ClientUsageHistoryModal.tsx` (+266 -0) 📝 `frontend/src/generated/examples.ts` (+59 -0) 📝 `frontend/src/generated/schemas.ts` (+176 -0) 📝 `frontend/src/generated/types.ts` (+37 -0) 📝 `frontend/src/generated/zod.ts` (+41 -0) ➕ `frontend/src/lib/clients/usage-history.ts` (+80 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+15 -0) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+19 -0) ➕ `frontend/src/test/client-usage-history-modal.test.tsx` (+113 -0) 📝 `internal/database/db.go` (+2 -0) 📝 `internal/database/migrate_data.go` (+2 -0) ➕ `internal/database/model/client_change_log.go` (+13 -0) ➕ `internal/database/model/client_traffic_window.go` (+12 -0) 📝 `internal/web/controller/client.go` (+11 -0) 📝 `internal/web/service/client_bulk.go` (+65 -2) _...and 23 more files_ </details> ### 📄 Description ## Summary A new button in the client edit section opens the client's full **usage & change history**: 1. **Lifetime traffic** — total upload/download the client has used across its whole life, not just the current cycle. Every reset, auto-renewal, rename or deletion archives the counters it closes, so lifetime = archived windows + the live counters. 2. **All changes to the client's info** — one row per changed field (name, sub ID, expiry/renewals, data limit, enable/disable, renewal plan, IP/HWID limits, ad tag, group), with old → new values and the source (panel, Telegram bot, auto-renew), plus the rename and deletion events. A no-op save logs nothing. 3. **Usage on each node, upload vs download separated** — the per-node counters since the last reset, with the node's display name. ## Why Renewing or resetting a client currently zeroed `client_traffics.up/down` and the used amount was gone — a reseller could not answer "how much has this client ever used, on which node, and who changed its limits when". The panel already tracks per-node counters (`node_client_traffics`); the missing pieces were the archived closed windows and a metadata change log, surfaced somewhere the operator already looks (the client editor). ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [x] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [x] Telegram bot ## Changes **New tables (auto-migrated; empty on upgrade):** - `client_traffic_windows` — one row per closed counter window: `email, at, up, down, reason` (`reset | renew | deleted | renamed`), indexed by email+time. - `client_change_logs` — one row per changed field: `email, at, field, old_value, new_value, source` (`panel | tgbot | auto-renew`), indexed by email+time. **Where windows are archived (same transaction as the zeroing/deletion, so nothing is lost or double-counted):** - Single-client reset, bulk reset, reset-inbound, reset-all (the all variant archives every row with real usage) — `reason: reset` - Auto-renewal (`autoRenewClients`) archives the closed quota window and logs the expiry change — `reason: renew`, source `auto-renew` - Client deletion (both delete helpers) archives the counters and logs a `deleted` event — `reason: deleted`. The final row cleanup of a delete re-checks for any counter row that no per-inbound path archived (a client shared with an inbound outside the delete, or detached from every inbound) and archives it there too, so every dropped counter lands in exactly one window — never zero, never two. - A rename that discards the old email's row archives it — `reason: renamed` - A rename also **re-keys the whole history onto the new email** (guarded by rows-affected so the multi-inbound fan-out logs the rename exactly once), so the history follows the client instead of being stranded under the old name. **Where changes are logged:** - Client editor save / `POST /clients/update/:email` — diffs the old record against the saved one and logs only the fields that actually changed (`panel`). - The Telegram bot's renew setters (set expiry / set data limit / set IP limit) and the enable toggles — log the field they write (`tgbot`). - Bulk adjust (expiry/total deltas) and bulk enable/disable — log per client, only for values that flipped (`panel`). **Read side:** `GET /clients/usageHistory/:email` returns lifetime totals (SUM over all archived windows + live row), the open window's counters, the closed windows (newest first, capped at 2000 rows — the totals still count every row), the per-node up/down joined with node names, and the change log (newest first, capped at 500). Documented in the API-docs registry with a generated `ClientUsageHistory` response schema; the generated OpenAPI spec, its docs copy, and the docs reference page are regenerated. Unknown future fields/reasons render raw instead of blank. **UI:** a labeled **Usage & change history** button in the edit modal's footer (edit mode, client with an email) opens a nested modal with the lifetime totals (upload/download + current cycle), a per-node table with separate ↑/↓ columns, the closed-cycles table, and the change log with human labels for every field in all 14 locales. Fetching uses the house react-query pattern (per-email cache, enabled only while the modal is open). ## How was this tested? - New Go tests (`client_usage_history_test.go`): a single-client reset archives a `reset` window and lifetime = archived + live; bulk reset archives only rows with real usage; an auto-renewal archives a `renew` window and logs the expiry change under `auto-renew`; deletion archives a `deleted` window and logs the event; deleting an attached client archives its counters exactly once (the per-inbound path handles them, the final cleanup sees nothing left); deleting a detached client archives the surviving counters in the final cleanup; a bulk delete archives one `deleted` window + one event per email; a rename archives the discarded counters under `renamed` without a deletion event; an editor save logs exactly the changed fields and a no-op save logs nothing; bulk enable/disable logs only the flipped clients; a rename migrates every window and log row to the new email and records the rename once; an empty client reads back all zeros with empty collections; a node's counters come back separated into upload/download with the node's name; the endpoint's JSON shape carries all eight top-level keys. - New frontend tests: the modal renders the lifetime totals, the per-node table and the change log from a mocked fetch, renders the closed-cycles table with the `renew` reason, and shows the empty state. - Verified three times on this head: (1) the full `go-test` suite over every package; (2) a `-race` run over the touched packages plus the new tests twice; (3) both Postgres CI gate test sets against a UTF8 Postgres 17 instance (CI uses 16). The pinned `golangci-lint` v2.15.0, gofumpt/goimports, and the frontend lint/format/typecheck/vitest gates pass as well. Two local-only caveats, both reproduced on clean `main` so they are not from this PR: a small set of frontend locale tests time out on this Windows box, and one `-race` test binary fails to load (`STATUS_ENTRYPOINT_NOT_FOUND`). ## Breaking changes None. The two new tables are additive (auto-migrated, empty on upgrade); existing clients read back as "no history yet" until their next reset/renewal/edit. Archived windows only cover usage closed after the upgrade — pre-existing `client_traffics.up/down` is not back-filled, so a long-lived panel's lifetime total starts from the upgrade point. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6587
No description provided.