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

feat: add a progress field on applied controls #1443

Merged
merged 5 commits into from
Jan 30, 2025

Conversation

melinoix
Copy link
Contributor

@melinoix melinoix commented Jan 28, 2025

This is a new field. It is an integer between 0 to 100 (percentage). Defaults to 0. Cannot be Null.

When the status is set to “active”, this field is set to 100.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a progress tracking field for applied controls.
    • Introduced ability to filter applied controls by progress.
  • Internationalization

    • Added "Progress" label in English and French translations.
  • User Interface

    • Enhanced applied control policy form with a progress score component.
    • Updated detail view to include the progress field.
  • Backend

    • Implemented automatic progress field updates based on control status.

Copy link

coderabbitai bot commented Jan 28, 2025

Walkthrough

This pull request introduces a new progress_field for applied controls across the backend and frontend of the application. The changes span multiple files, including a Django migration, model updates, view modifications, translation files, and frontend components. The new field allows tracking the progress of applied controls, with a default value of 0 and the ability to automatically set to 100 when the control status is "active".

Changes

File Change Summary
backend/core/migrations/0050_appliedcontrol_progress_field.py Added migration for new progress_field
backend/core/models.py Added progress_field to AppliedControl model with auto-update logic
backend/core/views.py Added progress_field to filterable fields in AppliedControlViewSet
frontend/messages/en.json Added translation for "Progress" field and reinstated "Red zone radius"
frontend/messages/fr.json Added French translation for "Progress" field and updated "Red zone radius"
frontend/src/lib/components/Forms/ModelForm/AppliedControlPolicyForm.svelte Added Score component for progress field
frontend/src/lib/utils/crud.ts Added progress_field to detail view fields
frontend/src/lib/utils/schemas.ts Added progress_field to AppliedControlSchema

Sequence Diagram

sequenceDiagram
    participant User
    participant Form
    participant Model
    participant Backend
    
    User->>Form: Enters Applied Control
    Form->>Model: Sets progress_field
    Model->>Backend: Saves control
    Backend-->>Model: Validate status
    alt Status is "active"
        Model->>Model: Set progress_field to 100
    end
    Model-->>Form: Return updated control
    Form-->>User: Display control details
Loading

Poem

🐰 A rabbit's tale of progress bright,
Tracking controls with all my might!
From zero to hundred, watch it climb,
A metric dancing in sublime design,
Progress marches on, oh what delight! 🚀

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 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. (Beta)
  • @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

@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: 2

🧹 Nitpick comments (1)
backend/core/models.py (1)

1870-1871: Improve auto-update logic for progress field.

The current implementation only sets progress to 100 when status is "active". Consider these improvements:

  1. Reset progress to 0 when status changes from "active" to any other status
  2. Validate that manual progress updates don't exceed 100

Apply this diff to improve the logic:

-        if self.status == "active":
-            self.progress_field = 100
+        if not self._state.adding:  # Only for updates
+            old_instance = AppliedControl.objects.get(pk=self.pk)
+            if self.status == "active" and old_instance.status != "active":
+                self.progress_field = 100
+            elif self.status != "active" and old_instance.status == "active":
+                self.progress_field = 0
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9dd846b and 4996d56.

📒 Files selected for processing (8)
  • backend/core/migrations/0050_appliedcontrol_progress_field.py (1 hunks)
  • backend/core/models.py (2 hunks)
  • backend/core/views.py (2 hunks)
  • frontend/messages/en.json (1 hunks)
  • frontend/messages/fr.json (1 hunks)
  • frontend/src/lib/components/Forms/ModelForm/AppliedControlPolicyForm.svelte (2 hunks)
  • frontend/src/lib/utils/crud.ts (1 hunks)
  • frontend/src/lib/utils/schemas.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • backend/core/migrations/0050_appliedcontrol_progress_field.py
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: test (3.12)
  • GitHub Check: ruff (3.12)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (7)
frontend/src/lib/components/Forms/ModelForm/AppliedControlPolicyForm.svelte (1)

7-7: LGTM! Verify the translation key exists.

The <Score> component integration looks good with appropriate configuration for progress tracking (0-100 range). The implementation aligns with the PR objectives.

Let's verify that the translation key exists in the messages files:

Also applies to: 143-151

✅ Verification successful

Translation key verification successful

The progress translation key is properly defined in all language files and contains appropriate values.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the progress translation key exists in the messages files.

echo "Checking for progress translation key in message files..."
rg -l "progress[\"']?\s*:" frontend/messages/

Length of output: 564


Script:

#!/bin/bash
# Check the actual content of the progress key in the English messages file
echo "Checking content of progress key in English messages file..."
cat frontend/messages/en.json | jq '.progress'

Length of output: 188

frontend/src/lib/utils/schemas.ts (1)

156-157: LGTM! Schema definition matches the backend model.

The progress_field schema is correctly defined as an optional number with default value 0, matching the backend model definition.

frontend/src/lib/utils/crud.ts (1)

266-267: LGTM! Field added to detail view configuration.

The progress_field is correctly added to detailViewFields, enabling progress tracking in detail views.

backend/core/views.py (2)

1058-1058: LGTM! Progress field added to filter options.

The addition of progress_field to the filterset fields enables filtering of applied controls based on their progress value, which aligns with the PR objectives.


1412-1412: LGTM! Progress field added to duplicate functionality.

The addition of progress_field ensures that the progress value is properly carried over when duplicating an applied control.

frontend/messages/en.json (1)

1143-1144: LGTM! Translations added for progress field.

The translations are properly added:

  1. redZoneRadius translation is reinstated
  2. progressField translation is added for the new progress field
frontend/messages/fr.json (1)

1142-1143: LGTM! The translations are accurate.

Both translations are grammatically correct and accurately represent their English counterparts:

  • "Rayon de la zone rouge" correctly translates "red zone radius"
  • "Progrès" correctly translates "progress field"

Comment on lines 1857 to 1858
progress_field = models.IntegerField(default=0, verbose_name=_("Progress Field"))

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Create a migration file for the new field.

A migration file is needed to add the progress_field to the database schema.

Run Django's makemigrations command to generate the migration file:

#!/bin/bash
# Description: Check if migration file exists and show how to create it.

echo "Checking for migration file..."
find backend/core/migrations -type f -name "*_appliedcontrol_progress_field.py"

echo -e "\nIf no migration file is found, create one by running:"
echo "python manage.py makemigrations core"

🛠️ Refactor suggestion

Add validators to ensure progress stays within 0-100 range.

The progress_field should include validators to ensure values stay within the valid range.

Apply this diff to add validators:

-    progress_field = models.IntegerField(default=0, verbose_name=_("Progress Field"))
+    progress_field = models.IntegerField(
+        default=0,
+        verbose_name=_("Progress Field"),
+        validators=[
+            MinValueValidator(0, message="Progress cannot be less than 0"),
+            MaxValueValidator(100, message="Progress cannot be more than 100")
+        ]
+    )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
progress_field = models.IntegerField(default=0, verbose_name=_("Progress Field"))
progress_field = models.IntegerField(
default=0,
verbose_name=_("Progress Field"),
validators=[
MinValueValidator(0, message="Progress cannot be less than 0"),
MaxValueValidator(100, message="Progress cannot be more than 100")
]
)

@ab-smith
Copy link
Contributor

ab-smith commented Jan 30, 2025

@melinoix the color scheme should be inverted: going from red (0) to green (100),
also coderabbit suggestion about a validator at the model level is a good thing that the API will benefit from.

…hub.com:intuitem/ciso-assistant-community into CA-839-add-a-progress-field-on-applied-controls
Copy link

@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 (1)
backend/core/models.py (1)

1857-1864: Add help text to document the field's behavior.

Consider adding help_text to document that the progress field represents a percentage (0-100) and its automatic update behavior when status changes to "active".

     progress_field = models.IntegerField(
         default=0,
         verbose_name=_("Progress Field"),
+        help_text=_("Progress percentage (0-100). Automatically set to 100 when status becomes active."),
         validators=[
             MinValueValidator(0, message="Progress cannot be less than 0"),
             MaxValueValidator(100, message="Progress cannot be more than 100"),
         ],
     )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 99f7dc8 and 74900a4.

📒 Files selected for processing (3)
  • backend/core/migrations/0050_appliedcontrol_progress_field.py (1 hunks)
  • backend/core/models.py (3 hunks)
  • frontend/src/lib/components/Forms/ModelForm/AppliedControlPolicyForm.svelte (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • backend/core/migrations/0050_appliedcontrol_progress_field.py
  • frontend/src/lib/components/Forms/ModelForm/AppliedControlPolicyForm.svelte
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: test (3.12)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (javascript-typescript)

Comment on lines +1877 to +1878
if self.status == "active":
self.progress_field = 100
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve status handling in the save method.

Consider these improvements:

  1. Use the Status enum instead of string literal for better type safety
  2. Handle the case when status changes from "active" to another status
-        if self.status == "active":
+        if self.status == self.Status.ACTIVE:
             self.progress_field = 100
+        elif self.status != self.Status.ACTIVE and self._state.adding is False:
+            # Only update progress if this is not a new instance
+            original = type(self).objects.get(pk=self.pk)
+            if original.status == self.Status.ACTIVE:
+                # Reset progress when moving from active to another status
+                self.progress_field = 0

Committable suggestion skipped: line range outside the PR's diff.

@ab-smith ab-smith merged commit b3c334d into main Jan 30, 2025
21 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2025
@ab-smith ab-smith deleted the CA-839-add-a-progress-field-on-applied-controls branch January 30, 2025 17:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants