Skip to content

Conversation

@shreeharsha-factly
Copy link
Contributor

@shreeharsha-factly shreeharsha-factly commented Jul 25, 2025

Summary by CodeRabbit

  • New Features

    • Added an analytics endpoint for retrieving user statistics, including total users and user creation counts by day or month within a specified date range.
    • Introduced a report mode for user listing, allowing export of all users created within a specified date range without pagination.
  • Bug Fixes

    • Improved error handling for user creation to provide clearer responses when an email already exists or when other errors occur.

@coderabbitai
Copy link

coderabbitai bot commented Jul 25, 2025

Walkthrough

New analytics functionality is introduced for the admin interface, including a router and an HTTP handler to provide user analytics data. The admin route setup is updated to mount the analytics routes. User creation now handles duplicate email errors from the identity provider, and user listing supports a report mode to filter users by creation date with date range validation.

Changes

File(s) Change Summary
server/action/admin/analytics/router.go Introduces analytics router with a /users GET route handled by the new details function.
server/action/admin/analytics/user.go Adds the details handler for user analytics, supporting date range queries and response formatting.
server/action/admin/route.go Mounts the new /analytics route under the admin router with proper middleware protection.
server/action/admin/user/create.go Adds explicit handling of duplicate user email errors and status code checks after identity provider requests.
server/action/admin/user/list.go Enhances user listing with a report mode, supporting date range filtering and validation, and disables paging.

Sequence Diagram(s)

sequenceDiagram
    participant Admin as Admin Client
    participant Router as Analytics Router
    participant Handler as details Handler
    participant DB as Database

    Admin->>Router: GET /analytics/users?from=YYYY-MM-DD&to=YYYY-MM-DD
    Router->>Handler: Invoke details(w, r)
    Handler->>DB: Query total user count
    alt With date range
        Handler->>DB: Query user counts grouped by day/month
        DB-->>Handler: Aggregated user data
        Handler-->>Admin: JSON { total_users, analytics }
    else Without date range
        Handler-->>Admin: JSON { total_users }
    end
Loading
sequenceDiagram
    participant Admin as Admin Client
    participant Router as Admin Router
    participant Handler as User Create Handler
    participant Kratos as Kratos Admin API
    participant DB as Database

    Admin->>Router: POST /admin/user/create
    Router->>Handler: Invoke user creation logic
    Handler->>Kratos: Create identity
    alt Status 201 Created
        Handler->>DB: Create user in DB
        Handler-->>Admin: Success response
    else Status 409 Conflict
        Handler-->>Admin: Error "email already exists"
    else Other error
        Handler-->>Admin: Internal server error
    end
Loading
sequenceDiagram
    participant Admin as Admin Client
    participant Router as Admin Router
    participant Handler as User List Handler
    participant DB as Database

    Admin->>Router: GET /admin/user/list?is_report=true&from=YYYY-MM-DD&to=YYYY-MM-DD
    Router->>Handler: Invoke list logic
    Handler->>DB: Query users by created_at in date range
    Handler-->>Admin: JSON user list (all results, no paging)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Poem

In the warren, numbers grow,
Analytics now let us know!
With routes anew and careful checks,
We dodge those duplicate email wrecks.
Reports by date, so clear and bright,
The admin burrow’s future’s light.
🐇✨ Data hops into the night!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a6d434a and f3480b6.

📒 Files selected for processing (5)
  • server/action/admin/analytics/router.go (1 hunks)
  • server/action/admin/analytics/user.go (1 hunks)
  • server/action/admin/route.go (2 hunks)
  • server/action/admin/user/create.go (2 hunks)
  • server/action/admin/user/list.go (3 hunks)
🔇 Additional comments (13)
server/action/admin/analytics/router.go (1)

1-11: LGTM! Clean and simple router implementation.

The analytics router is well-structured with a clear separation of concerns. The single route registration follows RESTful conventions.

server/action/admin/route.go (2)

7-7: LGTM! Proper import addition.

The analytics package import is correctly placed and follows the existing import organization.


23-23: LGTM! Consistent route mounting.

The analytics router is properly mounted following the same pattern as existing routes and is correctly protected by the CheckMasterKey middleware.

server/action/admin/user/create.go (2)

6-6: LGTM! Proper import addition for error handling.

The errors import is correctly added to support the enhanced error handling logic.


77-87: LGTM! Excellent error handling improvement.

The enhanced error handling properly addresses different failure scenarios:

  • Explicit handling of 409 Conflict for duplicate emails with a clear error message
  • Generic handling for other non-201 status codes
  • Prevents unnecessary database operations when identity creation fails
  • Maintains consistent error handling patterns with the codebase
server/action/admin/user/list.go (3)

5-5: LGTM! Proper import addition for time handling.

The time import is correctly added to support the new date range functionality.


24-26: LGTM! Clear parameter extraction.

The query parameter extraction for report functionality is clean and follows established patterns.


37-81: LGTM! Well-implemented report mode with proper validation.

The report mode implementation includes several good practices:

  • Sensible default of 30 days when no date range is provided
  • Proper date parsing with error handling
  • Reasonable 3-month range limit to prevent performance issues
  • End-of-day adjustment for the to date
  • Appropriate bypassing of pagination for report mode

The date range validation is consistent and the error handling follows the established patterns in the codebase.

server/action/admin/analytics/user.go (5)

13-26: LGTM! Well-structured type definitions.

The struct definitions are clean and properly organized:

  • Appropriate JSON tags for API responses
  • Clear separation between formatted output (Data), response structure (response), and raw database results (rawdata)

28-44: LGTM! Clean function setup and total user count.

The function initialization and total user count query are properly implemented with appropriate error handling.


46-63: Note the different date range limit compared to list.go.

The date parsing and validation logic is correct, but there's a difference in range limits:

  • This analytics handler allows up to 12 months
  • The list.go report mode allows up to 3 months

This might be intentional (analytics vs detailed reports), but ensure this difference is documented or consistent with business requirements.


65-74: LGTM! Sensible format selection logic.

The dynamic format selection based on date range is well thought out:

  • Daily granularity for periods up to 30 days
  • Monthly granularity for longer periods
  • Clear format strings for presentation

75-93: No action needed for date_trunc usage
This project exclusively uses PostgreSQL (evidenced by gorm.io/driver/postgres in server/model/setup.go and widespread use of github.com/jinzhu/gorm/dialects/postgres), so date_trunc is fully supported.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/users/report-and-analytics

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@surajmn1 surajmn1 assigned surajmn1 and unassigned surajmn1 Jul 25, 2025
@surajmn1 surajmn1 requested review from elliot14A and surajmn1 and removed request for elliot14A July 25, 2025 08:48
@shreeharsha-factly shreeharsha-factly merged commit 2fce00d into develop Jul 25, 2025
2 of 3 checks passed
@shreeharsha-factly shreeharsha-factly deleted the feat/users/report-and-analytics branch July 25, 2025 08:49
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.

3 participants