Skip to content

[WEB-2573] improvement: search-issues API optimization.#5727

Merged
pushya22 merged 1 commit intopreviewfrom
improvement/search-issues
Oct 1, 2024
Merged

[WEB-2573] improvement: search-issues API optimization.#5727
pushya22 merged 1 commit intopreviewfrom
improvement/search-issues

Conversation

@prateekshourya29
Copy link
Member

@prateekshourya29 prateekshourya29 commented Sep 30, 2024

  • limit search results to 100 issues.

Summary by CodeRabbit

  • New Features

    • Enhanced issue search functionality with additional filtering options, including workspace, parent, and module parameters.
    • Introduced a limit of 100 distinct issue results for improved performance.
  • Bug Fixes

    • Corrected indentation in the filtering methods for better code clarity and maintainability.

* limit search results to 100 issues.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 30, 2024

Walkthrough

The changes involve modifications to the filtering logic for issues within the GlobalSearchEndpoint and IssueSearchEndpoint classes. The filter_issues method now limits results to a maximum of 100 distinct issues, while the get method in IssueSearchEndpoint has been enhanced with additional filtering conditions based on various parameters from the GET request. These adjustments aim to refine the search results and improve the overall querying process.

Changes

File Path Change Summary
apiserver/plane/app/views/search/base.py Updated filter_issues method to limit results to 100 issues and corrected indentation of filter_cycles.
apiserver/plane/app/views/search/issue.py Enhanced get method in IssueSearchEndpoint with additional filtering logic based on multiple parameters.

Possibly related PRs

Suggested reviewers

  • sriramveeraghanta
  • pablohashescobar
  • SatishGandham

Poem

🐰 In the fields where issues roam,
A search was made to find a home.
With filters set and limits tight,
A hundred issues, just right!
So hop along, let queries play,
The rabbit’s joy is here to stay! 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
apiserver/plane/app/views/search/issue.py (3)

Line range hint 70-91: Add pagination to handle larger datasets gracefully

While limiting the results to 100 items improves performance, implementing pagination would offer a more flexible and user-friendly solution. This allows clients to request specific pages of results and manage data more efficiently, especially if the total number of issues exceeds the limit.

Consider integrating Django's pagination utilities. Here's an example of how you might modify the code:

from django.core.paginator import Paginator

def get(self, request, slug, project_id):
    # ... (existing code)

    page_number = request.query_params.get('page', 1)
    page_size = request.query_params.get('page_size', 20)

    paginator = Paginator(issues.values(
        "name",
        "id",
        "start_date",
        "sequence_id",
        "project__name",
        "project__identifier",
        "project_id",
        "workspace__slug",
        "state__name",
        "state__group",
        "state__color",
    ), page_size)

    page_obj = paginator.get_page(page_number)

    return Response(
        {
            'results': list(page_obj),
            'count': paginator.count,
            'num_pages': paginator.num_pages,
            'current_page': page_obj.number,
        },
        status=status.HTTP_200_OK,
    )

This approach provides clients with additional context about the data and allows them to navigate through the results.


Line range hint 57-69: Handle exceptions when retrieving issue objects

In the blocks where you retrieve an issue using Issue.issue_objects.get(pk=issue_id), there's a possibility of a DoesNotExist exception if the issue_id is invalid or does not exist. This could lead to a server error.

Consider adding exception handling to manage this scenario gracefully:

from django.shortcuts import get_object_or_404

if parent == "true" and issue_id:
    issue = get_object_or_404(Issue.issue_objects, pk=issue_id)
    issues = issues.filter(
        ~Q(pk=issue_id), ~Q(pk=issue.parent_id), ~Q(parent_id=issue_id)
    )

Repeat similar changes for other instances where issue = Issue.issue_objects.get(pk=issue_id) is called.


Line range hint 53-55: Ensure consistent boolean parameter handling

In the line target_date = request.query_params.get("target_date", True), the default value is set to True, which is a boolean, but request.query_params.get returns a string or None. This might lead to inconsistencies when comparing target_date later.

Consider using "true" or "false" as default values to maintain consistency:

- target_date = request.query_params.get("target_date", True)
+ target_date = request.query_params.get("target_date", "true")

Alternatively, explicitly convert the parameter to a boolean where it's used.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between bfef0e8 and 8fe0099.

📒 Files selected for processing (2)
  • apiserver/plane/app/views/search/base.py (1 hunks)
  • apiserver/plane/app/views/search/issue.py (1 hunks)
🔇 Additional comments (5)
apiserver/plane/app/views/search/base.py (3)

93-93: 🛠️ Refactor suggestion

Ensure consistency across filter methods and update response structure

While the limitation of 100 results has been applied to the filter_issues method, it's important to ensure consistency across all filter methods in the GlobalSearchEndpoint class. Additionally, the current response structure in the get method doesn't account for potentially truncated results.

Consider the following improvements:

  1. Apply similar result limitations to other filter methods for consistency.
  2. Update the get method to handle and communicate truncated results.

Here's a suggested refactor for the get method:

def get(self, request, slug):
    # ... (existing parameter handling)

    results = {}
    truncated = {}

    for model, func in MODELS_MAPPER.items():
        model_results = func(query, slug, project_id, workspace_search)
        if isinstance(model_results, dict) and 'truncated' in model_results:
            results[model] = model_results['results']
            truncated[model] = model_results['truncated']
        else:
            results[model] = model_results
            truncated[model] = False

    return Response({
        "results": results,
        "truncated": truncated
    }, status=status.HTTP_200_OK)

This refactor:

  1. Allows individual filter methods to return both results and a truncation flag.
  2. Includes a 'truncated' object in the response to indicate which result sets were limited.

To ensure this change is implemented correctly and doesn't break existing functionality, please run the following verification:

#!/bin/bash
# Check all filter methods for consistent result limiting
grep -r "def filter_" .

# Verify the structure of the Response object in the get method
grep -r "return Response(" .

# Look for any external code that might be relying on the current response structure
grep -r "results.*:" .

Line range hint 1-93: Overall assessment: Good start, but consider broader optimizations

The change to limit the results in filter_issues is a step in the right direction for optimizing the search-issues API. However, there are opportunities for further improvement:

  1. Consistency: Consider applying similar result limitations to all filter methods in the GlobalSearchEndpoint class.
  2. Flexibility: Make the result limit configurable rather than hardcoded.
  3. Efficiency: Apply the limit earlier in the query chain for better database performance.
  4. Transparency: Update the response structure to indicate when results are truncated.

These improvements would make the API more robust, efficient, and user-friendly. They would also better align with the PR's objective of optimizing the search functionality.

For a more comprehensive optimization:

  1. Consider implementing pagination for all filter methods. This would provide a consistent interface for handling large result sets across all search types.
  2. Evaluate the performance impact of the current filtering approach, especially for large datasets. You might want to consider using database-specific optimizations or full-text search capabilities for better performance.
  3. Think about caching frequently accessed search results to reduce database load.

To ensure that these suggestions don't conflict with existing architecture or requirements, please run the following checks:

#!/bin/bash
# Check for any existing pagination implementations
grep -r "paginate" .

# Look for any performance-related comments or TODOs
grep -ri "todo.*performance" .
grep -ri "optimize" .

# Check for any caching mechanisms already in place
grep -r "cache" .

These checks will help identify any existing work or considerations related to the suggested improvements.


93-93: 🛠️ Refactor suggestion

Consider refining the result limitation approach

The addition of [:100] to limit the results aligns with the PR objective of optimizing the search-issues API. This change can improve performance by reducing the amount of data returned. However, there are some considerations:

  1. The hardcoded limit of 100 might not be flexible for different use cases. Consider making this a configurable parameter.
  2. There's no indication to the client whether there are more results beyond this limit. It might be helpful to include a flag in the response indicating if the results were truncated.
  3. Applying the limit after all filtering and distinct operations might not be the most efficient approach for very large datasets. Consider using database-level limitations (e.g., LIMIT in SQL) earlier in the query chain.

Here's a suggested refactor to address these points:

from django.conf import settings

class GlobalSearchEndpoint(BaseAPIView):
    # ... (other methods)

    def filter_issues(self, query, slug, project_id, workspace_search):
        # ... (existing filtering logic)

        issues = Issue.issue_objects.filter(
            q,
            project__project_projectmember__member=self.request.user,
            project__project_projectmember__is_active=True,
            project__archived_at__isnull=True,
            workspace__slug=slug,
        )

        if workspace_search == "false" and project_id:
            issues = issues.filter(project_id=project_id)

        # Apply limit earlier in the query
        limit = getattr(settings, 'ISSUE_SEARCH_LIMIT', 100)
        issues = issues.distinct()[:limit]

        results = list(issues.values(
            "name",
            "id",
            "sequence_id",
            "project__identifier",
            "project_id",
            "workspace__slug",
        ))

        return {
            'results': results,
            'truncated': len(results) == limit
        }

This refactor:

  1. Uses a configurable limit from settings.
  2. Applies the limit earlier in the query chain.
  3. Includes a 'truncated' flag to indicate if there are more results.

To ensure this change doesn't break existing functionality, please run the following verification:

apiserver/plane/app/views/search/issue.py (2)

Line range hint 95-99: Ensure role comparison uses the correct data type

In the condition checking the user's role:

if ProjectMember.objects.filter(
    project_id=project_id,
    member=self.request.user,
    is_active=True,
    role=5
).exists():

Ensure that the role comparison matches the data type used for role definitions. If role is defined as a string elsewhere, comparing it to an integer might cause unexpected behavior.

Please confirm that role is indeed an integer field and that 5 corresponds to the intended role.


Line range hint 86-88: Clarify the exclusion logic for module parameter

When excluding issues related to a specific module:

if module:
    issues = issues.exclude(issue_module__module=module)

Consider whether module should filter issues belonging to that module instead of excluding them. Excluding might not be the intended behavior if the goal is to search within a specific module.

Please verify if the exclusion is intentional. If you want to filter issues within the specified module, modify the code as follows:

- issues = issues.exclude(issue_module__module=module)
+ issues = issues.filter(issue_module__module=module)

@pushya22 pushya22 requested a review from SatishGandham October 1, 2024 08:25
@pushya22 pushya22 merged commit 927d265 into preview Oct 1, 2024
@pushya22 pushya22 deleted the improvement/search-issues branch October 1, 2024 08:47
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