Skip to content

Refactor contingency count to include detailed information per contingency list#170

Open
FranckLecuyer wants to merge 4 commits into
mainfrom
enrich_contingency_count_information
Open

Refactor contingency count to include detailed information per contingency list#170
FranckLecuyer wants to merge 4 commits into
mainfrom
enrich_contingency_count_information

Conversation

@FranckLecuyer

Copy link
Copy Markdown
Contributor

PR Summary

…gency list.

Signed-off-by: Franck LECUYER <franck.lecuyer@rte-france.com>
@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 585f1c3a-bdd7-4792-951c-acd76dceb819

📥 Commits

Reviewing files that changed from the base of the PR and between 43fbe8a and 9a56e45.

📒 Files selected for processing (3)
  • src/main/java/org/gridsuite/actions/server/ContingencyListService.java
  • src/main/java/org/gridsuite/actions/server/dto/ContingencyCountByContingencyList.java
  • src/test/java/org/gridsuite/actions/server/ContingencyListControllerTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/java/org/gridsuite/actions/server/ContingencyListService.java

📝 Walkthrough

Walkthrough

ContingencyCount now returns per-contingency-list counts keyed by UUID. A new ContingencyCountByContingencyList record adds an error message field. ContingencyListService builds the map response, and controller tests were updated for grouped results and error handling.

Changes

Per-list contingency count

Layer / File(s) Summary
New DTO and updated ContingencyCount contract
src/main/java/org/gridsuite/actions/server/dto/ContingencyCountByContingencyList.java, src/main/java/org/gridsuite/actions/server/dto/ContingencyCount.java
Adds ContingencyCountByContingencyList with nbContingencies, notFoundElements, and invalidContingencyErrorMessage. Replaces ContingencyCount's two int fields with countByContingencyList.
Service map-based count
src/main/java/org/gridsuite/actions/server/ContingencyListService.java
getContingencyCount now builds a per-UUID map of contingency counts and missing elements, and stores error messages when contingency list evaluation fails.
Updated controller tests
src/test/java/org/gridsuite/actions/server/ContingencyListControllerTest.java
Controller tests now assert grouped counts per contingency list, handle unknown IDs with an empty map, and cover invalid contingency element errors.

Suggested reviewers: thangqp

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

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.
Description check ❓ Inconclusive The description is just a template placeholder and doesn't meaningfully describe the changes. Replace the template text with a short summary of what changed, why, and any notable review notes.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed It clearly summarizes the main change: contingency counts now include per-list detailed information.
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.

@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: 2

🤖 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/actions/server/ContingencyListService.java`:
- Around line 141-147: Reject duplicate request IDs before building the
contingenciesCountByContingencyList map in ContingencyListService, because the
current put(uuid, ...) path silently overwrites repeated ids and changes the
behavior of getContingencyCount(...) compared with
getContingencyCountByGroup(...). Update the logic around the ids loop to detect
duplicates up front and fail fast if repeats are not supported; if repeats must
be allowed, change the counting flow so each occurrence is preserved instead of
using a UUID-keyed map that collapses duplicates.

In `@src/main/java/org/gridsuite/actions/server/dto/ContingencyCount.java`:
- Around line 15-16: The `/contingency-lists/count` response shape in
ContingencyCount was changed incompatibly, so keep the legacy scalar total
fields alongside countByContingencyList or version the endpoint before removing
them. Update ContingencyListController and its OpenAPI/response description to
match the chosen contract in the same change, using ContingencyCount and
ContingencyListController as the key symbols to locate the affected code.
🪄 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: e8899c4a-f201-4f8f-a945-8d613611fee9

📥 Commits

Reviewing files that changed from the base of the PR and between 2fc5059 and 43fbe8a.

📒 Files selected for processing (4)
  • src/main/java/org/gridsuite/actions/server/ContingencyListService.java
  • src/main/java/org/gridsuite/actions/server/dto/ContingencyCount.java
  • src/main/java/org/gridsuite/actions/server/dto/ContingencyCountByContingencyList.java
  • src/test/java/org/gridsuite/actions/server/ContingencyListControllerTest.java

Comment on lines +141 to +147
Map<UUID, ContingencyCountByContingencyList> contingenciesCountByContingencyList = new HashMap<>();
for (UUID uuid : ids) {
Optional<PersistentContingencyList> contingencyList = getAnyContingencyList(uuid, network);
if (contingencyList.isPresent()) {
PersistentContingencyList l = contingencyList.get();
nbContingencies += getContingencies(l, network).size();
nbNotFoundElements += l.getNotFoundElements(network).size();
}
contingencyList.ifPresent(l -> contingenciesCountByContingencyList.put(uuid,
new ContingencyCountByContingencyList(getContingencies(l, network).size(), l.getNotFoundElements(network))));
}
return new ContingencyCount(nbContingencies, nbNotFoundElements);
return new ContingencyCount(contingenciesCountByContingencyList);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Reject duplicate ids before populating this map.

put(uuid, ...) silently overwrites repeated request IDs, so ?ids=A&ids=A no longer behaves like the previous implementation and no longer matches getContingencyCountByGroup(...), which still counts every occurrence. If duplicates are unsupported, fail fast here; otherwise the response model needs to preserve multiplicity.

Possible fix
 private ContingencyCount getContingencyCount(Network network, List<UUID> ids) {
-    Map<UUID, ContingencyCountByContingencyList> contingenciesCountByContingencyList = new HashMap<>();
-    for (UUID uuid : ids) {
+    Set<UUID> uniqueIds = new LinkedHashSet<>(ids);
+    if (uniqueIds.size() != ids.size()) {
+        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Duplicate contingency list ids are not supported");
+    }
+    Map<UUID, ContingencyCountByContingencyList> contingenciesCountByContingencyList = new LinkedHashMap<>();
+    for (UUID uuid : uniqueIds) {
         Optional<PersistentContingencyList> contingencyList = getAnyContingencyList(uuid, network);
         contingencyList.ifPresent(l -> contingenciesCountByContingencyList.put(uuid,
                 new ContingencyCountByContingencyList(getContingencies(l, network).size(), l.getNotFoundElements(network))));
     }
     return new ContingencyCount(contingenciesCountByContingencyList);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Map<UUID, ContingencyCountByContingencyList> contingenciesCountByContingencyList = new HashMap<>();
for (UUID uuid : ids) {
Optional<PersistentContingencyList> contingencyList = getAnyContingencyList(uuid, network);
if (contingencyList.isPresent()) {
PersistentContingencyList l = contingencyList.get();
nbContingencies += getContingencies(l, network).size();
nbNotFoundElements += l.getNotFoundElements(network).size();
}
contingencyList.ifPresent(l -> contingenciesCountByContingencyList.put(uuid,
new ContingencyCountByContingencyList(getContingencies(l, network).size(), l.getNotFoundElements(network))));
}
return new ContingencyCount(nbContingencies, nbNotFoundElements);
return new ContingencyCount(contingenciesCountByContingencyList);
Set<UUID> uniqueIds = new LinkedHashSet<>(ids);
if (uniqueIds.size() != ids.size()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Duplicate contingency list ids are not supported");
}
Map<UUID, ContingencyCountByContingencyList> contingenciesCountByContingencyList = new LinkedHashMap<>();
for (UUID uuid : uniqueIds) {
Optional<PersistentContingencyList> contingencyList = getAnyContingencyList(uuid, network);
contingencyList.ifPresent(l -> contingenciesCountByContingencyList.put(uuid,
new ContingencyCountByContingencyList(getContingencies(l, network).size(), l.getNotFoundElements(network))));
}
return new ContingencyCount(contingenciesCountByContingencyList);
🤖 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/actions/server/ContingencyListService.java`
around lines 141 - 147, Reject duplicate request IDs before building the
contingenciesCountByContingencyList map in ContingencyListService, because the
current put(uuid, ...) path silently overwrites repeated ids and changes the
behavior of getContingencyCount(...) compared with
getContingencyCountByGroup(...). Update the logic around the ids loop to detect
duplicates up front and fail fast if repeats are not supported; if repeats must
be allowed, change the counting flow so each occurrence is preserved instead of
using a UUID-keyed map that collapses duplicates.

Comment on lines 15 to +16
public record ContingencyCount(
int contingencies,
int notFoundElements
Map<UUID, ContingencyCountByContingencyList> countByContingencyList

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

This changes the public /contingency-lists/count schema without a compatible rollout.

src/main/java/org/gridsuite/actions/server/ContingencyListController.java:56-66 still describes this endpoint as returning a global total, but this record drops the legacy scalar fields entirely. Existing clients will break as soon as they deserialize the new payload. Please either version the endpoint or keep the old fields alongside countByContingencyList, and update the controller/OpenAPI wording in the same change.

🤖 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/actions/server/dto/ContingencyCount.java` around
lines 15 - 16, The `/contingency-lists/count` response shape in ContingencyCount
was changed incompatibly, so keep the legacy scalar total fields alongside
countByContingencyList or version the endpoint before removing them. Update
ContingencyListController and its OpenAPI/response description to match the
chosen contract in the same change, using ContingencyCount and
ContingencyListController as the key symbols to locate the affected code.

… the corresponding error message in the dto

Signed-off-by: Franck LECUYER <franck.lecuyer@rte-france.com>
Signed-off-by: Franck LECUYER <franck.lecuyer@rte-france.com>
@sonarqubecloud

sonarqubecloud Bot commented Jul 7, 2026

Copy link
Copy Markdown

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