Skip to content

Snapshot Summary #1336

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Snapshot Summary #1336

wants to merge 16 commits into from

Conversation

Yashgupta9330
Copy link
Collaborator

@Yashgupta9330 Yashgupta9330 commented Apr 10, 2025

fixes: #1232

image

Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

Summary by CodeRabbit

  • New Features

    • Added a "Snapshot Summary" section to the snapshot details page, displaying counts and example names for users, projects, chapters, issues, and releases.
    • Snapshot summaries are now available via the GraphQL API and visible in the UI.
  • Tests

    • Added unit tests to verify correct parsing and display of snapshot summary data.
  • Chores

    • Updated the spell checker dictionary with additional entries.

Summary by CodeRabbit

  • New Features

    • Added a "Snapshot Summary" section to the snapshot details page, displaying counts and example names for users, projects, chapters, issues, and releases.
    • The snapshot summary is now available via GraphQL and shown in the UI with category icons.
  • Bug Fixes

    • No bug fixes included.
  • Tests

    • Added unit tests to verify correct rendering of the snapshot summary section.

Walkthrough

The changes introduce a summary feature for snapshots across the backend and frontend. A new generate_summary method is added to the Snapshot model to produce a summary string describing new entities in the snapshot. The GraphQL schema is updated to expose this summary via a new summary field on the SnapshotNode, with an appropriate resolver. On the frontend, the GraphQL query, TypeScript types, and the snapshot details page are updated to retrieve and display this summary below the snapshot title and date range, including parsing the summary string to show counts and example names with icons.

Changes

Files/Paths Change Summary
backend/apps/owasp/models/snapshot.py Added generate_summary method to the Snapshot model to produce a summary of new entities in a snapshot.
backend/apps/owasp/graphql/nodes/snapshot.py Added summary GraphQL field and resolve_summary resolver to SnapshotNode.
frontend/src/server/queries/snapshotQueries.ts Included summary field in the GET_SNAPSHOT_DETAILS GraphQL query.
frontend/src/types/snapshot.ts Added summary: string property to SnapshotDetailsProps interface.
frontend/src/app/snapshots/[id]/page.tsx Added parsing and rendering of snapshot summary below the title, showing counts and examples with icons.
frontend/tests/unit/pages/SnapshotDetails.test.tsx Added test case verifying rendering of the snapshot summary section with expected summary data.
frontend/tests/unit/data/mockSnapshotData.ts Added summary property with descriptive snapshot summary text to mock snapshot data.

Assessment against linked issues

Objective Addressed Explanation
Generate a snapshot summary with counts/examples of new entities (users, projects, chapters, etc.) (#1232)
Expose the generated summary via the backend and GraphQL API (#1232)
Display the generated summary at the top of the snapshot details page (#1232)

Suggested reviewers

  • kasya

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc724e8 and d89aede.

📒 Files selected for processing (1)
  • cspell/custom-dict.txt (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • cspell/custom-dict.txt
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Run frontend e2e tests
  • GitHub Check: Run frontend unit tests
  • GitHub Check: Run backend tests
  • GitHub Check: CodeQL (javascript-typescript)

🪧 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 generate docstrings to generate docstrings 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.

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.

@Yashgupta9330 Yashgupta9330 marked this pull request as draft April 10, 2025 17:21
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

🧹 Nitpick comments (3)
frontend/src/components/SnapshotCard.tsx (1)

15-17: Consider adding conditional rendering for the summary.

The summary display looks good, but there's no handling for cases where summary might be undefined or empty. Consider adding conditional rendering to gracefully handle these cases.

-        <div className="mb-2 w-full text-xs text-gray-500 dark:text-gray-300 text-center">
-          {summary}
-        </div>
+        {summary && (
+          <div className="mb-2 w-full text-xs text-gray-500 dark:text-gray-300 text-center">
+            {summary}
+          </div>
+        )}
backend/apps/owasp/models/snapshot.py (2)

55-84: Well-structured summary generation with room for optimization.

The generate_summary method is well-structured and provides a clear, informative summary of snapshot data. However, consider these improvements:

  1. Add error handling for unexpected scenarios
  2. Consider performance optimization as each entity type requires a separate database query

Here's an improved version with error handling:

def generate_summary(self, max_examples=2):
    """Generate a snapshot summary with counts and examples.
    
    Args:
        max_examples: Maximum number of examples to show for each entity type.
        
    Returns:
        str: A formatted summary string with counts and examples of new entities.
        
    Note:
        This method performs multiple database queries which may impact performance
        with large datasets.
    """
    summary_parts = []

    def summarize(queryset, label, example_attr):
        try:
            count = queryset.count()
            if count == 0:
                return None
            examples = list(queryset.values_list(example_attr, flat=True)[:max_examples])
            example_str = ", ".join(str(e) for e in examples)
            return f"{count} {label}{'s' if count != 1 else ''} (e.g., {example_str})"
        except Exception as e:
            # Log the error or handle it appropriately
            return f"Error processing {label}s: {str(e)}"

    entities = [
        (self.new_users, "user", "login"),
        (self.new_projects, "project", "name"),
        (self.new_chapters, "chapter", "name"),
        (self.new_issues, "issue", "title"),
        (self.new_releases, "release", "tag_name"),
    ]

    for queryset, label, attr in entities:
        part = summarize(queryset, label, attr)
        if part:
            summary_parts.append(part)

    return (
        "Snapshot Summary: " + "; ".join(summary_parts)
        if summary_parts
        else "No new entities were added."
    )

59-65: Consider optimizing the summarize function for better performance.

The current implementation calls queryset.count() and then queryset.values_list(), resulting in two database queries per entity type. This could be optimized by fetching both the count and examples in a single query.

def summarize(queryset, label, example_attr):
    count = queryset.count()
    if count == 0:
        return None
-   examples = list(queryset.values_list(example_attr, flat=True)[:max_examples])
+   # Get both count and examples in one query
+   examples_queryset = queryset[:max_examples]
+   examples = [getattr(obj, example_attr) for obj in examples_queryset]
    example_str = ", ".join(str(e) for e in examples)
    return f"{count} {label}{'s' if count != 1 else ''} (e.g., {example_str})"
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ebad12b and b875e54.

📒 Files selected for processing (6)
  • backend/apps/owasp/graphql/nodes/snapshot.py (2 hunks)
  • backend/apps/owasp/models/snapshot.py (1 hunks)
  • frontend/src/components/SnapshotCard.tsx (1 hunks)
  • frontend/src/server/queries/snapshotQueries.ts (1 hunks)
  • frontend/src/types/card.ts (1 hunks)
  • frontend/src/types/snapshot.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
backend/apps/owasp/graphql/nodes/snapshot.py (2)
backend/apps/github/models/release.py (1)
  • summary (57-59)
backend/apps/owasp/models/snapshot.py (1)
  • generate_summary (55-84)
frontend/src/components/SnapshotCard.tsx (2)
frontend/src/types/card.ts (1)
  • SnapshotCardProps (64-71)
backend/apps/github/models/release.py (1)
  • summary (57-59)
🪛 GitHub Actions: Run CI/CD
frontend/src/components/SnapshotCard.tsx

[error] 1-1: Unstaged changes detected. Run make check and use git add to address it.

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: CodeQL (python)
  • GitHub Check: CodeQL (javascript-typescript)
🔇 Additional comments (7)
frontend/src/server/queries/snapshotQueries.ts (1)

66-66: Good addition of the summary field

This change correctly adds the summary field to the GraphQL query, allowing the frontend to retrieve the summary data generated by the backend. This aligns with the changes made to the backend's SnapshotNode class and the TypeScript interfaces.

frontend/src/types/card.ts (1)

70-70: Appropriate type definition for summary property

The addition of the summary property to the SnapshotCardProps interface is consistent with the changes to the GraphQL query. The type is correctly specified as a string.

frontend/src/types/snapshot.ts (1)

26-26: Consistent interface update

The addition of the summary property to the Snapshots interface matches the changes made to the GraphQL query and the SnapshotCardProps interface. This ensures type safety throughout the application.

backend/apps/owasp/graphql/nodes/snapshot.py (2)

25-25: Field properly defined

The summary field is correctly defined using graphene.String(), making it available in the GraphQL schema for queries.


60-62: Resolver method looks good

The resolver method correctly calls the generate_summary method to produce the snapshot summary. The implementation looks solid and maintains consistency with other resolvers in the class.

Note that this uses the default max_examples=2 parameter from the generate_summary method, which is a reasonable default to keep summaries concise.

frontend/src/components/SnapshotCard.tsx (2)

7-7: Props destructuring includes the new summary parameter.

The function signature has been updated to include the summary parameter, which matches the SnapshotCardProps interface definition from frontend/src/types/card.ts.


1-46:

❓ Verification inconclusive

Address the pipeline failure.

There's a pipeline failure indicating unstaged changes. Run make check to identify formatting issues or other changes that need to be addressed.


🏁 Script executed:

#!/bin/bash
# Check if there are any unstaged changes in the frontend directory
git status --porcelain -- frontend/

Length of output: 37


Pipeline Issue: Verify Formatting and Unstaged Changes
Our verification using git status --porcelain -- frontend/ returned no unstaged changes. The pipeline failure might instead be related to formatting issues or other checks performed by make check. Please run make check locally to identify any formatting deviations or other issues, then address those if found.

🧰 Tools
🪛 GitHub Actions: Run CI/CD

[error] 1-1: Unstaged changes detected. Run make check and use git add to address it.

Comment on lines +59 to +84
def summarize(queryset, label, example_attr):
count = queryset.count()
if count == 0:
return None
examples = list(queryset.values_list(example_attr, flat=True)[:max_examples])
example_str = ", ".join(str(e) for e in examples)
return f"{count} {label}{'s' if count != 1 else ''} (e.g., {example_str})"

entities = [
(self.new_users, "user", "login"),
(self.new_projects, "project", "name"),
(self.new_chapters, "chapter", "name"),
(self.new_issues, "issue", "title"),
(self.new_releases, "release", "tag_name"),
]

for queryset, label, attr in entities:
part = summarize(queryset, label, attr)
if part:
summary_parts.append(part)

return (
"Snapshot Summary: " + "; ".join(summary_parts)
if summary_parts
else "No new entities were added."
)
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Add unit tests for the new functionality.

The generate_summary method is a good candidate for unit testing to ensure it handles different data scenarios correctly.


🏁 Script executed:

#!/bin/bash
# Check if there are existing tests for the Snapshot model
echo "Checking for existing tests for Snapshot model..."
find backend -name "test_*.py" -o -name "*_test.py" | xargs grep -l "Snapshot" || echo "No existing tests found"

# Look for test patterns in the project
echo -e "\nExamining test patterns in the project..."
find backend -name "test_*.py" -o -name "*_test.py" | head -n 1 | xargs cat || echo "No test files found"

Length of output: 1868


Action Required: Add Dedicated Unit Tests for the generate_summary Method

While there are existing tests for the Snapshot model (as seen in backend/tests/apps/owasp/models/snapshot_test.py), it isn’t clear that the new generate_summary functionality is specifically covered. Please add unit tests that ensure the method behaves correctly under various data conditions. For example:

  • Test when the queryset returns zero records (should return "No new entities were added.").
  • Test when there is exactly one record (ensuring the singular label is used).
  • Test when multiple records are returned and the maximum examples limit (max_examples) is enforced.

@Yashgupta9330 Yashgupta9330 marked this pull request as ready for review April 18, 2025 15:53
@Yashgupta9330 Yashgupta9330 marked this pull request as draft April 18, 2025 16:20
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: 0

🧹 Nitpick comments (3)
frontend/src/app/snapshots/[id]/page.tsx (3)

38-68: Add error handling to the summary parser function.

The parsing function works well for the expected format, but lacks error handling for edge cases.

 const parseSnapshotSummary = (summary: string): ParsedSummary => {
   const result: ParsedSummary = {
     users: { count: 0, examples: [] },
     projects: { count: 0, examples: [] },
     chapters: { count: 0, examples: [] },
     issues: { count: 0, examples: [] },
     releases: { count: 0, examples: [] },
   }

   if (!summary) return result

   const sections = [
     { key: 'users', pattern: /(\d+) users \(e\.g\.,\s*([^)]+)\)/i },
     { key: 'projects', pattern: /(\d+) projects \(e\.g\.,\s*([^)]+)\)/i },
     { key: 'chapters', pattern: /(\d+) chapters \(e\.g\.,\s*([^)]+)\)/i },
     { key: 'issues', pattern: /(\d+) issues \(e\.g\.,\s*([^)]+)\)/i },
     { key: 'releases', pattern: /(\d+) releases \(e\.g\.,\s*([^)]+)\)/i },
   ]

   sections.forEach((section) => {
     const match = summary.match(section.pattern)
     if (match && match.length >= 3) {
+      const count = parseInt(match[1], 10)
       result[section.key as keyof ParsedSummary] = {
-        count: parseInt(match[1], 10),
+        count: isNaN(count) ? 0 : count,
-        examples: match[2].split(',').map((s) => s.trim()),
+        examples: match[2] ? match[2].split(',').map((s) => s.trim()).filter(Boolean) : [],
       }
     }
   })

   return result
 }

165-195: Well-implemented summary section with responsive grid layout.

The summary section is cleanly implemented with a responsive grid layout and consistent card styling. The conditional rendering ensures it only appears when data is available.

Consider extracting the summary card into a reusable component since all cards follow the same structure:

// Suggested component extraction (not for direct implementation)
const SummaryCard = ({ label, count, examples, icon }) => (
  <div className="flex items-start gap-4 rounded-lg border p-4 shadow-sm dark:border-gray-700">
    <FontAwesomeIcon icon={icon} className="h-6 w-6 text-blue-500" />
    <div>
      <h3 className="text-lg font-semibold text-gray-800 dark:text-gray-100">
        {count} {label}
      </h3>
      <p className="text-sm text-gray-600 dark:text-gray-400">
        {examples.length > 0 ? `e.g., ${examples.join(', ')}` : 'No examples available'}
      </p>
    </div>
  </div>
);

187-189: Handle empty examples array in summary cards.

The current implementation doesn't handle the case where there are no examples for a category.

 <p className="text-sm text-gray-600 dark:text-gray-400">
-  e.g., {data.examples.join(', ')}
+  {data.examples.length > 0 ? `e.g., ${data.examples.join(', ')}` : 'No examples available'}
 </p>
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6267aec and a5eba95.

📒 Files selected for processing (1)
  • frontend/src/app/snapshots/[id]/page.tsx (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Run frontend unit tests
  • GitHub Check: Run backend tests
  • GitHub Check: Run frontend e2e tests
  • GitHub Check: CodeQL (javascript-typescript)
🔇 Additional comments (6)
frontend/src/app/snapshots/[id]/page.tsx (6)

4-11: Well-chosen icons for the summary section.

The selected FontAwesome icons appropriately represent each entity type in the summary section (users, projects, chapters, issues, releases).


30-36: Good type definition for parsed summary data.

The ParsedSummary type provides a clear structure for the summary data with proper typing for counts and examples.


76-76: Good addition of summary state.

Adding a dedicated state variable for the parsed summary data follows good React practices by separating concerns.


84-87: Properly updates state with parsed summary data.

The component correctly integrates the new summary data into the state management flow.


138-147: Simplified loading and error handling.

The conditional rendering for loading and error states is now more concise and easier to follow.


209-209: Good filtering for active chapters and projects.

The addition of filtering for active chapters and projects ensures that only relevant items are displayed to the user.

Also applies to: 221-221

@Yashgupta9330 Yashgupta9330 marked this pull request as ready for review April 18, 2025 16:40
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: 0

🧹 Nitpick comments (1)
frontend/__tests__/unit/pages/SnapshotDetails.test.tsx (1)

102-119: Well-structured test case for new snapshot summary feature

The test case follows the existing patterns in the test file and effectively verifies that the new snapshot summary feature is correctly rendered. It checks for the presence of the summary heading and the specific entity counts that would be parsed from the summary string provided by the backend.

Consider also testing for the presence of the icons that accompany each statistic, since the AI summary mentions that the implementation includes displaying icons alongside the counts.

+ expect(screen.getByTestId('user-icon')).toBeInTheDocument();
+ expect(screen.getByTestId('project-icon')).toBeInTheDocument();
+ expect(screen.getByTestId('chapter-icon')).toBeInTheDocument();
+ expect(screen.getByTestId('issue-icon')).toBeInTheDocument();
+ expect(screen.getByTestId('release-icon')).toBeInTheDocument();

This assumes that the icons have been assigned appropriate test IDs. If not, you might need to adapt the selectors based on how icons are actually implemented.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb190a2 and 3965f77.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/pages/SnapshotDetails.test.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Run backend tests
  • GitHub Check: Run frontend unit tests
  • GitHub Check: Run frontend e2e tests
  • GitHub Check: CodeQL (javascript-typescript)

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
5 Security Hotspots

See analysis details on SonarQube Cloud

@kasya kasya mentioned this pull request Apr 20, 2025
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.

Implement Snapshot summary generation
1 participant