Skip to content

Conversation

@dushaniw
Copy link
Contributor

@dushaniw dushaniw commented Feb 2, 2026

Overview

This PR refactors the GetDeploymentsWithState method to enforce a soft limit of N records per Gateway while ensuring that current lifecycle states (DEPLOYED/UNDEPLOYED) are never truncated by the history limit.

Problem

Previously, a simple ORDER BY created_at could cause active deployments to be "pushed out" of the visible list if a high volume of new deployments were created (even if those new ones weren't actually deployed). Additionally, we lacked a per-gateway limit when querying across multiple gateways for a single API.

Changes

  • Weighted Ranking: Introduced a ROW_NUMBER() window function partitioned by gateway_uuid.

  • Prioritization Logic: Applied a CASE statement inside the ranking to ensure that any record existing in the api_deployment_status table (Priority 0) always receives rank_idx = 1.

  • Soft Limit Enforcement: Added a constraint to only return the top N records per gateway.

  • State Mapping: Improved the LEFT JOIN logic to correctly identify ARCHIVED deployments (those within the top N history that have no entry in the status table).

  • Filtering: Updated the query to support combined filters for gatewayID and status without breaking the ranking window.

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced status validation for deployment state queries.
  • Performance

    • Optimized deployment retrieval with per-Gateway limits, prioritizing active and recent deployments.
    • Added database indexing to accelerate deployment-related queries.
  • Refactor

    • Simplified deployment existence checks for API-Gateway pairs.

Copilot AI review requested due to automatic review settings February 2, 2026 17:09
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 2, 2026

Warning

Rate limit exceeded

@dushaniw has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 23 minutes and 50 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

The changes implement per-gateway deployment limiting with status-based ranking. A database index was added, the repository query was refactored to use CTEs for ranking deployments per gateway, and calling code was updated to propagate a configurable limit throughout the service and repository layers. One unused method was removed.

Changes

Cohort / File(s) Summary
Database Schema
platform-api/src/internal/database/schema.postgres.sql
Added index idx_api_gw_created on api_deployments table to accelerate queries filtering by API, organization, gateway, and creation timestamp.
Repository Core Logic
platform-api/src/internal/repository/api.go, platform-api/src/internal/repository/interfaces.go
Expanded GetDeploymentsWithState signature with maxPerAPIGW parameter; refactored query to use CTE-based ranking per gateway by status (active first) and recency; added status value validation; removed CountDeploymentsByAPIAndGateway method.
Service Layer
platform-api/src/internal/service/deployment.go, platform-api/src/internal/service/deployment_test.go
Updated GetDeployments and test mock to pass maxPerAPIGW from configuration to repository method.
Gateway Internal Service
platform-api/src/internal/service/gateway_internal.go
Simplified deployment existence check by replacing iteration over all deployments with direct call to GetDeploymentStatus, reducing database query burden.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hopping through the gateways with ranking so neat,
Per-gateway deployment limits, a feature complete,
Status takes priority, the freshest come first,
A CTE sparkle quenches the database's thirst!
One method hops away, the code now runs lighter,
Configuration-driven limits make rabbit delight brighter!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Description check ❓ Inconclusive The PR description provides a clear overview of the problem, solution, and implementation approach, but does not follow the repository's required template structure with all sections. Consider restructuring the description to align with the template sections: Purpose, Goals, Approach, User Stories, Documentation, Automation Tests, Security Checks, Samples, Related PRs, and Test Environment.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title covers the main change (soft limits for deployments retrieval) but is somewhat verbose with dual descriptions separated by a pipe character.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the GetDeploymentsWithState method to enforce a soft limit on the number of deployment records retrieved per gateway while ensuring that currently deployed/undeployed states are always prioritized over archived deployments.

Changes:

  • Introduced window function-based ranking with ROW_NUMBER() to limit results per gateway
  • Modified query to prioritize active deployments (DEPLOYED/UNDEPLOYED) over archived ones
  • Optimized deployment existence check in gateway_internal.go to use GetDeploymentStatus instead of GetDeploymentsWithState

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
platform-api/src/internal/repository/api.go Refactored GetDeploymentsWithState with CTE-based window function query to implement soft limits and prioritization; removed unused CountDeploymentsByAPIAndGateway method
platform-api/src/internal/repository/interfaces.go Added maxPerAPIGW parameter to GetDeploymentsWithState signature; removed CountDeploymentsByAPIAndGateway from interface
platform-api/src/internal/service/deployment.go Updated GetDeploymentsWithState call to pass config value for maxPerAPIGW
platform-api/src/internal/service/gateway_internal.go Changed from GetDeploymentsWithState to GetDeploymentStatus for more efficient deployment existence check
platform-api/src/internal/service/deployment_test.go Updated mock signature to match new GetDeploymentsWithState interface
platform-api/src/internal/database/schema.postgres.sql Added composite index idx_api_gw_created for optimizing the window function query

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.

2 participants