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

fix: rendered the analytics for labels #5906

Merged
merged 2 commits into from
Oct 24, 2024
Merged

Conversation

NarayanBavisetti
Copy link
Collaborator

@NarayanBavisetti NarayanBavisetti commented Oct 24, 2024

chore:

  • this pull request fixes the issue with analytics rendering when labels are selected on the x-axis.

Summary by CodeRabbit

  • New Features

    • Added support for CSV file uploads by including "text/csv" in the allowed attachment types.
  • Bug Fixes

    • Improved filtering logic in the analytics queries to ensure correct data retrieval without altering existing functionality.
  • Documentation

    • Clarified the intent of filtering conditions in the analytics endpoint and background tasks.

Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

Walkthrough

The changes in this pull request involve modifications to the filtering logic within the AnalyticsEndpoint class and the get_label_details function. The bitwise AND operator has been replaced with separate filter conditions to clarify the logic in queries. Additionally, the MIME type list in the settings file has been updated to include "text/csv", allowing for the processing of CSV files. No new functionality has been introduced, and the overall structure and error handling remain unchanged.

Changes

File Path Change Summary
apiserver/plane/app/views/analytic/base.py Adjusted filter condition in AnalyticsEndpoint from bitwise AND to separate conditions for clarity.
apiserver/plane/bgtasks/analytic_plot_export.py Separated filter conditions in get_label_details for clarity, maintaining overall structure.
apiserver/plane/settings/common.py Added "text/csv" to ATTACHMENT_MIME_TYPES list to allow CSV file uploads.

Possibly related PRs

Suggested labels

⚙️backend

Suggested reviewers

  • sriramveeraghanta
  • pablohashescobar

🐇 In the code, we hop and play,
Adjusting filters in a clever way.
CSVs now join the MIME type crew,
With clearer logic, our queries renew.
Let's celebrate with a joyful cheer,
For changes made, we hold so dear! 🥳


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: 3

🧹 Outside diff range and nitpick comments (1)
apiserver/plane/bgtasks/analytic_plot_export.py (1)

Line range hint 359-363: Fix incorrect collection usage in module name mapping.

There's a bug in the module name mapping logic. The code is incorrectly using label_details instead of module_details when mapping module names in segmented rows.

Apply this fix:

            module = next(
                (
                    mod
-                    for mod in label_details
+                    for mod in module_details
                    if str(mod[MODULE_ID]) == str(segm)
                ),
                None,
            )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 074ad6d and 07b6e9f.

📒 Files selected for processing (3)
  • apiserver/plane/app/views/analytic/base.py (1 hunks)
  • apiserver/plane/bgtasks/analytic_plot_export.py (1 hunks)
  • apiserver/plane/settings/common.py (1 hunks)
🔇 Additional comments (2)
apiserver/plane/settings/common.py (1)

442-442: LGTM with security considerations.

The addition of "text/csv" MIME type is appropriate for analytics export functionality. However, ensure that proper input validation and sanitization are in place to prevent CSV injection attacks when processing uploaded CSV files.

Let's verify if CSV validation is implemented:

✅ Verification successful

Let me try a different search strategy to verify CSV handling in the codebase.

This will help us understand:

  1. How CSV files are handled in general
  2. If there's any generic file validation that would apply to CSV uploads

Let me try a more focused search to find CSV validation logic.

This will help us understand:

  1. Any validation logic in Python backend code
  2. How MIME types are validated/restricted

Based on the search results, I can now provide a definitive response about the CSV MIME type validation:

CSV file type is properly validated through MIME type checks

The codebase has a centralized MIME type validation system in settings/common.py where text/csv is now part of the ATTACHMENT_MIME_TYPES list. This list is actively enforced in the attachment upload flow through the validation check:

if not type or type not in settings.ATTACHMENT_MIME_TYPES:
    return Response({"error": "Invalid file type."})

This ensures that only files with approved MIME types, including CSV files, can be uploaded. The validation happens at the API level before any file processing occurs.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for CSV validation or sanitization logic
rg -i "csv.*validation|sanitize.*csv|clean.*csv" 

Length of output: 50


Script:

#!/bin/bash
# Search for CSV file handling patterns
rg -i "csv" -A 3 -B 3

# Also check for file upload validation patterns
rg -i "file.*validation|validate.*file|clean.*file" -A 3 -B 3

Length of output: 198003


Script:

#!/bin/bash
# Search for file validation patterns in Python files
rg -t py "validate|sanitize|clean" -A 3 -B 3

# Also check for MIME type validation patterns
rg -t py "ATTACHMENT_MIME_TYPES|ALLOWED_MIME_TYPES" -A 3 -B 3

Length of output: 107285

apiserver/plane/bgtasks/analytic_plot_export.py (1)

135-136: LGTM! Improved filter readability.

The separation of filter conditions into distinct clauses is a more idiomatic Django approach and improves code readability while maintaining the same functionality. The filters correctly handle:

  1. Ensuring only labeled issues are included (labels__id__isnull=False)
  2. Excluding deleted label associations (label_issue__deleted_at__isnull=True)

apiserver/plane/settings/common.py Show resolved Hide resolved
Comment on lines 114 to 119
**filters,
labels__id__isnull=False
& Q(label_issue__deleted_at__isnull=True),
labels__id__isnull=False,
label_issue__deleted_at__isnull=True,
)
.distinct("labels__id")
.order_by("labels__id")
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring repeated filtering patterns.

The filtering pattern for handling soft deletes and null checks is repeated across different entities (labels, cycles, modules). Consider extracting this into a reusable query manager or utility function to maintain consistency and reduce duplication.

Example implementation:

def get_entity_details(queryset, entity_type):
    """
    Generic function to fetch entity details with proper null and soft delete handling
    
    :param queryset: Base queryset to filter
    :param entity_type: String indicating the entity type (labels, cycles, modules)
    :return: Filtered queryset with appropriate values
    """
    entity_configs = {
        'labels': {
            'id_field': 'labels__id',
            'deleted_at_field': 'label_issue__deleted_at',
            'values': ['labels__id', 'labels__color', 'labels__name']
        },
        'cycles': {
            'id_field': 'issue_cycle__cycle_id',
            'deleted_at_field': 'issue_cycle__deleted_at',
            'values': ['issue_cycle__cycle_id', 'issue_cycle__cycle__name']
        },
        # Add other entities...
    }
    
    config = entity_configs[entity_type]
    return (
        queryset.filter(**{
            f"{config['id_field']}__isnull": False,
            f"{config['deleted_at_field']}__isnull": True
        })
        .distinct(config['id_field'])
        .order_by(config['id_field'])
        .values(*config['values'])
    )

Comment on lines +115 to +116
labels__id__isnull=False,
label_issue__deleted_at__isnull=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Based on the search results, I can now generate the final response as I have sufficient information to verify the concern.

The change in analytics endpoint is inconsistent with label filtering patterns across the codebase

The search results reveal that most of the codebase uses a consistent pattern of combining label filters with bitwise AND (&), while the analytics endpoint now uses comma-separated conditions. This inconsistency could lead to:

  • Maintenance challenges due to different filtering patterns for the same logic
  • Potential behavioral differences in edge cases

Key findings:

  • Files like utils/grouper.py, views/issue/base.py, and others consistently use: ~Q(labels__id__isnull=True) & Q(label_issue__deleted_at__isnull=True)
  • Only the analytics endpoint (views/analytic/base.py) and the analytics export task use comma-separated conditions
  • The functionality remains the same, but the implementation pattern differs

Note: While both approaches work in Django, maintaining consistency across the codebase is important for long-term maintainability. Consider aligning the analytics endpoint with the rest of the codebase by using the bitwise AND pattern: ~Q(labels__id__isnull=True) & Q(label_issue__deleted_at__isnull=True).

🔗 Analysis chain

LGTM! The filter conditions are now properly separated.

The change from bitwise AND to comma-separated conditions follows Django's best practices for applying multiple filters. This should correctly filter out null labels and deleted label associations.

Let's verify the fix works as expected:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the analytics endpoint correctly handles label filtering
# by checking for any remaining issues with null labels or deleted associations

# Test 1: Check for any remaining issues with null labels in analytics
rg -A 5 "labels__id__isnull.*True" .

# Test 2: Check for consistency in label filtering across the codebase
rg -A 5 "label_issue__deleted_at__isnull.*True" .

# Test 3: Look for any similar patterns that might need the same fix
ast-grep --pattern 'Issue.objects.filter($$$).distinct("labels__id")'

Length of output: 21611

@pushya22 pushya22 merged commit 6d08cf2 into preview Oct 24, 2024
14 of 15 checks passed
@pushya22 pushya22 deleted the chore/label-analytics branch October 24, 2024 15:05
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