From 854bd613a2e098ed24d7f614e940da5e41430342 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Wed, 28 May 2025 12:00:49 +0200 Subject: [PATCH 1/6] add a test with a vl without substation --- .../geodata/server/GeoDataServiceTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java index df61aa8d..e767ab50 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java @@ -245,6 +245,7 @@ void testNonExisting() { .setG2(0.0) .setB2(386E-6 / 2) .add(); + // if not geo-data is associated to a substation, this substation shold not be mentionned in the returned value List substationsGeoData = geoDataService.getSubstationsByCountries(network, new HashSet<>(Collections.singletonList(Country.FR))); assertFalse(substationsGeoData.stream().anyMatch(Objects::isNull), "Must not contain nulls"); assertFalse(substationsGeoData.stream().anyMatch(s -> notexistsub1.getId().equals(s.getId())), @@ -258,6 +259,35 @@ void testNonExisting() { "Must not contain unknown lines " + notexistline.getId()); } + @Test + void outOfSubstationVoltageLevelShouldNotHinderLineDataGathering() { + Network network = EurostagTutorialExample1Factory.create(); + List existingVls = network.getVoltageLevelStream().toList(); + List existingLines = network.getLineStream().toList(); + VoltageLevel p1 = network.newVoltageLevel() + .setId("PIQUAGE1").setNominalV(380).setTopologyKind(TopologyKind.BUS_BREAKER) + .add(); + Bus bus1 = p1.getBusBreakerView().newBus() + .setId("NPIQ1") + .add(); + Line halfPiq = network.newLine() + .setId("LINE_PIQ_AT_A_SIDE") + .setVoltageLevel1(existingVls.get(0).getId()) + .setBus1(existingVls.get(0).getBusBreakerView().getBuses().iterator().next().getId()) + .setConnectableBus1(existingVls.get(0).getBusBreakerView().getBuses().iterator().next().getId()) + .setVoltageLevel2(p1.getId()).setBus2(bus1.getId()).setConnectableBus2(bus1.getId()) + .setR(3.0) + .setX(33.0) + .setG1(0.0) + .setB1(386E-6 / 2) + .setG2(0.0) + .setB2(386E-6 / 2) + .add(); + + List linesGeoData = geoDataService.getLinesByCountries(network, new HashSet<>(Collections.singletonList(Country.FR))); + assertEquals(2, linesGeoData.size()); + } + @Test void testSimilarNeighborhoodOffset() { Network network = EurostagTutorialExample1Factory.create(); From a0096b7afe4c0eee8c25a38e53c48f05a0dec73a Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Wed, 28 May 2025 13:04:32 +0200 Subject: [PATCH 2/6] fix simple out of substation voltage level should not hinder geo data search --- .../geodata/server/GeoDataService.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index b7ca4460..8d2e457c 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -385,12 +385,11 @@ private static Map> getNeighbours(List substatio for (Substation s : substations) { for (VoltageLevel vl : s.getVoltageLevels()) { for (Line line : vl.getConnectables(Line.class)) { - Substation s1 = line.getTerminal1().getVoltageLevel().getSubstation().orElseThrow(); // TODO - Substation s2 = line.getTerminal2().getVoltageLevel().getSubstation().orElseThrow(); // TODO - if (s1 != s) { - neighbours.get(s.getId()).add(s1.getId()); - } else if (s2 != s) { - neighbours.get(s.getId()).add(s2.getId()); + Substation s1 = line.getTerminal1().getVoltageLevel().getSubstation().orElse(null); + Substation s2 = line.getTerminal2().getVoltageLevel().getSubstation().orElse(null); + Substation otherSide = s1 == s ? s2 : s1; + if (otherSide != null) { + neighbours.get(s.getId()).add(otherSide.getId()); } } } @@ -445,7 +444,8 @@ boolean emptyOrEquals(String emptyable, String s) { *

* returns null when the substations at the end of the line are missing. */ - private LineGeoData getLineGeoDataWithEndSubstations(Map linesGeoDataDb, Map substationGeoDataDb, String lineId, Substation substation1, Substation substation2) { + private LineGeoData getLineGeoDataWithEndSubstations(Map linesGeoDataDb, + Map substationGeoDataDb, String lineId, Substation substation1, Substation substation2) { LineGeoData geoData = linesGeoDataDb.get(lineId); SubstationGeoData substation1GeoData = substationGeoDataDb.get(substation1.getId()); SubstationGeoData substation2GeoData = substationGeoDataDb.get(substation2.getId()); @@ -515,17 +515,25 @@ List getLinesByCountries(Network network, Set countries) { // we also want the destination substation (so we add the neighbouring country) Set countryAndNextTo = mapSubstationsByLine.entrySet().stream().flatMap(entry -> - Stream.of(entry.getValue().getLeft(), entry.getValue().getRight()).map(Substation::getNullableCountry).filter(Objects::nonNull)).collect(Collectors.toSet()); + Stream.of(entry.getValue().getLeft(), entry.getValue().getRight()) + .filter(Objects::nonNull) + .map(Substation::getNullableCountry) + .filter(Objects::nonNull)).collect(Collectors.toSet()); Map substationGeoDataDb = getSubstationMapByCountries(network, countryAndNextTo); List geoData = new ArrayList<>(); - mapSubstationsByLine.forEach((key, value) -> { - LineGeoData geo = getLineGeoDataWithEndSubstations(linesGeoDataDb, substationGeoDataDb, key, value.getLeft(), value.getRight()); - if (geo != null) { - geoData.add(geo); - } - }); + mapSubstationsByLine.entrySet().stream() + .filter(entry -> entry.getValue() != null) + .filter(entry -> entry.getValue().getLeft() != null) + .filter(entry -> entry.getValue().getRight() != null) + .forEach(entry -> { + LineGeoData geo = getLineGeoDataWithEndSubstations(linesGeoDataDb, substationGeoDataDb, entry.getKey(), + entry.getValue().getLeft(), entry.getValue().getRight()); + if (geo != null) { + geoData.add(geo); + } + }); LOGGER.info("{} lines read from DB in {} ms", linesGeoDataDb.size(), stopWatch.getTime(TimeUnit.MILLISECONDS)); @@ -534,8 +542,8 @@ List getLinesByCountries(Network network, Set countries) { private Pair getSubstations(Identifiable identifiable) { return switch (identifiable.getType()) { - case LINE -> Pair.of(((Line) identifiable).getTerminal1().getVoltageLevel().getSubstation().orElseThrow(), - ((Line) identifiable).getTerminal2().getVoltageLevel().getSubstation().orElseThrow()); + case LINE -> Pair.of(((Line) identifiable).getTerminal1().getVoltageLevel().getSubstation().orElse(null), + ((Line) identifiable).getTerminal2().getVoltageLevel().getSubstation().orElse(null)); case TIE_LINE -> Pair.of(((TieLine) identifiable).getDanglingLine1().getTerminal().getVoltageLevel().getSubstation().orElseThrow(), ((TieLine) identifiable).getDanglingLine2().getTerminal().getVoltageLevel().getSubstation().orElseThrow()); From b566a083f597930a43fbd436248f1640d1cfae18 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Wed, 28 May 2025 13:28:02 +0200 Subject: [PATCH 3/6] remove new unread variables --- .../java/org/gridsuite/geodata/server/GeoDataServiceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java index e767ab50..22ac8f1d 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java @@ -263,14 +263,13 @@ void testNonExisting() { void outOfSubstationVoltageLevelShouldNotHinderLineDataGathering() { Network network = EurostagTutorialExample1Factory.create(); List existingVls = network.getVoltageLevelStream().toList(); - List existingLines = network.getLineStream().toList(); VoltageLevel p1 = network.newVoltageLevel() .setId("PIQUAGE1").setNominalV(380).setTopologyKind(TopologyKind.BUS_BREAKER) .add(); Bus bus1 = p1.getBusBreakerView().newBus() .setId("NPIQ1") .add(); - Line halfPiq = network.newLine() + network.newLine() .setId("LINE_PIQ_AT_A_SIDE") .setVoltageLevel1(existingVls.get(0).getId()) .setBus1(existingVls.get(0).getBusBreakerView().getBuses().iterator().next().getId()) From 8b63eb6625021bd0cf6cf95de966017d7c07da6d Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Mon, 2 Jun 2025 16:42:11 +0200 Subject: [PATCH 4/6] correct typo in comment --- .../java/org/gridsuite/geodata/server/GeoDataServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java index 22ac8f1d..42f36603 100644 --- a/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java +++ b/src/test/java/org/gridsuite/geodata/server/GeoDataServiceTest.java @@ -245,7 +245,7 @@ void testNonExisting() { .setG2(0.0) .setB2(386E-6 / 2) .add(); - // if not geo-data is associated to a substation, this substation shold not be mentionned in the returned value + // if not geo-data is associated to a substation, this substation should not be mentionned in the returned value List substationsGeoData = geoDataService.getSubstationsByCountries(network, new HashSet<>(Collections.singletonList(Country.FR))); assertFalse(substationsGeoData.stream().anyMatch(Objects::isNull), "Must not contain nulls"); assertFalse(substationsGeoData.stream().anyMatch(s -> notexistsub1.getId().equals(s.getId())), From 8af410ea7474b4081c36b8baee3253bdbd9a594f Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Tue, 3 Jun 2025 18:25:15 +0200 Subject: [PATCH 5/6] correct load strategy consistency --- .../geodata/server/GeoDataController.java | 10 ++++++-- .../geodata/server/GeoDataService.java | 24 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataController.java b/src/main/java/org/gridsuite/geodata/server/GeoDataController.java index 36373ae4..e2a8d09b 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataController.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataController.java @@ -58,7 +58,10 @@ public ResponseEntity> getSubstations(@Parameter(descrip @Parameter(description = "Countries") @RequestParam(name = "country", required = false) List countries, @RequestBody(required = false) List substationIds) { Set countrySet = toCountrySet(countries); - Network network = networkStoreService.getNetwork(networkUuid, substationIds != null ? PreloadingStrategy.NONE : PreloadingStrategy.COLLECTION); + PreloadingStrategy preloadingStrategy = geoDataService.preferPreload(countrySet, substationIds) ? + PreloadingStrategy.COLLECTION : + PreloadingStrategy.NONE; + Network network = networkStoreService.getNetwork(networkUuid, preloadingStrategy); if (variantId != null) { network.getVariantManager().setWorkingVariant(variantId); } @@ -74,7 +77,10 @@ public ResponseEntity> getLines(@Parameter(description = "Netw @Parameter(description = "Countries") @RequestParam(name = "country", required = false) List countries, @RequestBody(required = false) List lineIds) { Set countrySet = toCountrySet(countries); - Network network = networkStoreService.getNetwork(networkUuid, lineIds != null ? PreloadingStrategy.NONE : PreloadingStrategy.COLLECTION); + PreloadingStrategy preloadingStrategy = geoDataService.preferPreload(countrySet, lineIds) ? + PreloadingStrategy.COLLECTION : + PreloadingStrategy.NONE; + Network network = networkStoreService.getNetwork(networkUuid, preloadingStrategy); if (variantId != null) { network.getVariantManager().setWorkingVariant(variantId); } diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index 8d2e457c..542e9e32 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -554,16 +554,28 @@ private Pair getSubstations(Identifiable identifiable }; } + public boolean preferPreload(Set countrySet, List ids) { + if (ids == null || ids.size() > 4) { + return true; + } + + return false; + } + @Transactional(readOnly = true) public List getSubstationsData(Network network, Set countrySet, List substationIds) { CompletableFuture> substationGeoDataFuture = geoDataExecutionService.supplyAsync(() -> { - if (substationIds != null) { + if (substationIds != null && substationIds.size() < 4) { if (!countrySet.isEmpty()) { LOGGER.warn("Countries will not be taken into account to filter substation position."); } return getSubstationsByIds(network, new HashSet<>(substationIds)); } else { - return getSubstationsByCountries(network, countrySet); + List substationsByCountries = getSubstationsByCountries(network, countrySet); + if (substationIds != null && !substationIds.isEmpty()) { + return substationsByCountries.stream().filter(s -> substationIds.contains(s.getId())).toList(); + } + return substationsByCountries; } }); try { @@ -579,13 +591,17 @@ public List getSubstationsData(Network network, Set @Transactional(readOnly = true) public List getLinesData(Network network, Set countrySet, List lineIds) { CompletableFuture> lineGeoDataFuture = geoDataExecutionService.supplyAsync(() -> { - if (lineIds != null) { + if (!preferPreload(countrySet, lineIds)) { if (!countrySet.isEmpty()) { LOGGER.warn("Countries will not be taken into account to filter line position."); } return getLinesByIds(network, new HashSet<>(lineIds)); } else { - return getLinesByCountries(network, countrySet); + List linesByCountries = getLinesByCountries(network, countrySet); + if (lineIds != null && !lineIds.isEmpty()) { + return linesByCountries.stream().filter(ln -> lineIds.contains(ln.getId())).toList(); + } + return linesByCountries; } }); try { From 90575b132fbb6da102fd1619890ae3505fe9a8c3 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Wed, 4 Jun 2025 09:16:28 +0200 Subject: [PATCH 6/6] sonar happier --- .../gridsuite/geodata/server/GeoDataController.java | 4 ++-- .../org/gridsuite/geodata/server/GeoDataService.java | 10 +++------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataController.java b/src/main/java/org/gridsuite/geodata/server/GeoDataController.java index e2a8d09b..2378c28f 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataController.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataController.java @@ -58,7 +58,7 @@ public ResponseEntity> getSubstations(@Parameter(descrip @Parameter(description = "Countries") @RequestParam(name = "country", required = false) List countries, @RequestBody(required = false) List substationIds) { Set countrySet = toCountrySet(countries); - PreloadingStrategy preloadingStrategy = geoDataService.preferPreload(countrySet, substationIds) ? + PreloadingStrategy preloadingStrategy = geoDataService.preferPreload(substationIds) ? PreloadingStrategy.COLLECTION : PreloadingStrategy.NONE; Network network = networkStoreService.getNetwork(networkUuid, preloadingStrategy); @@ -77,7 +77,7 @@ public ResponseEntity> getLines(@Parameter(description = "Netw @Parameter(description = "Countries") @RequestParam(name = "country", required = false) List countries, @RequestBody(required = false) List lineIds) { Set countrySet = toCountrySet(countries); - PreloadingStrategy preloadingStrategy = geoDataService.preferPreload(countrySet, lineIds) ? + PreloadingStrategy preloadingStrategy = geoDataService.preferPreload(lineIds) ? PreloadingStrategy.COLLECTION : PreloadingStrategy.NONE; Network network = networkStoreService.getNetwork(networkUuid, preloadingStrategy); diff --git a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java index 542e9e32..ca24eb19 100644 --- a/src/main/java/org/gridsuite/geodata/server/GeoDataService.java +++ b/src/main/java/org/gridsuite/geodata/server/GeoDataService.java @@ -554,12 +554,8 @@ private Pair getSubstations(Identifiable identifiable }; } - public boolean preferPreload(Set countrySet, List ids) { - if (ids == null || ids.size() > 4) { - return true; - } - - return false; + public boolean preferPreload(List ids) { + return ids == null || ids.size() > 4; } @Transactional(readOnly = true) @@ -591,7 +587,7 @@ public List getSubstationsData(Network network, Set @Transactional(readOnly = true) public List getLinesData(Network network, Set countrySet, List lineIds) { CompletableFuture> lineGeoDataFuture = geoDataExecutionService.supplyAsync(() -> { - if (!preferPreload(countrySet, lineIds)) { + if (!preferPreload(lineIds)) { if (!countrySet.isEmpty()) { LOGGER.warn("Countries will not be taken into account to filter line position."); }