Skip to content

perf(store): consolidate user stats into single SQL query#149

Merged
appleboy merged 7 commits intomainfrom
worktree-sql
Apr 2, 2026
Merged

perf(store): consolidate user stats into single SQL query#149
appleboy merged 7 commits intomainfrom
worktree-sql

Conversation

@appleboy
Copy link
Copy Markdown
Member

@appleboy appleboy commented Apr 2, 2026

Summary

  • Replace 3 sequential COUNT(*) queries with a single SQL subquery for the admin user detail page (/admin/users/{uuid})
  • Remove now-unused CountActiveTokensByUserID, CountOAuthConnectionsByUserID, and CountUserAuthorizationsByUserID from interface, store, mocks, and tests
  • Reduces DB round-trips from 4 to 2 when rendering user stats

Test plan

  • TestGetUserStatsByUserID — new store-level test for the combined query
  • TestGetUserStats — existing service test passes with new implementation
  • All store and service tests pass
  • Verified compilation of affected packages
  • Manual verification: /admin/users/{uuid} displays correct token, connection, and authorization counts

🤖 Generated with Claude Code

- Replace 3 sequential COUNT queries with one subquery-based query
- Remove unused CountActiveTokensByUserID, CountOAuthConnectionsByUserID,
  and CountUserAuthorizationsByUserID from interface, store, and tests
- Reduce admin user detail page from 4 DB round-trips to 2

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 2, 2026 09:21
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

❌ Patch coverage is 51.16279% with 21 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/mocks/mock_store.go 0.00% 18 Missing ⚠️
internal/store/user.go 88.88% 1 Missing and 1 partial ⚠️
internal/services/user.go 85.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves performance for the admin user detail page (/admin/users/{uuid}) by consolidating multiple per-user stats queries into a single store call, reducing database round-trips when rendering user stats.

Changes:

  • Added Store.GetUserStatsByUserID to fetch active token, OAuth connection, and authorization counts via a single SQL statement.
  • Updated UserService.GetUserStats to use the consolidated store method and removed the old per-stat count calls.
  • Removed the now-unused per-stat count methods from the store interface and regenerated mocks/tests accordingly.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/store/user.go Introduces GetUserStatsByUserID using one raw SQL query with subqueries to return all stats at once.
internal/store/user_test.go Replaces per-count tests with a single test for the consolidated stats method (currently only covers the all-zero case).
internal/services/user.go Switches service stats aggregation to the new consolidated store method and updates error wrapping.
internal/mocks/mock_store.go Regenerates mocks to remove old count methods and add GetUserStatsByUserID.
internal/core/store.go Updates UserReader interface to replace three count methods with GetUserStatsByUserID.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Fix GORM column mapping: OAuthConnectionCount was mapped to
  o_auth_connection_count instead of oauth_conn_count by convention
- Add explicit gorm:"column:..." tags to prevent naming mismatches
- Add non-zero test case with active/revoked tokens, OAuth connection,
  and user authorizations to verify correct counting

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Add is_active = true filter to authorization count subquery to
  match the behavior of ListUserAuthorizations and the admin UI
- Add revoked authorization to test to verify inactive grants are excluded

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…stency

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ters

- Replace 3 bare int64 return values with types.UserStatsCounts struct
  to prevent positional transposition bugs
- Rename authorization_count to active_authorization_count to clarify
  the is_active filter behavior
- Add store/types/user_stats.go for the new struct type

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Clean up unused dependency checksum left after go mod tidy

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@appleboy appleboy merged commit 98120b0 into main Apr 2, 2026
20 of 21 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