Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collection impact #356

Merged
merged 46 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
54b0b69
feat(): Add collection impact
sBouzols Oct 24, 2023
78094aa
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Oct 24, 2023
ae9d0a7
feat(): Replace collection factor by collection threshold to avoid ge…
sBouzols Oct 26, 2023
ca791c3
docs(): add author infos
sBouzols Nov 10, 2023
59c6748
Merge remote-tracking branch 'origin/main' into feat_add_collection_i…
sBouzols Jan 18, 2024
882c5d5
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Feb 7, 2024
6ac8d31
doc(): comment fix
sBouzols Feb 7, 2024
c223c9c
test(): fix simple TU without spring initialization with @Value
sBouzols Feb 7, 2024
e4602b4
test(): improve testElementImpacts to check collection impacted equip…
sBouzols Feb 7, 2024
85a8ab6
code smell fix
sBouzols Feb 7, 2024
eaf9180
tests(): Add assertResultImpacts for some TUs
sBouzols Feb 9, 2024
e1fa819
docs(): Add comment over a strange test case
sBouzols Feb 9, 2024
2d86915
Feat(): Add collection impact for substations
sBouzols Feb 9, 2024
c092cea
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Feb 13, 2024
b68c10e
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Feb 13, 2024
74cd686
fix() from review
sBouzols Feb 14, 2024
9751dcb
doc(): don't change license year for now
sBouzols Feb 15, 2024
3c2d6c0
refactor(): lombok annotations
sBouzols Feb 15, 2024
9d9870a
refactor(): getImpactedSubstationsIds
sBouzols Feb 15, 2024
d1ced7d
refactor(): remove collection-threshold from powsybl-ws scope
sBouzols Feb 15, 2024
84d2939
refactor(): rename networkSimpleElementImpacts to networkSimpleImpacts
sBouzols Feb 15, 2024
79c6278
refactor(): assertThat containsAll
sBouzols Feb 15, 2024
e91c685
remove breaking line
sBouzols Feb 15, 2024
5bb4253
remove order for assertResultImpacts
sBouzols Feb 15, 2024
2a0ca72
remove order in assert for formula modif
sBouzols Feb 15, 2024
b737b34
fix(): from review
sBouzols Feb 20, 2024
5e80d08
refactor(): for loop instead of Collection.forEach from review
sBouzols Feb 20, 2024
74aee50
Merge remote-tracking branch 'origin/main' into feat_add_collection_i…
sBouzols Feb 20, 2024
9e18860
checkstyle
sBouzols Feb 20, 2024
986ec7d
refactor(): from review
sBouzols Feb 27, 2024
b58f584
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Feb 28, 2024
b3bec94
fix(): fro review
sBouzols Feb 28, 2024
b5e46eb
Merge branch 'feat_add_collection_impact_2' of https://github.com/gri…
sBouzols Feb 28, 2024
64b4803
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Mar 1, 2024
a76e475
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Mar 1, 2024
d2d333f
revert changes over appclication-default
sBouzols Mar 1, 2024
62bf00b
typo
sBouzols Mar 1, 2024
ae536d2
refactor(): rename collectionImpactElementTypes to elementTypes
sBouzols Mar 1, 2024
467d5aa
fix from review
sBouzols Mar 1, 2024
35a2f88
Merge branch 'main' into feat_add_collection_impact_2
sBouzols Mar 4, 2024
9fb737d
fix from review
sBouzols Mar 4, 2024
dd661dd
Add TU for coverage
sBouzols Mar 4, 2024
7b93a6e
fix TU
sBouzols Mar 4, 2024
21c9d52
Fix 'collectionThreshold' not injected
Mar 4, 2024
a9624ab
Merge branch 'feat_add_collection_impact_2' of https://github.com/gri…
Mar 4, 2024
bd21570
Fix assertResultImpacts
Mar 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.Builder;
import lombok.Data;
import org.gridsuite.modification.server.impacts.AbstractBaseImpact;
import org.gridsuite.modification.server.impacts.AbstractBaseImpact.ImpactType;
import org.gridsuite.modification.server.impacts.SimpleElementImpact;

import java.util.List;
Expand Down Expand Up @@ -54,7 +53,7 @@ public ApplicationStatus max(ApplicationStatus other) {

public Set<String> getImpactedSubstationsIds() {
return networkImpacts.stream()
.filter(impact -> impact.getType() == ImpactType.SIMPLE)
.filter(impact -> impact.isSimple())
.flatMap(impact -> ((SimpleElementImpact) impact).getSubstationIds().stream())
.collect(Collectors.toCollection(TreeSet::new)); // using TreeSet to keep natural order
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.gridsuite.modification.server.impacts;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.powsybl.iidm.network.IdentifiableType;
Expand Down Expand Up @@ -45,4 +46,10 @@ public enum ImpactType {
private ImpactType type;

private IdentifiableType elementType;

@JsonIgnore
public abstract boolean isSimple();

@JsonIgnore
public abstract boolean isCollection();
ne0ds marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public class CollectionElementImpact extends AbstractBaseImpact {
public ImpactType getType() {
return ImpactType.COLLECTION;
}

public boolean isSimple() {
return false;
}

public boolean isCollection() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* This class describes an element type network impact
* This type of network impact only describes an individual impacted item and the list of associated substations
Expand Down Expand Up @@ -43,4 +45,27 @@ public enum SimpleImpactType {
public ImpactType getType() {
return ImpactType.SIMPLE;
}

public boolean isSimple() {
return true;
}

public boolean isCollection() {
return false;
}

@JsonIgnore
public boolean isCreation() {
return getSimpleImpactType() == SimpleImpactType.CREATION;
}

@JsonIgnore
public boolean isModification() {
return getSimpleImpactType() == SimpleImpactType.MODIFICATION;
}

@JsonIgnore
public boolean isDeletion() {
return getSimpleImpactType() == SimpleImpactType.DELETION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ public class NetworkModificationApplicator {

@Getter private final FilterService filterService;

@Value("${impacts.collection-threshold:50}")
private Integer collectionThreshold;

public NetworkModificationApplicator(NetworkStoreService networkStoreService, EquipmentInfosService equipmentInfosService,
ReportService reportService, FilterService filterService, @Value("${impacts.collection-threshold:50}") Integer collectionThreshold) {
ReportService reportService, FilterService filterService, /*TODO REMOVE*/ Integer collectionThreshold) {
this.networkStoreService = networkStoreService;
this.equipmentInfosService = equipmentInfosService;
this.reportService = reportService;
this.filterService = filterService;
// TODO REMOVE from constructor when VoltageInitReportTest becomes a @SpringBootTest
this.collectionThreshold = collectionThreshold;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class NetworkStoreListener implements NetworkListener {

private final Set<SimpleElementImpact> simpleImpacts = new LinkedHashSet<>();

private Integer collectionThreshold;
private final Integer collectionThreshold;

protected NetworkStoreListener(Network network, UUID networkUuid,
NetworkStoreService networkStoreService, EquipmentInfosService equipmentInfosService, Integer collectionThreshold) {
Expand Down Expand Up @@ -274,51 +274,53 @@ private List<AbstractBaseImpact> flushNetworkImpacts() {
.build());
}

return new ArrayList<>(reduceNetworkImpacts());
return reduceNetworkImpacts();
}

private boolean isAllNetworkImpacted() {
Set<String> allImpactedSubstationsIds = simpleImpacts.stream().flatMap(i -> i.getSubstationIds().stream()).collect(Collectors.toSet());
return allImpactedSubstationsIds.size() >= collectionThreshold;
}

private Set<AbstractBaseImpact> reduceNetworkImpacts() {
Set<AbstractBaseImpact> reducedImpacts = new HashSet<>();
private List<AbstractBaseImpact> reduceNetworkImpacts() {
List<AbstractBaseImpact> reducedImpacts = new ArrayList<>();
Set<String> impactedSubstationsIds = new HashSet<>();
sBouzols marked this conversation as resolved.
Show resolved Hide resolved

// Impacts type collection
Arrays.stream(IdentifiableType.values()).forEach(elementType -> {
Set<SimpleElementImpact> impacts = getSimpleImpacts(elementType);
for (IdentifiableType elementType : IdentifiableType.values()) {
List<SimpleElementImpact> impacts = getSimpleImpacts(elementType);
if (impacts.size() >= collectionThreshold) {
reducedImpacts.add(CollectionElementImpact.builder()
.elementType(elementType)
.build());
} else {
impactedSubstationsIds.addAll(impacts.stream().flatMap(i -> i.getSubstationIds().stream()).collect(Collectors.toSet()));
impactedSubstationsIds.addAll(impacts.stream().flatMap(i -> i.getSubstationIds().stream()).toList());
}
});
}

// Impacts type simple for substation only
reducedImpacts.addAll(impactedSubstationsIds.stream().map(id ->
SimpleElementImpact.builder()
.simpleImpactType(SimpleImpactType.MODIFICATION)
.elementType(IdentifiableType.SUBSTATION)
.elementId(id)
.substationIds(Set.of(id))
.build()
).collect(Collectors.toSet()));
reducedImpacts.addAll(
impactedSubstationsIds.stream().map(id ->
SimpleElementImpact.builder()
.simpleImpactType(SimpleImpactType.MODIFICATION)
.elementType(IdentifiableType.SUBSTATION)
.elementId(id)
.substationIds(Set.of(id))
.build()
).toList()
);

// Impacts type simple for deletion only
reducedImpacts.addAll(simpleImpacts.stream().filter(i -> i.getSimpleImpactType() == SimpleImpactType.DELETION).collect(Collectors.toSet()));
reducedImpacts.addAll(simpleImpacts.stream().filter(i -> i.isDeletion()).distinct().toList());

return reducedImpacts;
}

private Set<SimpleElementImpact> getSimpleImpacts(IdentifiableType elementType) {
private List<SimpleElementImpact> getSimpleImpacts(IdentifiableType elementType) {
return simpleImpacts.stream()
.filter(i -> i.getSimpleImpactType() != SimpleImpactType.DELETION)
.filter(i -> i.getElementType() == elementType)
.collect(Collectors.toSet());
.filter(i -> !i.isDeletion() && i.getElementType() == elementType)
.distinct()
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -68,7 +67,7 @@ public void testElementImpact() throws IOException {

assertEquals("[s1, s2, s3, s4]", result.getImpactedSubstationsIds().toString());

HashSet<AbstractBaseImpact> impactsSet = new HashSet<>(List.of(creationImpact, creationImpact, creationImpact));
Set<AbstractBaseImpact> impactsSet = Set.copyOf(List.of(creationImpact, creationImpact, creationImpact));

assertEquals("[{\"type\":\"SIMPLE\",\"elementType\":\"LINE\",\"simpleImpactType\":\"CREATION\",\"elementId\":\"lineId\",\"substationIds\":[\"s1\",\"s2\"]}]", mapper.writeValueAsString(impactsSet));
}
Expand Down Expand Up @@ -96,7 +95,7 @@ public void testCollectionElementImpact() {

assertEquals(Set.of(), result.getImpactedSubstationsIds());

HashSet<AbstractBaseImpact> impactsSet = new HashSet<>(List.of(linesCollectionImpact, linesCollectionImpact, linesCollectionImpact));
Set<AbstractBaseImpact> impactsSet = Set.copyOf(List.of(linesCollectionImpact, linesCollectionImpact, linesCollectionImpact));

assertEquals("[{\"type\":\"COLLECTION\",\"elementType\":\"LINE\"}]", mapper.writeValueAsString(impactsSet));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ private static void testEmptyImpacts(ApplicationStatus globalApplicationStatusEx
assertThat(networkModificationResult).recursivelyEquals(resultExpected);
}

public static void testElementImpacts(ObjectMapper mapper, String resultAsString, int nbImpacts, Set<IdentifiableType> collectionImpactElementTypes, Set<String> substationIds) throws JsonProcessingException {
public static void testElementImpacts(ObjectMapper mapper, String resultAsString, int nbImpacts, Set<IdentifiableType> elementTypes, Set<String> substationIds) throws JsonProcessingException {
Optional<NetworkModificationResult> networkModificationResult = mapper.readValue(resultAsString, new TypeReference<>() { });
assertTrue(networkModificationResult.isPresent());

assertEquals(ApplicationStatus.ALL_OK, networkModificationResult.get().getApplicationStatus());
assertEquals(new TreeSet<>(substationIds), networkModificationResult.get().getImpactedSubstationsIds());
assertEquals(nbImpacts, networkModificationResult.get().getNetworkImpacts().size());
assertThat(networkModificationResult.get().getNetworkImpacts()).containsAll(collectionImpactElementTypes.stream().map(TestImpactUtils::createCollectionElementImpact).toList());
assertThat(networkModificationResult.get().getNetworkImpacts()).containsAll(elementTypes.stream().map(TestImpactUtils::createCollectionElementImpact).toList());
}

public static void testElementImpacts(ObjectMapper mapper, String resultAsString, List<AbstractBaseImpact> elementImpactsExpected) throws JsonProcessingException {
Expand Down Expand Up @@ -94,7 +94,7 @@ public static void testElementImpact(SimpleImpactType type, ObjectMapper mapper,
NetworkModificationResult resultExpected = NetworkModificationResult.builder()
.applicationStatus(ApplicationStatus.ALL_OK)
.lastGroupApplicationStatus(ApplicationStatus.ALL_OK)
.networkImpacts(createSubstationImpacts(new HashSet<>(substationIds)))
.networkImpacts(createSubstationImpacts(substationIds))
.build();
assertThat(networkModificationResult.get()).recursivelyEquals(resultExpected);
}
Expand Down Expand Up @@ -157,20 +157,18 @@ public static void testBranchImpacts(ObjectMapper mapper, SimpleImpactType type,
private static List<AbstractBaseImpact> createBranchImpacts(SimpleImpactType type, IdentifiableType branchType, String branchId,
String breakerId1, String disconnectorId1, String substationId1,
String breakerId2, String disconnectorId2, String substationId2) {
LinkedList<AbstractBaseImpact> impacts = new LinkedList<>();
List<AbstractBaseImpact> impacts = new ArrayList<>();
sBouzols marked this conversation as resolved.
Show resolved Hide resolved
if (type == SimpleImpactType.DELETION) {
List<SimpleElementImpact> switchImpacts = List.of(
sBouzols marked this conversation as resolved.
Show resolved Hide resolved
createElementImpact(SimpleImpactType.DELETION, branchType, branchId, new HashSet<>(List.of(substationId1, substationId2))),
createElementImpact(SimpleImpactType.DELETION, branchType, branchId, Set.copyOf(List.of(substationId1, substationId2))),
createElementImpact(SimpleImpactType.DELETION, IdentifiableType.SWITCH, breakerId1, Set.of(substationId1)),
createElementImpact(SimpleImpactType.DELETION, IdentifiableType.SWITCH, disconnectorId1, Set.of(substationId1)),
createElementImpact(SimpleImpactType.DELETION, IdentifiableType.SWITCH, breakerId2, Set.of(substationId2)),
createElementImpact(SimpleImpactType.DELETION, IdentifiableType.SWITCH, disconnectorId2, Set.of(substationId2))
);
impacts.addAll(0, switchImpacts);
} else {
Set<String> substationIds = new HashSet<>();
substationIds.addAll(List.of(substationId1, substationId2));
impacts.addAll(createSubstationImpacts(substationIds));
impacts.addAll(createSubstationImpacts(Set.copyOf(List.of(substationId1, substationId2))));
sBouzols marked this conversation as resolved.
Show resolved Hide resolved
}
return impacts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.gridsuite.modification.server.elasticsearch.EquipmentInfosService;
import org.gridsuite.modification.server.elasticsearch.TombstonedEquipmentInfosRepository;
import org.gridsuite.modification.server.impacts.AbstractBaseImpact;
import org.gridsuite.modification.server.impacts.CollectionElementImpact;
import org.gridsuite.modification.server.impacts.SimpleElementImpact;
import org.gridsuite.modification.server.modifications.ModificationUtils;
import org.gridsuite.modification.server.repositories.NetworkModificationRepository;
Expand Down Expand Up @@ -1135,11 +1134,6 @@ private void testMultipleDeletionImpacts(String networkModificationResultAsStrin

// Equipment has been added as TombstonedEquipmentInfos in ElasticSearch
assertTrue(existTombstonedEquipmentInfos(simpleImpact.getElementId(), TEST_NETWORK_ID, VariantManagerConstants.INITIAL_VARIANT_ID));
} else if (impact instanceof CollectionElementImpact) {
// Hard to check because
// Not all equipments of this collection impact type have been removed from the network
// Not all equipments of this collection impact type have been added as TombstonedEquipmentInfos in ElasticSearch
// There is no equipmentType in the TombstonedEquipmentInfos entity
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.stream.Collectors;

import static org.gridsuite.modification.server.utils.TestUtils.assertLogMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.gridsuite.modification.server.Impacts.TestImpactUtils.createCollectionElementImpact;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -36,7 +38,7 @@ public abstract class AbstractByFilterDeletionTest extends AbstractNetworkModifi

@Override
protected void assertResultImpacts(List<AbstractBaseImpact> impacts) {
// TODO later
assertThat(impacts).containsExactly(createCollectionElementImpact(getIdentifiableType()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.powsybl.iidm.network.EnergySource;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import lombok.SneakyThrows;
import org.gridsuite.modification.server.ModificationType;
import org.gridsuite.modification.server.dto.GeneratorCreationInfos;
import org.gridsuite.modification.server.dto.ModificationInfos;
import org.gridsuite.modification.server.dto.TabularCreationInfos;
import org.gridsuite.modification.server.impacts.AbstractBaseImpact;
import org.gridsuite.modification.server.modifications.AbstractNetworkModificationTest;
import org.gridsuite.modification.server.utils.NetworkCreation;
import org.junit.Test;
Expand All @@ -29,7 +31,9 @@

import static com.vladmihalcea.sql.SQLStatementCountValidator.assertSelectCount;
import static com.vladmihalcea.sql.SQLStatementCountValidator.reset;
import static org.assertj.core.api.Assertions.assertThat;
import static org.gridsuite.modification.server.utils.TestUtils.assertLogMessage;
import static org.gridsuite.modification.server.Impacts.TestImpactUtils.createCollectionElementImpact;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -156,6 +160,11 @@ protected void assertAfterNetworkModificationDeletion() {
assertNull(getNetwork().getGenerator("id4"));
}

@Override
protected void assertResultImpacts(List<AbstractBaseImpact> impacts) {
assertThat(impacts).containsExactly(createCollectionElementImpact(IdentifiableType.GENERATOR));
}

@Test
public void testCheckSqlRequestsCount() throws Exception {
UUID modificationUuid = saveModification(buildModification());
Expand Down
14 changes: 7 additions & 7 deletions src/test/resources/application-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ spring:
properties:
dialect: org.hibernate.dialect.H2Dialect
hibernate:
'[format_sql]': true
'[generate_statistics]': true
format_sql: true
generate_statistics: true
dialect: org.hibernate.dialect.H2Dialect
hibernate:
#to turn off schema validation that fails (because of clob types) and blocks tests even if the schema is compatible
ddl-auto: none
logging:
level:
'[org.springframework.orm.jpa]': INFO
'[org.springframework.transaction]': INFO
'[org.hibernate.SQL]': INFO
'[org.hibernate.type.descriptor.sql.BasicBinder]': INFO
level:
org.springframework.orm.jpa: INFO
org.springframework.transaction: INFO
org.hibernate.SQL: INFO
org.hibernate.type.descriptor.sql.BasicBinder: INFO
gridsuite:
services:
report-server:
Expand Down
Loading