Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aggregation functions for URLs statistics to reduce load times #43

Conversation

Zingzy
Copy link
Member

@Zingzy Zingzy commented Nov 15, 2024

This PR addresses Issue #42

Summary by Sourcery

Add aggregation functions for URL statistics to improve performance and reduce load times by using aggregation pipelines. Refactor data handling to streamline code and enhance efficiency, and sanitize bot names in URL redirection logic to ensure valid database keys.

New Features:

  • Introduce aggregation functions for URL statistics to optimize data retrieval and reduce load times.

Enhancements:

  • Refactor URL data handling to use aggregation pipelines, improving performance and reducing code complexity.
  • Sanitize bot names in URL redirection logic to prevent invalid characters in database keys.

@Zingzy Zingzy added priority: high High priority tasks backend Changes related to Backand/API optimization database labels Nov 15, 2024
@Zingzy Zingzy added this to the SEO and Optimization milestone Nov 15, 2024
@Zingzy Zingzy linked an issue Nov 15, 2024 that may be closed by this pull request
Copy link
Contributor

sourcery-ai bot commented Nov 15, 2024

Reviewer's Guide by Sourcery

This PR optimizes URL statistics retrieval by introducing MongoDB aggregation functions and refactoring the data processing logic. The changes move data transformation from application-level to database-level processing, reducing memory usage and improving response times. The implementation includes new aggregation pipeline utilities and modifications to existing routes to leverage these optimizations.

Sequence diagram for URL statistics retrieval

sequenceDiagram
    participant Client
    participant Server
    participant MongoDB

    Client->>Server: Request /stats/<short_code>
    Server->>MongoDB: Call aggregate_url(pipeline)
    MongoDB-->>Server: Return aggregated URL data
    Server-->>Client: Render stats.html with URL data
Loading

Class diagram for URL data aggregation

classDiagram
    class MongoUtils {
        +load_url(id, projection)
        +aggregate_url(pipeline)
        +load_emoji_url(alias, projection)
        +aggregate_emoji_url(pipeline)
    }

    class PipelineUtils {
        +get_stats_pipeline(short_code)
        +_create_field_transform(field_name)
    }

    MongoUtils --> PipelineUtils: uses

    note for MongoUtils "New aggregation functions added"
    note for PipelineUtils "New utility for creating aggregation pipelines"
Loading

File-Level Changes

Change Details Files
Introduce MongoDB aggregation functions to replace manual data processing
  • Add new aggregate_url and aggregate_emoji_url functions to handle database aggregations
  • Create utility functions to generate MongoDB aggregation pipelines
  • Implement field transformation logic for browser, OS, country, and referrer statistics
utils/mongo_utils.py
utils/pipeline_utils.py
Optimize URL data retrieval and processing in routes
  • Replace manual data processing with aggregation pipeline calls
  • Add projection parameter to limit initial data retrieval
  • Simplify null checks and default value handling
  • Remove redundant data processing loops and transformations
blueprints/stats.py
Improve bot detection and handling
  • Add sanitization for bot names to prevent MongoDB operator injection
  • Fix bot statistics storage format
blueprints/url_shortener.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Zingzy - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Zingzy
Copy link
Member Author

Zingzy commented Nov 15, 2024

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Zingzy - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using more specific exception handling instead of catching generic Exceptions in aggregate_url() and aggregate_emoji_url()
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@Zingzy Zingzy merged commit 8623057 into main Nov 15, 2024
@Zingzy Zingzy deleted the 42-optimize-stats-retrieval-and-calculation-using-aggregation-pipelines branch November 15, 2024 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Changes related to Backand/API database optimization priority: high High priority tasks
Projects
Status: ✔️ Done
Development

Successfully merging this pull request may close these issues.

Optimize Stats retrieval and calculation using Aggregation Pipelines
1 participant