cut off power pagination missing distinct#254
Conversation
📝 WalkthroughWalkthroughSecurityAnalysisResultService refines the cut-off power contingencies query by adding ChangesCut-off power contingencies query improvements
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ 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. Comment |
|
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisResultService.java
| 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()); |
There was a problem hiding this comment.
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.
| 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.



PR Summary
cut off power pagination missing distinct