Skip to content

Commit

Permalink
Remove the id automatically generated to have a unique index for ELS (#…
Browse files Browse the repository at this point in the history
…125)

Signed-off-by: Slimane AMAR <slimane.amar@rte-france.com>
  • Loading branch information
SlimaneAmar authored Nov 24, 2023
1 parent 7126f42 commit c3a3207
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,7 @@ public void deleteAllEquipmentInfosOnInitialVariant(UUID networkUuid) {
}

public List<EquipmentInfos> getAllEquipmentInfos(UUID networkUuid) {
List<EquipmentInfos> infos = new ArrayList<>();
equipmentInfosService.findAll(networkUuid).forEach(infos::add);
return infos;
return equipmentInfosService.findAll(networkUuid);
}

public boolean hasEquipmentInfos(UUID networkUuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import com.powsybl.network.conversion.server.NetworkConversionException;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.elasticsearch.annotations.*;

import java.util.Set;
import java.util.UUID;
Expand All @@ -37,7 +33,16 @@
@TypeAlias(value = "EquipmentInfos")
public class EquipmentInfos {
@Id
String uniqueId;
@AccessType(AccessType.Type.PROPERTY)
@SuppressWarnings("unused")
public String getUniqueId() {
return networkUuid + "_" + variantId + "_" + id;
}

@SuppressWarnings("unused")
public void setUniqueId(String uniqueId) {
// No setter because it a composite value
}

@MultiField(
mainField = @Field(name = "equipmentId", type = FieldType.Text),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.UUID;

/**
* @author Slimane Amar <slimane.amar at rte-france.com>
*/
public interface EquipmentInfosRepository extends ElasticsearchRepository<EquipmentInfos, String> {
Iterable<EquipmentInfos> findAllByNetworkUuid(@NonNull UUID networkUuid);
List<EquipmentInfos> findAllByNetworkUuid(@NonNull UUID networkUuid);

long countByNetworkUuid(@NonNull UUID networkUuid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void addAll(@NonNull final List<EquipmentInfos> equipmentsInfos) {
.forEach(equipmentInfosRepository::saveAll);
}

public Iterable<EquipmentInfos> findAll(@NonNull UUID networkUuid) {
public List<EquipmentInfos> findAll(@NonNull UUID networkUuid) {
return equipmentInfosRepository.findAllByNetworkUuid(networkUuid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.network.conversion.server;

import com.google.common.collect.Iterables;
import com.powsybl.commons.datasource.ReadOnlyDataSource;
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
Expand All @@ -28,8 +27,6 @@
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -61,16 +58,25 @@ public void testAddDeleteEquipmentInfos() {
EquipmentInfos.builder().networkUuid(NETWORK_UUID).variantId(VariantManagerConstants.INITIAL_VARIANT_ID).id("id1").name("name1").type(IdentifiableType.LOAD.name()).voltageLevels(Set.of(VoltageLevelInfos.builder().id("vl1").name("vl1").build())).substations(Set.of(SubstationInfos.builder().id("s1").name("s1").build())).build(),
EquipmentInfos.builder().networkUuid(NETWORK_UUID).variantId(VariantManagerConstants.INITIAL_VARIANT_ID).id("id2").name("name2").type(IdentifiableType.LOAD.name()).voltageLevels(Set.of(VoltageLevelInfos.builder().id("vl2").name("vl2").build())).substations(Set.of(SubstationInfos.builder().id("s2").name("s2").build())).build()
);

equipmentInfosService.addAll(infos);
Iterable<EquipmentInfos> all = equipmentInfosService.findAll(NETWORK_UUID);
assertEquals(2, Iterables.size(all));
List<EquipmentInfos> allList = StreamSupport.stream(all.spliterator(), false)
.collect(Collectors.toList());
assertEquals(infos, allList);
List<EquipmentInfos> infosDB = equipmentInfosService.findAll(NETWORK_UUID);
assertEquals(2, infosDB.size());
assertEquals(infos, infosDB);
assertEquals(infos.stream().map(i -> i.getNetworkUuid() + "_" + i.getVariantId() + "_" + i.getId()).toList(), infosDB.stream().map(i -> i.getUniqueId()).toList());

// Change names but uniqueIds are same
infos = List.of(
EquipmentInfos.builder().networkUuid(NETWORK_UUID).variantId(VariantManagerConstants.INITIAL_VARIANT_ID).id("id1").name("newName1").type(IdentifiableType.LOAD.name()).voltageLevels(Set.of(VoltageLevelInfos.builder().id("vl1").name("vl1").build())).substations(Set.of(SubstationInfos.builder().id("s1").name("s1").build())).build(),
EquipmentInfos.builder().networkUuid(NETWORK_UUID).variantId(VariantManagerConstants.INITIAL_VARIANT_ID).id("id2").name("newName2").type(IdentifiableType.LOAD.name()).voltageLevels(Set.of(VoltageLevelInfos.builder().id("vl2").name("vl2").build())).substations(Set.of(SubstationInfos.builder().id("s2").name("s2").build())).build()
);
equipmentInfosService.addAll(infos);
infosDB = equipmentInfosService.findAll(NETWORK_UUID);
assertEquals(2, infosDB.size());
assertEquals(infos, infosDB);
assertEquals(infos.stream().map(i -> i.getNetworkUuid() + "_" + i.getVariantId() + "_" + i.getId()).toList(), infosDB.stream().map(i -> i.getUniqueId()).toList());

equipmentInfosService.deleteAllOnInitialVariant(NETWORK_UUID);
assertEquals(0, Iterables.size(equipmentInfosService.findAll(NETWORK_UUID)));
assertEquals(0, equipmentInfosService.findAll(NETWORK_UUID).size());
}

@Test
Expand All @@ -80,7 +86,7 @@ public void testEquipmentInfos() {
UUID networkUuid = UUID.randomUUID();

VoltageLevel vl = network.getVoltageLevel("BBE1AA1");
EquipmentInfos equipmentInfos = networkConversionService.toEquipmentInfos(vl, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
EquipmentInfos equipmentInfos = NetworkConversionService.toEquipmentInfos(vl, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
EquipmentInfos expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -93,7 +99,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

Substation substation = network.getSubstation("BBE1AA");
equipmentInfos = networkConversionService.toEquipmentInfos(substation, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(substation, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -112,7 +118,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

Switch switch1 = network.getSwitch("FRA1AA1_switch");
equipmentInfos = networkConversionService.toEquipmentInfos(switch1, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(switch1, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -125,7 +131,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

Load load = network.getLoad("BBE1AA1 _load");
equipmentInfos = networkConversionService.toEquipmentInfos(load, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(load, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -138,7 +144,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

Bus bus = network.getBusBreakerView().getBus("BBE1AA1 ");
equipmentInfos = networkConversionService.toEquipmentInfos(bus, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(bus, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -151,7 +157,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

Generator generator = network.getGenerator("BBE1AA1 _generator");
equipmentInfos = networkConversionService.toEquipmentInfos(generator, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(generator, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -164,7 +170,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

HvdcLine hvdcLine = network.getHvdcLine("FRA1AA_BBE1AA_hvdcline");
equipmentInfos = networkConversionService.toEquipmentInfos(hvdcLine, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(hvdcLine, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -177,7 +183,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

TwoWindingsTransformer twoWindingsTransformer = network.getTwoWindingsTransformer("BBE1AA2 BBE3AA1 2");
equipmentInfos = networkConversionService.toEquipmentInfos(twoWindingsTransformer, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(twoWindingsTransformer, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -190,7 +196,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

ThreeWindingsTransformer threeWindingsTransformer = network.getThreeWindingsTransformer("BBE1AA_w3t");
equipmentInfos = networkConversionService.toEquipmentInfos(threeWindingsTransformer, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(threeWindingsTransformer, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -203,7 +209,7 @@ public void testEquipmentInfos() {
assertEquals(expectedEquipmentInfos, equipmentInfos);

Line line = network.getLine("BBE1AA1 BBE2AA1 1");
equipmentInfos = networkConversionService.toEquipmentInfos(line, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(line, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand All @@ -221,7 +227,7 @@ public void testEquipmentInfos() {
networkUuid = UUID.randomUUID();

line = network.getLine("LINE_S1VL1");
equipmentInfos = networkConversionService.toEquipmentInfos(line, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
equipmentInfos = NetworkConversionService.toEquipmentInfos(line, networkUuid, VariantManagerConstants.INITIAL_VARIANT_ID);
expectedEquipmentInfos = EquipmentInfos.builder()
.networkUuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
Expand Down

0 comments on commit c3a3207

Please sign in to comment.