Skip to content

feat: add computation quotas check#1013

Open
GhilesA wants to merge 3 commits into
mainfrom
feat/add_quotas_on_computation
Open

feat: add computation quotas check#1013
GhilesA wants to merge 3 commits into
mainfrom
feat/add_quotas_on_computation

Conversation

@GhilesA

@GhilesA GhilesA commented Jul 6, 2026

Copy link
Copy Markdown

PR Summary

  • send error when max quota is reached
  • expose quota check status (can be enabled if necessary, default quota check is off for now)

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@GhilesA, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 3 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9b0273fb-a5a1-4a8b-8132-ae59614b8195

📥 Commits

Reviewing files that changed from the base of the PR and between 8ba7287 and 09ef1ac.

📒 Files selected for processing (24)
  • src/main/java/org/gridsuite/study/server/controller/StudyController.java
  • src/main/java/org/gridsuite/study/server/dto/QuotaType.java
  • src/main/java/org/gridsuite/study/server/dto/UserProfileInfos.java
  • src/main/java/org/gridsuite/study/server/error/StudyBusinessErrorCode.java
  • src/main/java/org/gridsuite/study/server/service/ConsumerService.java
  • src/main/java/org/gridsuite/study/server/service/StudyService.java
  • src/main/java/org/gridsuite/study/server/service/UserAdminService.java
  • src/main/resources/config/application.yaml
  • src/test/java/org/gridsuite/study/server/NetworkModificationTest.java
  • src/test/java/org/gridsuite/study/server/NodeSequenceTest.java
  • src/test/java/org/gridsuite/study/server/ShortCircuitTest.java
  • src/test/java/org/gridsuite/study/server/StateEstimationTest.java
  • src/test/java/org/gridsuite/study/server/StudyControllerDynamicMarginCalculationTest.java
  • src/test/java/org/gridsuite/study/server/StudyControllerDynamicSecurityAnalysisTest.java
  • src/test/java/org/gridsuite/study/server/StudyControllerDynamicSimulationTest.java
  • src/test/java/org/gridsuite/study/server/StudyServiceTest.java
  • src/test/java/org/gridsuite/study/server/VoltageInitTest.java
  • src/test/java/org/gridsuite/study/server/dto/QuotaTypeTest.java
  • src/test/java/org/gridsuite/study/server/rootnetworks/ModificationToExcludeTest.java
  • src/test/java/org/gridsuite/study/server/service/StudyServiceDynamicSimulationTest.java
  • src/test/java/org/gridsuite/study/server/service/UserAdminServiceTest.java
  • src/test/java/org/gridsuite/study/server/utils/TestUtils.java
  • src/test/java/org/gridsuite/study/server/utils/wiremock/UserAdminServerStubs.java
  • src/test/resources/application-default.yml
📝 Walkthrough

Walkthrough

Adds per-computation quota types and REST operations, enforces quotas before computation запуск, tracks active operations, releases quotas on completion, and updates configuration and tests for the new quota API.

Changes

Quota model and REST integration

Layer / File(s) Summary
Quota types and profile contract
src/main/java/org/gridsuite/study/server/dto/*, src/main/java/org/gridsuite/study/server/error/StudyBusinessErrorCode.java
Adds QuotaType mapping, replaces the scalar build limit with per-type quota values, and adds the quota-exceeded error code.
UserAdminService quota endpoints
src/main/java/org/gridsuite/study/server/service/UserAdminService.java, src/test/java/org/gridsuite/study/server/service/UserAdminServiceTest.java
Adds maximum/current quota retrieval and quota operation start/end REST calls, with endpoint interaction tests.

Computation quota lifecycle

Layer / File(s) Summary
Quota enforcement and computation tracking
src/main/java/org/gridsuite/study/server/service/StudyService.java, src/main/java/org/gridsuite/study/server/controller/StudyController.java, src/test/java/org/gridsuite/study/server/StudyServiceTest.java
Adds configurable preflight checks, quota-based build limits, quota starts for computation entrypoints, and tests for quota availability behavior.
Quota release handling
src/main/java/org/gridsuite/study/server/service/ConsumerService.java
Ends quota-tracked operations when computations fail, stop, or complete successfully.
Quota configuration
src/main/resources/config/application.yaml, src/test/resources/application-default.yml
Adds the study.enable-operation-quotas setting and disables it in test defaults.
Quota-aware test infrastructure
src/test/java/org/gridsuite/study/server/**/*Test.java, src/test/java/org/gridsuite/study/server/utils/*
Updates quota retrieval payloads, operation stubs, mocked services, request filtering, and computation test expectations.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant StudyController
  participant StudyService
  participant UserAdminService
  participant ConsumerService
  Client->>StudyController: run computation
  StudyController->>StudyService: assertOnQuotasAvailability(computationType, userId)
  StudyService->>UserAdminService: get current and maximum quotas
  StudyController->>StudyService: start computation
  StudyService->>UserAdminService: startOperationWithQuota(...)
  ConsumerService->>UserAdminService: endOperationWithQuota(...)
  ConsumerService-->>Client: computation status/result notification
Loading

Suggested reviewers: etiennelt, abdelhedhili, flomillot

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding computation quota checks.
Description check ✅ Passed The description is directly related and matches the quota-check feature.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@GhilesA
GhilesA force-pushed the feat/add_quotas_on_computation branch from 03724ef to 05ec476 Compare July 6, 2026 13:44

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 (2)
src/test/java/org/gridsuite/study/server/utils/TestUtils.java (2)

239-264: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Duplicated removeServeEventsMatching pair across the two teardown helpers.

Consider extracting a small private helper (e.g. removeQuotaServeEvents(WireMockServer)) to avoid repeating the same two lines in assertWiremockServerRequestsEmptyThenShutdown and assertWiremockServerRequestsEmptyThenClear.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/test/java/org/gridsuite/study/server/utils/TestUtils.java` around lines
239 - 264, The two teardown helpers,
assertWiremockServerRequestsEmptyThenShutdown and
assertWiremockServerRequestsEmptyThenClear, repeat the same
removeServeEventsMatching calls for the quota start/end URLs. Extract that
duplicated logic into a small private helper such as
removeQuotaServeEvents(WireMockServer) and call it from both methods so the
quota-event cleanup is centralized and easier to maintain.

112-140: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Narrow the quota-skip regex to avoid masking legitimate GET /quota/max verification.

"/v1/users/.*/quota/.*" also matches a GET .../quota/max request, not just the start/end tracking calls it's meant to filter out. This differs from the precise UserAdminServerStubs.QUOTA_START_URL_PATTERN/QUOTA_END_URL_PATTERN (which require a /start or /end suffix) already imported in this file. If a future MockWebServer-based test needs to verify the max-quota check call, this broader filter would silently skip it.

♻️ Suggested fix reusing the precise patterns
             do {
                 recordedRequest = Objects.requireNonNull(server.takeRequest(TIMEOUT, TimeUnit.MILLISECONDS));
                 // skip quota start/end requests that are auto-handled and not part of test assertions
-            } while (recordedRequest.getPath() != null && recordedRequest.getPath().matches("/v1/users/.*/quota/.*"));
+            } while (recordedRequest.getPath() != null
+                    && (recordedRequest.getPath().matches(UserAdminServerStubs.QUOTA_START_URL_PATTERN)
+                        || recordedRequest.getPath().matches(UserAdminServerStubs.QUOTA_END_URL_PATTERN)));

Apply the same change in getRequestsDone.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/test/java/org/gridsuite/study/server/utils/TestUtils.java` around lines
112 - 140, The quota-request filtering in TestUtils.assertRequestMatches and
getRequestsDone is too broad and can accidentally skip legitimate GET /quota/max
calls. Replace the generic "/v1/users/.*/quota/.*" match with the precise quota
start/end patterns already available in this file via
UserAdminServerStubs.QUOTA_START_URL_PATTERN and
UserAdminServerStubs.QUOTA_END_URL_PATTERN, and apply the same narrowing in both
helper methods.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/org/gridsuite/study/server/service/ConsumerService.java`:
- Around line 402-405: The quota release path in ConsumerService’s handler only
checks userId before calling userAdminService.endOperationWithQuota, so it can
pass a null resultUuid and build a malformed quota URL. Update the free-quota
block to require both userId and resultUuid before invoking
endOperationWithQuota, matching the guard already used in the stopped handler,
and keep the call to OperationType.mapFromComputationType(computationType)
unchanged.

---

Nitpick comments:
In `@src/test/java/org/gridsuite/study/server/utils/TestUtils.java`:
- Around line 239-264: The two teardown helpers,
assertWiremockServerRequestsEmptyThenShutdown and
assertWiremockServerRequestsEmptyThenClear, repeat the same
removeServeEventsMatching calls for the quota start/end URLs. Extract that
duplicated logic into a small private helper such as
removeQuotaServeEvents(WireMockServer) and call it from both methods so the
quota-event cleanup is centralized and easier to maintain.
- Around line 112-140: The quota-request filtering in
TestUtils.assertRequestMatches and getRequestsDone is too broad and can
accidentally skip legitimate GET /quota/max calls. Replace the generic
"/v1/users/.*/quota/.*" match with the precise quota start/end patterns already
available in this file via UserAdminServerStubs.QUOTA_START_URL_PATTERN and
UserAdminServerStubs.QUOTA_END_URL_PATTERN, and apply the same narrowing in both
helper methods.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bd8f74b1-33af-4c41-a8f8-f55735d4b2f6

📥 Commits

Reviewing files that changed from the base of the PR and between a76a79c and 2e6c5ac.

📒 Files selected for processing (24)
  • src/main/java/org/gridsuite/study/server/controller/StudyController.java
  • src/main/java/org/gridsuite/study/server/dto/OperationType.java
  • src/main/java/org/gridsuite/study/server/dto/UserProfileInfos.java
  • src/main/java/org/gridsuite/study/server/error/StudyBusinessErrorCode.java
  • src/main/java/org/gridsuite/study/server/service/ConsumerService.java
  • src/main/java/org/gridsuite/study/server/service/StudyService.java
  • src/main/java/org/gridsuite/study/server/service/UserAdminService.java
  • src/main/resources/config/application.yaml
  • src/test/java/org/gridsuite/study/server/NetworkModificationTest.java
  • src/test/java/org/gridsuite/study/server/NodeSequenceTest.java
  • src/test/java/org/gridsuite/study/server/ShortCircuitTest.java
  • src/test/java/org/gridsuite/study/server/StateEstimationTest.java
  • src/test/java/org/gridsuite/study/server/StudyControllerDynamicMarginCalculationTest.java
  • src/test/java/org/gridsuite/study/server/StudyControllerDynamicSecurityAnalysisTest.java
  • src/test/java/org/gridsuite/study/server/StudyControllerDynamicSimulationTest.java
  • src/test/java/org/gridsuite/study/server/StudyServiceTest.java
  • src/test/java/org/gridsuite/study/server/VoltageInitTest.java
  • src/test/java/org/gridsuite/study/server/dto/OperationTypeTest.java
  • src/test/java/org/gridsuite/study/server/rootnetworks/ModificationToExcludeTest.java
  • src/test/java/org/gridsuite/study/server/service/StudyServiceDynamicSimulationTest.java
  • src/test/java/org/gridsuite/study/server/service/UserAdminServiceTest.java
  • src/test/java/org/gridsuite/study/server/utils/TestUtils.java
  • src/test/java/org/gridsuite/study/server/utils/wiremock/UserAdminServerStubs.java
  • src/test/resources/application-default.yml

@GhilesA GhilesA changed the title WIP: feat: add computation quotas check feat: add computation quotas check Jul 7, 2026
@GhilesA
GhilesA requested review from EtienneLt and etiennehomer July 7, 2026 14:15
@GhilesA
GhilesA force-pushed the feat/add_quotas_on_computation branch 2 times, most recently from e8a8de9 to 4de5dc8 Compare July 7, 2026 14:51
feat: fix tests
feat: add test
@GhilesA
GhilesA force-pushed the feat/add_quotas_on_computation branch from 4de5dc8 to b4accf1 Compare July 8, 2026 09:00

@etiennehomer etiennehomer 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.

Not enough time to test and to review UT
But i'm ok with the global design

Comment thread src/test/java/org/gridsuite/study/server/dto/OperationTypeTest.java Outdated
Comment thread src/main/java/org/gridsuite/study/server/service/ConsumerService.java Outdated
Comment thread src/main/java/org/gridsuite/study/server/service/StudyService.java Outdated
Comment thread src/main/java/org/gridsuite/study/server/service/StudyService.java Outdated
Comment thread src/main/java/org/gridsuite/study/server/service/StudyService.java
Comment thread src/main/java/org/gridsuite/study/server/dto/OperationType.java Outdated
Comment thread src/main/java/org/gridsuite/study/server/dto/OperationType.java Outdated
@GhilesA
GhilesA force-pushed the feat/add_quotas_on_computation branch from 5997533 to 8ba7287 Compare July 15, 2026 11:56

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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)
src/main/java/org/gridsuite/study/server/service/ConsumerService.java (1)

514-524: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Extract userId once to avoid redundancy.

The userId is currently being extracted from the headers in two separate places. You can declare it once before the condition to improve readability and avoid the redundant lookup.

♻️ Proposed refactor
-                if (computationType == LOAD_FLOW) {
-                    String userId = (String) msg.getHeaders().get(HEADER_USER_ID);
-                    handleLoadFlowSuccess(studyUuid, receiverObj.getNodeUuid(), receiverObj.getRootNetworkUuid(), resultUuid, userId);
-                }
-
-                // free quota
-                String userId = msg.getHeaders().get(HEADER_USER_ID, String.class);
+                String userId = msg.getHeaders().get(HEADER_USER_ID, String.class);
+
+                if (computationType == LOAD_FLOW) {
+                    handleLoadFlowSuccess(studyUuid, receiverObj.getNodeUuid(), receiverObj.getRootNetworkUuid(), resultUuid, userId);
+                }
+
+                // free quota
                 if (userId != null) {
                     userAdminService.endOperationWithQuota(userId, QuotaType.mapFromComputationType(computationType), resultUuid);
                 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/gridsuite/study/server/service/ConsumerService.java` around
lines 514 - 524, In the surrounding computation-success flow, extract the typed
userId from msg headers once before the LOAD_FLOW condition, then reuse it for
handleLoadFlowSuccess and the quota cleanup block. Remove the duplicate
declaration while preserving the existing null check and behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/org/gridsuite/study/server/dto/QuotaType.java`:
- Line 22: Rename the QuotaType enum constant BALANCE_ADJUSTEMENT to
BALANCE_ADJUSTMENT and update all references to use the corrected spelling
consistently.

---

Nitpick comments:
In `@src/main/java/org/gridsuite/study/server/service/ConsumerService.java`:
- Around line 514-524: In the surrounding computation-success flow, extract the
typed userId from msg headers once before the LOAD_FLOW condition, then reuse it
for handleLoadFlowSuccess and the quota cleanup block. Remove the duplicate
declaration while preserving the existing null check and behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 95646310-6cd4-4a1f-b82d-90f49d303d9e

📥 Commits

Reviewing files that changed from the base of the PR and between b4accf1 and 8ba7287.

📒 Files selected for processing (10)
  • src/main/java/org/gridsuite/study/server/dto/QuotaType.java
  • src/main/java/org/gridsuite/study/server/dto/UserProfileInfos.java
  • src/main/java/org/gridsuite/study/server/service/ConsumerService.java
  • src/main/java/org/gridsuite/study/server/service/StudyService.java
  • src/main/java/org/gridsuite/study/server/service/UserAdminService.java
  • src/test/java/org/gridsuite/study/server/NodeSequenceTest.java
  • src/test/java/org/gridsuite/study/server/StudyServiceTest.java
  • src/test/java/org/gridsuite/study/server/dto/QuotaTypeTest.java
  • src/test/java/org/gridsuite/study/server/rootnetworks/ModificationToExcludeTest.java
  • src/test/java/org/gridsuite/study/server/service/UserAdminServiceTest.java
🚧 Files skipped from review as they are similar to previous changes (6)
  • src/test/java/org/gridsuite/study/server/rootnetworks/ModificationToExcludeTest.java
  • src/test/java/org/gridsuite/study/server/service/UserAdminServiceTest.java
  • src/main/java/org/gridsuite/study/server/dto/UserProfileInfos.java
  • src/main/java/org/gridsuite/study/server/service/UserAdminService.java
  • src/test/java/org/gridsuite/study/server/StudyServiceTest.java
  • src/main/java/org/gridsuite/study/server/service/StudyService.java

Comment thread src/main/java/org/gridsuite/study/server/dto/QuotaType.java Outdated
@GhilesA
GhilesA force-pushed the feat/add_quotas_on_computation branch from 8ba7287 to 96c6a8b Compare July 15, 2026 12:52
@sonarqubecloud

Copy link
Copy Markdown

@GhilesA
GhilesA requested a review from etiennehomer July 17, 2026 08:38
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