Skip to content

UN-2408 [FEAT] Added per-organization configuration overrides for system settings#1330

Merged
athul-rs merged 10 commits into
mainfrom
UN-2408-support-dynamic-configuration
Jul 14, 2025
Merged

UN-2408 [FEAT] Added per-organization configuration overrides for system settings#1330
athul-rs merged 10 commits into
mainfrom
UN-2408-support-dynamic-configuration

Conversation

@muhammad-ali-e

@muhammad-ali-e muhammad-ali-e commented May 23, 2025

Copy link
Copy Markdown
Contributor

What

  • Added a robust per-organization configuration override system for dynamic system settings
  • Implemented the Configuration model with type-safe value validation and enum-based key management
  • Added comprehensive error handling with automatic fallback to default values for invalid configurations
  • Applied organization-specific configuration for MAX_PARALLEL_FILE_BATCHES with environment-configurable limits
  • Enhanced the system with min/max value constraints and logging for invalid values

Why

  • Different organizations require customized system behavior (e.g., varying batch sizes based on infrastructure capacity)
  • Provides flexibility to optimize performance per organization without affecting global settings
  • Enables safe configuration management with validation and automatic fallbacks
  • Supports future scalability for organization-specific timeouts, feature flags, and other system parameters
  • Improves system reliability by preventing invalid configuration values (zero, negative, out-of-range)

How

  • Created a new Configuration model in the configuration app with organization-scoped key-value overrides
  • Implemented enum-based configuration keys with type casting and validation constraints
  • Added robust value validation with min/max limits and automatic fallback to defaults
  • Enhanced error handling with comprehensive logging for debugging invalid configurations
  • Made maximum value limits environment-configurable via MAX_PARALLEL_FILE_BATCHES_MAX_VALUE
  • Updated configuration resolution logic: organization override > default system config
  • Refactored workflow batch processing to use the new configuration system

Can this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)

No - This PR is designed to be backward compatible:

  • All existing functionality continues to work with default settings when no organization overrides exist
  • The configuration system includes comprehensive fallback mechanisms to default values
  • Value validation prevents invalid configurations that could break workflow processing
  • No existing APIs or workflows are modified, only enhanced with organization-specific capabilities

Database Migrations

  • Yes - New configuration table added for storing organization-level configuration overrides
  • Migration includes proper constraints and field validation

Env Config

  • Added MAX_PARALLEL_FILE_BATCHES_MAX_VALUE environment variable for configurable upper limits
  • Updated sample.env with the new configuration option

Relevant Docs

  • Added comprehensive docstrings explaining the architectural decision to keep configuration separate from platform settings
  • Included validation and error handling documentation

Related Issues or PRs

  • Addresses JIRA ticket UN-2408

Dependencies Versions

  • No new dependencies added

Notes on Testing

  • Added edge case testing for zero, negative, null, and out-of-range values
  • Configuration system includes comprehensive error handling and logging
  • All invalid values automatically fall back to safe defaults

Screenshots

N/A - Backend configuration system

Checklist

I have read and understood the Contribution Guidelines.

Comment thread backend/configuration/models.py Outdated
Comment thread backend/configuration/models.py
Comment thread backend/workflow_manager/workflow_v2/workflow_helper.py Outdated
Comment thread backend/configuration/models.py
@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Jul 10, 2025

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Introduced organization-level configuration management, allowing system settings to be customized per organization.
    • Added support for configuring the maximum number of parallel file batches, with validation and default limits.
    • Provided a new admin-accessible model for managing configuration overrides.
  • Chores

    • Updated environment variable documentation to include new configuration options.

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration system allowing organization-specific settings to be managed in the app.
    • Added support for storing, retrieving, and casting typed configuration values per organization.
    • Configuration values now include validation and type handling (integer, string, boolean, JSON).
  • Improvements

    • Batch processing behavior now dynamically adapts based on organization-specific configuration instead of a fixed value.

Walkthrough

A new Django app named "configuration" is introduced, including its model, enums, and migration for storing per-organization configuration values. The Django project settings are updated to include this app. The workflow batching logic is refactored to fetch its configuration dynamically from the database using the new configuration system instead of a static settings value.

Changes

File(s) Change Summary
backend/backend/settings/base.py Added MAX_PARALLEL_FILE_BATCHES and MAX_PARALLEL_FILE_BATCHES_MAX_VALUE settings; included "configuration" in SHARED_APPS.
backend/sample.env Added environment variable MAX_PARALLEL_FILE_BATCHES_MAX_VALUE.
backend/configuration/apps.py Added Django app config class ConfigurationConfig for the new "configuration" app.
backend/configuration/enums.py Added enums and dataclasses for configuration types, keys, and value casting logic with validation.
backend/configuration/migrations/0001_initial.py Added initial migration for new Configuration model with fields, constraints, and organization linkage.
backend/configuration/models.py Added Configuration model with per-organization config storage, value casting, validation, and retrieval logic.
backend/workflow_manager/workflow_v2/workflow_helper.py Refactored file batching logic to fetch batch size from the new configuration model per organization.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant WorkflowHelper
    participant Configuration
    participant Database

    User->>WorkflowHelper: get_file_batches(user, files)
    WorkflowHelper->>Configuration: get_value_by_organization(ConfigKey, user.organization)
    Configuration->>Database: Query Configuration for (organization, ConfigKey)
    Database-->>Configuration: Return config value (or None)
    Configuration-->>WorkflowHelper: Return typed config value (or default)
    WorkflowHelper-->>User: Return file batches (using config value)
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to Reviews > Disable Cache setting
Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 6ab4443 and 3ecd2e5.

📒 Files selected for processing (2)
  • backend/backend/settings/base.py (2 hunks)
  • backend/configuration/enums.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • backend/backend/settings/base.py
  • backend/configuration/enums.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (3)
backend/workflow_manager/workflow_v2/workflow_helper.py (1)

118-125: Clarify the fallback logic and improve readability.

The implementation successfully introduces dynamic configuration, but there are several concerns:

  1. Redundant fallback: The or 1 condition may be unnecessary if Configuration.get_value_by_organization already returns the default value when no configuration is found.

  2. Hardcoded default: The hardcoded 1 should be extracted as a named constant for better readability.

  3. Missing organization handling: Consider what happens when UserContext.get_organization() returns None.

Apply this refactor to improve readability and remove redundancy:

+DEFAULT_BATCH_SIZE = 1
+
 # Prepare batches of files for parallel processing
 organization = UserContext.get_organization()
-BATCH_SIZE = (
-    Configuration.get_value_by_organization(
-        config_key=ConfigKey.MAX_PARALLEL_FILE_BATCHES, organization=organization
-    )
-    or 1
-)  # Max number of batches
+BATCH_SIZE = Configuration.get_value_by_organization(
+    config_key=ConfigKey.MAX_PARALLEL_FILE_BATCHES, organization=organization
+) or DEFAULT_BATCH_SIZE

Let me verify if the or condition is needed by examining the Configuration model implementation:

#!/bin/bash
# Check if Configuration.get_value_by_organization can return None/falsy values
ast-grep --pattern 'def get_value_by_organization($$$) {
  $$$
}'
backend/configuration/models.py (2)

11-31: Consider the relationship with existing platform settings.

The past review comment raises a valid concern about whether this should be part of existing platform settings. Consider documenting the distinction between platform-wide settings and organization-specific configurations to clarify when to use each approach.

Document the design decision about when to use this Configuration model versus existing platform settings to help future developers understand the separation of concerns.


18-18: Consider using an enum for the key field to improve type safety.

Using a string field for the key reduces type safety and makes it prone to typos. Consider using an enum or choices to constrain the possible values.

Apply this change to improve type safety:

+from django.db import models
+from configuration.enums import ConfigKey
+
 class Configuration(BaseModel):
     # ... other fields ...
-    key = models.CharField(max_length=100)
+    key = models.CharField(
+        max_length=100,
+        choices=[(config_key.name, config_key.value.name) for config_key in ConfigKey]
+    )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to Reviews > Disable Cache setting
Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 175f24c and 1d69ad5.

📒 Files selected for processing (6)
  • backend/backend/settings/base.py (1 hunks)
  • backend/configuration/apps.py (1 hunks)
  • backend/configuration/enums.py (1 hunks)
  • backend/configuration/migrations/0001_initial.py (1 hunks)
  • backend/configuration/models.py (1 hunks)
  • backend/workflow_manager/workflow_v2/workflow_helper.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (9)
backend/backend/settings/base.py (1)

297-297: LGTM! Configuration app properly integrated.

The addition of the "configuration" app to SHARED_APPS is correctly placed and follows Django conventions for registering new apps.

backend/configuration/apps.py (1)

1-7: LGTM! Standard Django app configuration.

The ConfigurationConfig class follows Django best practices and is consistent with the project's field type conventions.

backend/workflow_manager/workflow_v2/workflow_helper.py (1)

14-15: LGTM! Imports updated for dynamic configuration.

The import changes correctly replace the static Django settings with the new dynamic configuration system.

backend/configuration/migrations/0001_initial.py (1)

1-56: LGTM! Well-structured migration for Configuration model.

The migration correctly defines the Configuration model with:

  • Proper UUID primary key
  • Foreign key relationship to Organization
  • Unique constraint on organization+key for data integrity
  • Appropriate field types and constraints

The migration follows Django best practices and will support the dynamic configuration system effectively.

backend/configuration/models.py (1)

41-56: The method correctly handles default values on errors.

The get_value_by_organization method properly returns the default value from the ConfigKey when:

  • Organization is None
  • Configuration doesn't exist
  • Configuration is disabled
  • Value parsing fails

This addresses the concern raised in the past review comment about returning default values on errors.

backend/configuration/enums.py (4)

1-7: LGTM: Clean imports and dependencies

The import structure is well-organized and includes all necessary dependencies for the configuration system.


9-13: LGTM: Well-defined configuration types

The ConfigType enum provides a clear set of supported configuration value types that covers common use cases.


16-20: LGTM: Immutable configuration specification

The frozen dataclass provides a clean, immutable way to define configuration specifications with proper type hints.


24-28: Django setting “MAX_PARALLEL_FILE_BATCHES” is defined
Confirmed that MAX_PARALLEL_FILE_BATCHES is declared in backend/backend/settings/base.py, so referencing settings.MAX_PARALLEL_FILE_BATCHES in backend/configuration/enums.py will not raise an import-time error.

Comment thread backend/configuration/models.py
Comment thread backend/configuration/enums.py
@muhammad-ali-e muhammad-ali-e changed the title dynamic env configurations with Db table UN-2408 [FEAT] Added per-organization configuration overrides for system settings Jul 10, 2025
muhammad-ali-e and others added 3 commits July 10, 2025 13:27
- Convert Configuration.key to enum validation with clean() method
- Add robust value validation with min/max constraints
- Remove redundant DEFAULT_BATCH_SIZE and fallback logic
- Add environment-configurable MAX_PARALLEL_FILE_BATCHES_MAX_VALUE
- Update to modern Python type hints (Organization | None)
- Enhance error handling with comprehensive logging
- Add comprehensive docstrings explaining architectural decisions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Fix typed_value property to catch specific exceptions (ValueError, KeyError, TypeError)
- Fix redundant exception handling in get_value_by_organization (json.JSONDecodeError inherits from ValueError)
- Use generic Exception for comprehensive error handling in configuration retrieval
- Maintain robust fallback behavior while improving code quality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

@gaya3-zipstack gaya3-zipstack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

Comment thread backend/configuration/enums.py
Comment thread backend/configuration/models.py
Comment thread backend/backend/settings/base.py
@github-actions

Copy link
Copy Markdown
Contributor
filepath function $$\textcolor{#23d18b}{\tt{passed}}$$ SUBTOTAL
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_logs}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_cleanup}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_cleanup\_skip}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_client\_init}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_get\_image\_exists}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_get\_image}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_get\_container\_run\_config}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_get\_container\_run\_config\_without\_mount}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_run\_container}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_get\_image\_for\_sidecar}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{runner/src/unstract/runner/clients/test\_docker.py}}$$ $$\textcolor{#23d18b}{\tt{test\_sidecar\_container}}$$ $$\textcolor{#23d18b}{\tt{1}}$$ $$\textcolor{#23d18b}{\tt{1}}$$
$$\textcolor{#23d18b}{\tt{TOTAL}}$$ $$\textcolor{#23d18b}{\tt{11}}$$ $$\textcolor{#23d18b}{\tt{11}}$$

@sonarqubecloud

Copy link
Copy Markdown

@athul-rs athul-rs merged commit 21f71e8 into main Jul 14, 2025
7 checks passed
@athul-rs athul-rs deleted the UN-2408-support-dynamic-configuration branch July 14, 2025 05:05
pk-zipstack pushed a commit that referenced this pull request Aug 20, 2025
…tem settings (#1330)

* dynamic env configurations with Db table

* Address PR review comments and enhance configuration validation

- Convert Configuration.key to enum validation with clean() method
- Add robust value validation with min/max constraints
- Remove redundant DEFAULT_BATCH_SIZE and fallback logic
- Add environment-configurable MAX_PARALLEL_FILE_BATCHES_MAX_VALUE
- Update to modern Python type hints (Organization | None)
- Enhance error handling with comprehensive logging
- Add comprehensive docstrings explaining architectural decisions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix Sonar and CodeRabbit exception handling issues

- Fix typed_value property to catch specific exceptions (ValueError, KeyError, TypeError)
- Fix redundant exception handling in get_value_by_organization (json.JSONDecodeError inherits from ValueError)
- Use generic Exception for comprehensive error handling in configuration retrieval
- Maintain robust fallback behavior while improving code quality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* added docstring

---------

Co-authored-by: Gayathri <142381512+gaya3-zipstack@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
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.

5 participants