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 dev docker file and enable sql logging #2649

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

sainak
Copy link
Member

@sainak sainak commented Dec 11, 2024

Proposed Changes

  • fix dev docker file and enable sql logging

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • New Features

    • Introduced conditional logic for profiling features in Django settings based on environment variables.
    • Added a new variable to control debugger attachment in the environment configuration.
  • Bug Fixes

    • Updated debugging mode in the environment configuration to enable debugging.
  • Refactor

    • Optimized the Dockerfile for improved installation efficiency and architecture compatibility.
    • Streamlined server startup process in the development script for consistent behavior.

@sainak sainak requested a review from a team as a code owner December 11, 2024 14:20
Copy link

coderabbitai bot commented Dec 11, 2024

📝 Walkthrough

Walkthrough

The pull request introduces several changes to the Django application configuration and Docker setup. It modifies the Django settings to conditionally include the django-silk application based on the ENABLE_SILK environment variable. The .local.env file is updated to enable debugging mode and introduce a new variable for attaching a debugger. The Dockerfile optimizes dependency management and adds architecture checks for Typst installation. Lastly, the start-dev.sh script streamlines server startup behavior by consistently using runserver_plus based on the new debugging variable.

Changes

File Change Summary
config/settings/local.py Added conditional logic for django-silk inclusion based on ENABLE_SILK environment variable.
docker/.local.env Changed DJANGO_DEBUG from False to true and added ATTACH_DEBUGGER set to false.
docker/dev.Dockerfile Optimized installation process with build cache and added architecture checks for Typst.
scripts/start-dev.sh Modified server startup logic to use ATTACH_DEBUGGER for consistent use of runserver_plus.

Possibly related PRs

  • fixed dockerfile for local plugin installation #2622: The changes in the docker/dev.Dockerfile regarding the installation process and handling of plugins may relate to the overall configuration and environment setup, which is relevant to the changes made in the main PR regarding environment variables and application settings.

Suggested reviewers

  • vigneshhari
  • rithviknishad

🎉 In the world of code, changes take flight,
Debugging's now easier, everything feels right.
With silk wrapped in logic, and Docker refined,
The devs cheer with joy, no bugs left behind!
So here’s to the tweaks, both big and small,
A toast to the changes—let’s celebrate them all! 🎊


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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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

🧹 Outside diff range and nitpick comments (1)
config/settings/local.py (1)

22-24: LGTM, but the SILKY_PYTHON_PROFILER setting needs attention.

The conditional loading of django-silk is a nice touch for optimizing development resources. However, I couldn't help but notice that the SILKY_PYTHON_PROFILER = True setting below remains unconditional, which might cause issues when silk is disabled.

Consider wrapping the SILKY_PYTHON_PROFILER setting in the same conditional block:

if env("ENABLE_SILK", default=False):
    INSTALLED_APPS += ["silk"]
    MIDDLEWARE += ["silk.middleware.SilkyMiddleware"]
+   SILKY_PYTHON_PROFILER = True
-SILKY_PYTHON_PROFILER = True
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2b4f1dd and 21181ce.

📒 Files selected for processing (4)
  • config/settings/local.py (1 hunks)
  • docker/.local.env (1 hunks)
  • docker/dev.Dockerfile (1 hunks)
  • scripts/start-dev.sh (1 hunks)
🔇 Additional comments (4)
config/settings/local.py (1)

Line range hint 1-100: Verify development-only settings.

Just want to make sure these development settings (CORS_ORIGIN_ALLOW_ALL, DISABLE_RATELIMIT) aren't somehow finding their way into production.

✅ Verification successful

Development settings are properly isolated, though you might want to double-check that environment variable

Well, well, well... looks like everything is actually quite properly set up (how refreshing). The production environment:

  • Uses CORS_ALLOWED_ORIGINS with an explicit list instead of allowing all origins
  • Doesn't have CORS_ORIGIN_ALLOW_ALL anywhere in sight
  • Has DISABLE_RATELIMIT defaulting to False in base.py, and isn't overridden

Though, I suppose I should mention that DISABLE_RATELIMIT is controlled by an environment variable. You might want to ensure it's explicitly set to False in your production environment, just in case someone gets any funny ideas about environment variables.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if these development settings exist in production settings
echo "Checking for potentially unsafe settings in production..."
rg -l "CORS_ORIGIN_ALLOW_ALL\s*=\s*True|DISABLE_RATELIMIT\s*=\s*True" config/settings/production.py

Length of output: 226


Script:

#!/bin/bash
# Let's check if production.py exists and see its content
echo "First, checking if production.py exists..."
fd -t f "production.py" config/settings/

echo -e "\nChecking production settings content..."
cat config/settings/production.py 2>/dev/null || echo "File not found"

echo -e "\nChecking for CORS settings in production..."
rg "CORS_" config/settings/production.py || echo "No CORS settings found"

echo -e "\nChecking for rate limit settings in production..."
rg "RATELIMIT" config/settings/production.py || echo "No rate limit settings found"

# Let's also check if there are any other settings files that might be used in production
echo -e "\nChecking for other potential settings files..."
fd -t f . config/settings/

Length of output: 1270


Script:

#!/bin/bash
# Let's check deployment.py since production.py imports from it
echo "Checking deployment.py content..."
cat config/settings/deployment.py 2>/dev/null || echo "File not found"

echo -e "\nChecking for CORS settings in deployment.py..."
rg "CORS_" config/settings/deployment.py || echo "No CORS settings found"

echo -e "\nChecking for rate limit settings in deployment.py..."
rg "RATELIMIT" config/settings/deployment.py || echo "No rate limit settings found"

# Let's also check base.py for default settings
echo -e "\nChecking relevant settings in base.py..."
rg "(CORS_|RATELIMIT)" config/settings/base.py || echo "No relevant settings found in base.py"

Length of output: 5861

scripts/start-dev.sh (1)

14-18: Oh, I see we're finally standardizing on runserver_plus...

The changes look reasonable, though it's interesting that we're now printing SQL queries regardless of the debug state. I suppose more information is... better?

Let's make sure this won't flood our logs too much:

✅ Verification successful

SQL logging seems... manageable, I suppose

Looking at the query patterns across the codebase, most of them are simple filters and joins that won't generate particularly verbose SQL. The queries are primarily:

  • Basic filtering on foreign keys and boolean fields
  • Simple text searches and annotations
  • Standard Django ORM operations (select_related, prefetch_related)

While we'll see more SQL in the logs, it won't be overwhelming since there aren't any particularly complex or chatty queries that would flood the output.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if we have any particularly chatty SQL queries in our codebase
rg -l "select|SELECT" --type py | while read -r file; do
  echo "=== $file ==="
  ast-grep --pattern 'queryset.$$$' "$file"
done

Length of output: 18229

docker/.local.env (1)

10-11: Ah, enabling debug mode by default... how brave.

While this makes sense for a development environment, let's make absolutely sure this won't accidentally slip into production:

✅ Verification successful

Well, someone has their debug settings under control... mostly.

The debug flag appears to be properly configured:

  • Only .local.env has DEBUG set to true
  • The Django settings use environment variables with a safe default (DEBUG = env.bool("DJANGO_DEBUG", False))
  • Production configurations in .prebuilt.env don't show any concerning debug settings
  • Docker Compose files correctly separate development and production environments

Though I suppose it wouldn't hurt to have a .env.example file to prevent any... unfortunate misconfigurations in the future.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for any production environment files that might have DEBUG enabled
rg -l "DJANGO_DEBUG.*true" --glob '*.env' --glob '*.env.*' | grep -v "local.env"

# Also check for any Django settings files that might have hardcoded DEBUG=True
ast-grep --pattern 'DEBUG = True'

Length of output: 116


Script:

#!/bin/bash
# Let's cast a wider net for any debug-related settings in env files
rg -l "DEBUG.*=.*true" --glob '*.env*'

# Check for Django settings with DEBUG in a more flexible way
ast-grep --pattern 'DEBUG = $_'

# Look for any docker-compose files that might reference these env files
fd -e yml -e yaml docker-compose | xargs cat

# And check other potential env templates
fd ".env.template|.env.example|.env.sample"

Length of output: 2800

docker/dev.Dockerfile (1)

Line range hint 14-26: Proper architecture handling - how thoughtful.

The architecture check for Typst installation is well implemented, though it might be worth noting that we're making some assumptions about supported architectures.

Let's check if we handle architecture-specific cases consistently:

✅ Verification successful

Architecture handling is consistently implemented across both Dockerfiles

The architecture checks for Typst installation are identical in both dev and prod Dockerfiles, and I see no other architecture-dependent installations that might have been... overlooked. The implementation gracefully handles both amd64 and arm64 architectures while thoughtfully failing on unsupported ones - which is more than what some other projects do.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for other architecture-dependent installations
fd Dockerfile | while read -r file; do
  echo "=== $file ==="
  rg -A 5 "dpkg --print-architecture|uname -m" "$file"
done

Length of output: 801


Script:

#!/bin/bash
# Let's check if there are any other architecture-specific binaries or installations
rg -i "download|install|binary|bin" docker/ | grep -i "arch\|x86\|amd64\|arm"

Length of output: 698

Comment on lines +22 to +24
if env("ENABLE_SILK", default=False):
INSTALLED_APPS += ["silk"]
MIDDLEWARE += ["silk.middleware.SilkyMiddleware"]
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Would be lovely if we could add documentation for the ENABLE_SILK environment variable

The local setup documentation in docs/local-setup/configuration.rst seems to be missing any mention of this rather important development feature. Since developers might want to use Django Silk for profiling, it would be quite helpful to document this configuration option alongside other environment variables.

  • Add documentation about ENABLE_SILK environment variable in docs/local-setup/configuration.rst, perhaps under the development environment setup section
  • Include a brief explanation of what Silk does and when developers might want to enable it
🔗 Analysis chain

Documentation update might be nice.

Since this introduces a new environment variable that affects development workflow, it would be absolutely wonderful if we could document this change in the development setup guide.

Let's check if there's a relevant documentation file:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for development setup documentation
fd -t f -e md -e rst "setup|development|configuration" docs/

Length of output: 421


Script:

#!/bin/bash
# Check content of local setup configuration docs
cat docs/local-setup/configuration.rst

# Also check if there's any existing mention of silk in docs
rg -i "silk" docs/

Length of output: 5087

Comment on lines +35 to +36
RUN --mount=type=cache,target=/root/.cache/pip pip install pipenv==2024.4.0

Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Looks like we found a lonely pip install without cache in prod.Dockerfile

The build cache optimization is only implemented in dev.Dockerfile but not in prod.Dockerfile. For consistency and build performance, the same cache mount should be added to the pip install command in prod.Dockerfile as well.

  • docker/prod.Dockerfile: Add --mount=type=cache,target=/root/.cache/pip to the pip install command
🔗 Analysis chain

Finally, someone remembered that build caches exist!

Nice optimization using --mount=type=cache. This should make our builds significantly faster, assuming everyone remembers to enable BuildKit.

Let's check if we're consistent with cache usage across our Dockerfiles:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check other Dockerfiles for potential cache optimization opportunities
fd Dockerfile | while read -r file; do
  echo "=== $file ==="
  rg "pip install" "$file"
done

Length of output: 392

@vigneshhari vigneshhari merged commit 88de800 into develop Dec 11, 2024
6 checks passed
@vigneshhari vigneshhari deleted the sainak/fix-dev-docker-image branch December 11, 2024 14:26
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.63%. Comparing base (2b4f1dd) to head (21181ce).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2649   +/-   ##
========================================
  Coverage    69.63%   69.63%           
========================================
  Files          211      211           
  Lines        11879    11879           
  Branches      1202     1202           
========================================
  Hits          8272     8272           
  Misses        3240     3240           
  Partials       367      367           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sainak sainak self-assigned this Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants