fix(user-usage): keep manual resets out of reset cycles#458
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
thanks for pr, there are some points i should write here i check 2 other pr later but this pr stays open for a now, i will discuss it with other developers |
Thanks for reviewing. About the behavior change: I agree this is user-visible and can be considered major. I proposed it because the current implementation uses the latest usage reset log as the cycle anchor, so a manual/API reset also postpones the user's monthly/weekly/daily/yearly reset. Manual/API reset and scheduled reset have different meanings: manual/API reset is a one-off administrative action, while scheduled reset is the user's reset-cycle boundary. Right now both actions use the same anchor, so a manual reset can unexpectedly change the next scheduled reset. I do not want to force this behavior if the project considers the old logic intentional. I can adjust the PR to make the behavior explicit/backward-compatible, for example by adding an option such as Regarding I can also remove the unrelated dashboard date parsing changes from this PR to keep the scope smaller. |
Summary
This pull request fixes how traffic reset cycles are calculated when a user is reset manually from the panel or API.
Previously, all reset events were stored as the same kind of usage reset log. The panel then used the latest reset log as the anchor for the next periodic reset. Because of that, a manual reset could accidentally move a user's monthly, weekly, daily, or yearly reset date forward.
That behavior makes manual paid resets less useful: if a user pays only to clear current traffic usage, the regular plan reset should not be postponed.
Traffic Reset Cycle Logic
app/db/models.py, distinguishingmanual,scheduled, andnext_planreset events.user_usage_logs.reset_sourceinapp/db/migrations/versions/b8e4e47b9f2c_add_user_usage_reset_source.py. Existing reset logs are marked asscheduledso current users keep their existing reset-cycle anchors after upgrade.app/db/crud/user.pyso onlyscheduledandnext_planreset logs are used to calculate whether a user is due for the next automatic reset.app/jobs/reset_user_data_usage.pyto write reset logs withreset_source=scheduled.app/db/crud/user.pyto write reset logs withreset_source=next_plan, because applying a next plan should start a new usage cycle.Manual and API Reset Behavior
reset_source=manualinapp/db/crud/user.py, so panel/API resets clear current usage without changing the user's plan reset cycle.app/db/crud/user.pyso bulk manual resets use the same source-aware logic.app/db/crud/bulk.pyto log manual reset events instead of deleting reset history. This preserves lifetime traffic accounting while still preventing manual resets from moving scheduled reset dates.Reset Date Visibility
last_traffic_reset_atandnext_traffic_reset_atto user response models inapp/models/user.py.app/operation/subscription.py.app/templates/subscription/index.html.dashboard/src/service/api/index.ts.dashboard/src/components/dialogs/user-modal.tsx.Dashboard Date Handling
dashboard/src/components/users/columns.tsx.dashboard/src/components/users/status-badge.tsx.dashboard/src/pages/_dashboard.settings.cleanup.tsx.Test Coverage
tests/api/test_user_usage_reset_cycle.pyproving that a manual reset does not delay the next periodic reset.tests/api/test_user.pyto verify reset dates are exposed through subscription info and rendered on the subscription HTML page.tests/api/conftest.pyso JWT secret lookups use the test database consistently.Why This Is Better
The new model separates business intent instead of inferring it from a timestamp.
A scheduled reset means the user's plan cycle advanced. A next-plan reset means the user's next plan was activated and should also become the new cycle anchor. A manual reset means an admin or API caller cleared current usage immediately, but did not change the user's regular reset schedule.
This makes the behavior more predictable for operators and fairer for users who buy or receive an extra traffic reset. Manual reset becomes an additive action instead of something that can silently postpone the normal plan reset.