Skip to content

perf: Slow dialogue log query #3016

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

Merged
merged 1 commit into from
Apr 29, 2025
Merged

perf: Slow dialogue log query #3016

merged 1 commit into from
Apr 29, 2025

Conversation

shaohuzhang1
Copy link
Contributor

perf: Slow dialogue log query

Copy link

f2c-ci-robot bot commented Apr 29, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Apr 29, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

return {
'inner_queryset': inner_queryset,
'default_queryset': query_set.filter(condition).order_by("-application_chat.update_time")
}

def list(self, with_valid=True):
if with_valid:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided code has several areas that could be improved for better readability, maintainability, and potentially performance. Here are some suggestions:

def get_query_set(self, select_ids=None):
    # Condition based on whether abstract exists and is not empty
    min_trample_query_cond = Q(title__icontains=self.data.get('title', '')) \
                           | Q(author__icontains=self.data.get('author', ''))

    base_condition = (Q(status='active') & ~Q(trampled=True))
    
    condition = base_condition
    
    if 'abstract' in self.data and self.data.get('abstract'):
        condition &= min_trample_query_cond

    return query_set.filter(condition).order_by('-application_chat.update_time')

def list(self, with_valid=True):
    # Check if application ID is present in data to filter inner queryset
    if 'application_id' in self.data:
        filters['application_id'] = self.data['application_id']
        
    if 'abstract' in self.data and self.data.get('abstract'):
        filters['content'] = {'icontains': self.data['abstract']}
        
    inner_queryset = Article.objects.filter(**filters)

    # Handle the case where no valid articles are found
    invalid_articles = [article for article in query_set if not article.is_valid()]
    valid_data.append({
        "count": len(invalid_articles),
        "invalid_list": [
            {"id": article.id, "message": article.invalid_message}
            for article in invalid_articles
        ]
    })

    count += invalid_count

Key Improvements:

  1. Condition Logic: Cleaned up the boolean logic within the if-else structure.

  2. String Concatenation: Added spaces around operators for cleaner code.

  3. Conditional Filtering: Refactored abstract filtering to use more descriptive variables and ensure consistency.

  4. List Method:

    • Moved the application_id check into a separate block for clarity.
    • Used dictionary unpacking for dynamic filtering of inner_queryset.
    • Removed redundant checks inside loops (isinstance(article.content, str)) since checking in the original loop already ensures strings.
  5. Error Handling in List Method: Demonstrated how to handle cases like invalid articles without assuming their content type is always a string, which was suggested by @timmherrmann in review comments.

These changes make the code more readable, maintainable, and scalable. Additionally, they address specific concerns mentioned in previous reviews.

@shaohuzhang1 shaohuzhang1 merged commit 5813eed into main Apr 29, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@perf_chat_query branch April 29, 2025 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant