Skip to content

cut off power pagination missing distinct#254

Open
ghazwarhili wants to merge 1 commit into
mainfrom
nmk-cut-off-power-paged-duplicate-key
Open

cut off power pagination missing distinct#254
ghazwarhili wants to merge 1 commit into
mainfrom
nmk-cut-off-power-paged-duplicate-key

Conversation

@ghazwarhili

Copy link
Copy Markdown
Contributor

PR Summary

cut off power pagination missing distinct

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

SecurityAnalysisResultService refines the cut-off power contingencies query by adding distinct() to eliminate duplicate rows, switching to emptyPage(pageable) for empty results, and simplifying contingency ordering from a precomputed position map to direct indexOf lookup. Unused imports are removed.

Changes

Cut-off power contingencies query improvements

Layer / File(s) Summary
Query distinctness, empty handling, and ordering
src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java
A distinct() constraint is added to the cut-off power contingencies specification before filters to prevent duplicate rows. Empty-result handling now delegates to emptyPage(pageable) instead of Spring Data's Page.empty(pageable). Contingency ordering is simplified from building a positionByUuid map with IntStream/Collectors to sorting via orderedUuids.indexOf(...). Corresponding unused imports are removed.

Suggested reviewers

  • etiennehomer
  • AbdelHedhili
🚥 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 'cut off power pagination missing distinct' directly relates to the main change: adding DISTINCT to the cut-off power contingencies query to prevent duplicate rows in pagination results.
Description check ✅ Passed The description 'cut off power pagination missing distinct' accurately reflects the core change in the pull request addressing duplicate rows in cut-off power pagination by adding distinctness.
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.

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


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.

@sonarqubecloud

Copy link
Copy Markdown

@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

🤖 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/securityanalysis/server/service/SecurityAnalysisResultService.java`:
- Around line 469-472: The current sort in SecurityAnalysisResultService (using
orderedUuids.indexOf(c.getUuid()) inside the Comparator) is O(n^2); replace it
by precomputing a Map<UUID,Integer> position map from orderedUuids to their
index, then sort contingencies with Comparator.comparing(c ->
positionMap.get(c.getUuid())) so lookups become O(1); ensure the map is built
before calling contingencyRepository.findAllByUuidIn or immediately after
constructing orderedUuids and used in the sort that currently references
orderedUuids.indexOf, leaving the PageImpl construction unchanged.
🪄 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: d7b02628-8f63-4502-82d4-990b19bf64c0

📥 Commits

Reviewing files that changed from the base of the PR and between da86ddc and aa4366a.

📒 Files selected for processing (1)
  • src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java

Comment on lines 469 to 472
List<UUID> orderedUuids = uuidPage.map(ContingencyRepository.EntityUuid::getUuid).toList();
List<ContingencyEntity> contingencies = contingencyRepository.findAllByUuidIn(orderedUuids);
Map<UUID, Integer> positionByUuid = IntStream.range(0, orderedUuids.size()).boxed().collect(Collectors.toMap(orderedUuids::get, Function.identity()));
contingencies.sort(Comparator.comparingInt(c -> positionByUuid.get(c.getUuid())));
contingencies.sort(Comparator.comparing(c -> orderedUuids.indexOf(c.getUuid())));
return new PageImpl<>(contingencies, pageable, uuidPage.getTotalElements());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Replace indexOf-based sort with precomputed position map.

orderedUuids.indexOf(...) inside the comparator makes ordering quadratic for larger pages/unpaged exports. This is on a client-visible path (Pageable.unpaged(sort) is used for cut-off power CSV), so large result sets can degrade sharply.

Proposed fix
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
...
         List<UUID> orderedUuids = uuidPage.map(ContingencyRepository.EntityUuid::getUuid).toList();
         List<ContingencyEntity> contingencies = contingencyRepository.findAllByUuidIn(orderedUuids);
-        contingencies.sort(Comparator.comparing(c -> orderedUuids.indexOf(c.getUuid())));
+        Map<UUID, Integer> positions = IntStream.range(0, orderedUuids.size())
+                .boxed()
+                .collect(Collectors.toMap(orderedUuids::get, i -> i));
+        contingencies.sort(Comparator.comparingInt(c -> positions.getOrDefault(c.getUuid(), Integer.MAX_VALUE)));
         return new PageImpl<>(contingencies, pageable, uuidPage.getTotalElements());
📝 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
List<UUID> orderedUuids = uuidPage.map(ContingencyRepository.EntityUuid::getUuid).toList();
List<ContingencyEntity> contingencies = contingencyRepository.findAllByUuidIn(orderedUuids);
Map<UUID, Integer> positionByUuid = IntStream.range(0, orderedUuids.size()).boxed().collect(Collectors.toMap(orderedUuids::get, Function.identity()));
contingencies.sort(Comparator.comparingInt(c -> positionByUuid.get(c.getUuid())));
contingencies.sort(Comparator.comparing(c -> orderedUuids.indexOf(c.getUuid())));
return new PageImpl<>(contingencies, pageable, uuidPage.getTotalElements());
List<UUID> orderedUuids = uuidPage.map(ContingencyRepository.EntityUuid::getUuid).toList();
List<ContingencyEntity> contingencies = contingencyRepository.findAllByUuidIn(orderedUuids);
Map<UUID, Integer> positions = IntStream.range(0, orderedUuids.size())
.boxed()
.collect(Collectors.toMap(orderedUuids::get, i -> i));
contingencies.sort(Comparator.comparingInt(c -> positions.getOrDefault(c.getUuid(), Integer.MAX_VALUE)));
return new PageImpl<>(contingencies, pageable, uuidPage.getTotalElements());
🤖 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/securityanalysis/server/service/SecurityAnalysisResultService.java`
around lines 469 - 472, The current sort in SecurityAnalysisResultService (using
orderedUuids.indexOf(c.getUuid()) inside the Comparator) is O(n^2); replace it
by precomputing a Map<UUID,Integer> position map from orderedUuids to their
index, then sort contingencies with Comparator.comparing(c ->
positionMap.get(c.getUuid())) so lookups become O(1); ensure the map is built
before calling contingencyRepository.findAllByUuidIn or immediately after
constructing orderedUuids and used in the sort that currently references
orderedUuids.indexOf, leaving the PageImpl construction unchanged.

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