Skip to content

perf(users): avoid materializing reset history in lists - #850

Merged
ImMohammad20000 merged 3 commits into
PasarGuard:devfrom
dr-hoseyn:codex/perf-user-list-usage-aggregate
Sep 7, 2026
Merged

ImMohammad20000 merged 3 commits into
PasarGuard:devfrom
dr-hoseyn:codex/perf-user-list-usage-aggregate

Conversation

@dr-hoseyn

@dr-hoseyn dr-hoseyn commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • add opt-in aggregate loading for lifetime user traffic in the list query
  • make the dashboard user list calculate reset usage in SQL instead of materializing every user_usage_logs row
  • preserve the existing eager-loading default for non-dashboard callers
  • add regression coverage and a reproducible before/after benchmark

Benchmark

Command:

python -m scripts.benchmark_user_list_usage

Environment: Windows, Python 3.14, in-memory SQLite; 50 users with 1,000 reset-history rows each; median of 5 warmed runs. Peak memory is measured in a separate tracemalloc run.

Metric Before After Change
Median query + serialization time 1040.94 ms 15.25 ms -98.54%
Peak Python allocations 73.64 MiB 0.29 MiB -99.61%
SELECT statements 5 4 -20%
Usage-log ORM objects 50,000 0 -100%
Session identity-map objects 50,050 50 -99.90%

The response still returns the exact same lifetime_used_traffic values, including users with no reset history.

Tests

  • python -m pytest tests/test_user_list_usage_aggregate.py -q (2 passed)
  • python -m pytest tests --ignore=tests/api --ignore=tests/test_review_admins_unit.py -q (165 passed, 2 skipped)
  • ruff check app/db/crud/user.py app/operation/user.py tests/test_user_list_usage_aggregate.py scripts/benchmark_user_list_usage.py
  • ruff format --check app/db/crud/user.py app/operation/user.py tests/test_user_list_usage_aggregate.py scripts/benchmark_user_list_usage.py

Summary by CodeRabbit

  • Performance

    • Improved user-list loading by displaying lifetime traffic totals without loading individual usage history records.
    • Added configurable loading options for usage logs and lifetime usage data.
  • Bug Fixes

    • User lists now correctly show aggregated lifetime traffic while keeping detailed usage history unloaded.
  • Tests

    • Added regression coverage for lifetime traffic aggregation and efficient user-list loading.

@coderabbitai

coderabbitai Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: dc5c67af-b334-42f2-99a4-7185b5035c62

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: a6a5e8ac-f61f-4807-ba29-c4b5b0541753

📥 Commits

Reviewing files that changed from the base of the PR and between 234ab68 and e8d282a.

📒 Files selected for processing (4)
  • app/db/crud/user.py
  • app/operation/user.py
  • scripts/benchmark_user_list_usage.py
  • tests/test_user_list_usage_aggregate.py

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


Walkthrough

The user list query now conditionally loads usage history and can aggregate lifetime traffic from reset logs. The web operation selects aggregation without loading history. Regression tests and a benchmark cover query results, relationship loading, and resource usage.

Changes

User usage aggregation

Layer / File(s) Summary
Conditional user query loading
app/db/crud/user.py
Adds a reusable reset-traffic aggregate subquery. Adds flags for usage-log and lifetime-traffic loading. Applies each query option conditionally.
User list integration and validation
app/operation/user.py, tests/test_user_list_usage_aggregate.py, scripts/benchmark_user_list_usage.py
The user operation requests lifetime aggregation without loading usage logs. Tests verify totals and unloaded history. The benchmark compares both loading modes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to e8d28

The dashboard user list now calculates lifetime traffic in SQL without loading reset-history rows, preserving reported totals while reducing resource use. The change is ready to merge.

Sequence Diagram(s)

sequenceDiagram
  participant UserOperation
  participant CRUD_get_users
  participant UserUsageResetLogs
  UserOperation->>CRUD_get_users: Set load_usage_logs=False
  UserOperation->>CRUD_get_users: Set load_lifetime_used_traffic=True
  CRUD_get_users->>UserUsageResetLogs: Sum used_traffic_at_reset by user
  UserUsageResetLogs-->>CRUD_get_users: Return lifetime traffic totals
  CRUD_get_users-->>UserOperation: Return users without usage_logs
Loading

Suggested reviewers: x0sina, immohammad20000, m03ed

Poem

A rabbit sums the logs with care
And leaves old history sleeping there
The user list receives its flow
Tests confirm the totals show
A benchmark hops through rows just so

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: avoiding materialization of reset-history records in user lists for improved performance.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@dr-hoseyn

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@ImMohammad20000
ImMohammad20000 merged commit c7557d4 into PasarGuard:dev Sep 7, 2026
9 checks passed
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.

2 participants