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: Optimize job policy query #153

Merged
merged 2 commits into from
Oct 21, 2024
Merged

Conversation

adityachoudhari26
Copy link
Contributor

@adityachoudhari26 adityachoudhari26 commented Oct 21, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced job selection logic to include only jobs that are pending and passing the approval gate.
    • Improved filtering based on environment policy approvals for better control over job triggers.
  • Bug Fixes

    • Adjusted conditions to ensure accurate job status evaluation.

Copy link
Contributor

coderabbitai bot commented Oct 21, 2024

Walkthrough

The pull request modifies the run function in the apps/jobs/src/policy-checker/index.ts file. It introduces new imports and defines a constant to evaluate the status of the environmentPolicyApproval. The query for releaseJobTriggers has been revised to include additional joins with related tables and to utilize the new condition for filtering jobs based on their approval status. The logic for selecting jobs that are pending and passing the approval gate has been enhanced.

Changes

File Path Change Summary
apps/jobs/src/policy-checker/index.ts - Added imports for and, isNull, and or from @ctrlplane/db.
- Defined isPassingApprovalGate constant.
- Revised query for releaseJobTriggers with additional inner and left joins and modified filtering conditions.

Possibly related PRs

  • refactor job policy checker app #151: The changes in this PR involve modifications to the apps/jobs/src/policy-checker/index.ts file, specifically related to the run function, which is also a focus of the main PR. The visibility of the run function is altered, indicating a direct connection between the two PRs.

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between dc05138 and e1b28ed.

📒 Files selected for processing (1)
  • apps/jobs/src/policy-checker/index.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/jobs/src/policy-checker/index.ts

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.

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

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e510b04 and dc05138.

📒 Files selected for processing (1)
  • apps/jobs/src/policy-checker/index.ts (2 hunks)
🧰 Additional context used
🔇 Additional comments (3)
apps/jobs/src/policy-checker/index.ts (3)

21-24: Inner joining with environment table

The innerJoin with schema.environment correctly associates each releaseJobTrigger with its corresponding environment. This ensures that only triggers linked to existing environments are considered.


25-41: Review of left joins with environmentPolicy and environmentPolicyApproval

The use of leftJoin with schema.environmentPolicy and schema.environmentPolicyApproval allows for the inclusion of policy-related data when available while retaining triggers that may not have associated policies or approvals. This approach is appropriate for handling optional relationships.


42-42: Verify the combined conditions in the where clause

The where clause uses and(eq(schema.job.status, JobStatus.Pending), isPassingApprovalGate). Given the potential null values in status, ensure that this condition accurately filters jobs that are pending and either have passed the approval gate or do not require approval.

To confirm the logic, you might run a test query to check the selected records:

This script helps verify that the where clause includes all intended jobs.

Comment on lines 12 to 15
const isPassingApprovalGate = notInArray(
schema.environmentPolicyApproval.status,
["pending", "rejected"],
);
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle null values in isPassingApprovalGate condition

Since environmentPolicyApproval is left-joined, schema.environmentPolicyApproval.status may be null. Using notInArray might exclude records where status is null. If the intention is to include records with null status (i.e., no approval required), consider adjusting the condition to handle null values.

You could modify isPassingApprovalGate to include null statuses:

-import { and, eq, notInArray } from "@ctrlplane/db";
+import { and, eq, notInArray, or, isNull } from "@ctrlplane/db";

 const isPassingApprovalGate = or(
   notInArray(
     schema.environmentPolicyApproval.status,
     ["pending", "rejected"],
   ),
+  isNull(schema.environmentPolicyApproval.status),
 );

This adjustment ensures that records without an environmentPolicyApproval are included in the results.

Committable suggestion was skipped due to low confidence.

@adityachoudhari26 adityachoudhari26 merged commit 5358ede into main Oct 21, 2024
6 checks passed
@adityachoudhari26 adityachoudhari26 deleted the optimize-job-policy-query branch October 21, 2024 01:41
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.

1 participant