Skip to content

Commit

Permalink
fix: avoid to perform optimal request twice and complete eager retrie…
Browse files Browse the repository at this point in the history
…val for all tabulars

Signed-off-by: Joris Mancini <[email protected]>
  • Loading branch information
TheMaskedTurtle committed Feb 5, 2024
1 parent 7a5ecc9 commit 24d7e72
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ public class NetworkModificationRepository {

private final ModificationRepository modificationRepository;

private final EquipmentModificationRepositories equipmentModificationRepositories;
private final TabularModificationRepository tabularModificationRepository;

private static final String MODIFICATION_NOT_FOUND_MESSAGE = "Modification (%s) not found";

public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, EquipmentModificationRepositories equipmentModificationRepositories) {
public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, TabularModificationRepository tabularModificationRepository) {
this.modificationGroupRepository = modificationGroupRepository;
this.modificationRepository = modificationRepository;
this.equipmentModificationRepositories = equipmentModificationRepositories;
this.tabularModificationRepository = tabularModificationRepository;
}

@Transactional // To have the 2 delete in the same transaction (atomic)
public void deleteAll() {
modificationRepository.deleteAll();
modificationGroupRepository.deleteAll();
equipmentModificationRepositories.deleteAll();
tabularModificationRepository.deleteAll();
}

@Transactional // To have all create in the same transaction (atomic)
Expand Down Expand Up @@ -141,63 +141,45 @@ public List<ModificationInfos> getModifications(UUID groupUuid, boolean onlyMeta
}

public List<ModificationInfos> getModificationsMetadata(UUID groupUuid, boolean onlyStashed) {
Stream<ModificationEntity> modificationEntitySteam = modificationRepository
Stream<ModificationEntity> modificationEntities = modificationRepository
.findAllBaseByGroupId(getModificationGroup(groupUuid).getId())
.stream();
if (onlyStashed) {
return modificationEntitySteam.filter(m -> m.getStashed())
return modificationEntities.filter(ModificationEntity::getStashed)
.map(this::getModificationInfos)
.collect(Collectors.toList());
} else {
return modificationEntitySteam
return modificationEntities
.map(this::getModificationInfos)
.collect(Collectors.toList());
}
}

public TabularModificationEntity loadTabularModificationSubEntities(TabularModificationEntity tabularModificationEntity) {
switch (tabularModificationEntity.getModificationType()) {
case GENERATOR_MODIFICATION:
equipmentModificationRepositories.getGeneratorModificationRepository().findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList());
break;
default:
break;
}
return tabularModificationEntity;
}

public ModificationInfos getModificationInfos(ModificationEntity modificationEntity) {
if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) {
return loadTabularModificationSubEntities(tabularModificationEntity).toModificationInfos();
}
return modificationEntity.toModificationInfos();
}

public List<ModificationInfos> getModificationsInfos(List<UUID> groupUuids, boolean onlyStashed) {
Stream<ModificationEntity> modificationEntity = groupUuids.stream().flatMap(this::getModificationEntityStream);
Stream<ModificationEntity> modificationEntities = groupUuids.stream().flatMap(this::getModificationEntityStream);
if (onlyStashed) {
return modificationEntity.filter(m -> m.getStashed() == onlyStashed)
return modificationEntities.filter(m -> m.getStashed() == onlyStashed)
.map(this::getModificationInfos)
.collect(Collectors.toList());
} else {
return modificationEntity.map(this::getModificationInfos)
return modificationEntities.map(this::getModificationInfos)
.collect(Collectors.toList());
}
}

@Transactional(readOnly = true)
public ModificationInfos getModificationInfo(UUID modificationUuid) {
Optional<ModificationEntity> optionalModificationEntity = modificationRepository.findById(modificationUuid);
if (!optionalModificationEntity.isPresent()) {
throw new NetworkModificationException(MODIFICATION_NOT_FOUND, modificationUuid.toString());
}
ModificationEntity modificationEntity = optionalModificationEntity.get();
return modificationRepository.findById(modificationUuid)
.map(this::getModificationInfos)
.orElseThrow(() -> new NetworkModificationException(MODIFICATION_NOT_FOUND, modificationUuid.toString()));
}

private ModificationInfos getModificationInfos(ModificationEntity modificationEntity) {
if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) {
List<UUID> ids = modificationRepository.findSubModificationsIds(modificationUuid);
tabularModificationEntity.setModifications(equipmentModificationRepositories.findSubEntities(tabularModificationEntity.getModificationType(), ids));
return getModificationInfos(tabularModificationEntity);
tabularModificationRepository.fillSubEntities(tabularModificationEntity);
return tabularModificationEntity.toModificationInfos();
}
return getModificationInfos(modificationEntity);
return modificationEntity.toModificationInfos();
}

@Transactional // To have the 2 delete in the same transaction (atomic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import org.gridsuite.modification.server.ModificationType;
import org.gridsuite.modification.server.entities.ModificationEntity;
import org.gridsuite.modification.server.entities.TabularModificationEntity;
import org.gridsuite.modification.server.repositories.equipmentmodification.*;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

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

@Repository
public class EquipmentModificationRepositories {
public class TabularModificationRepository {

private final ModificationRepository modificationRepository;

private final BatteryModificationRepository batteryModificationRepository;

Expand All @@ -30,16 +31,17 @@ public class EquipmentModificationRepositories {

private final VoltageLevelModificationRepository voltageLevelModificationRepository;

public EquipmentModificationRepositories(
BatteryModificationRepository batteryModificationRepository,
GeneratorModificationRepository generatorModificationRepository,
LineModificationRepository lineModificationRepository,
LoadModificationRepository loadModificationRepository,
ShuntCompensatorModificationRepository shuntCompensatorModificationRepository,
SubstationModificationRepository substationModificationRepository,
TwoWindingsTransformerModificationRepository twoWindingsTransformerModificationRepository,
VoltageLevelModificationRepository voltageLevelModificationRepository
public TabularModificationRepository(
ModificationRepository modificationRepository, BatteryModificationRepository batteryModificationRepository,
GeneratorModificationRepository generatorModificationRepository,
LineModificationRepository lineModificationRepository,
LoadModificationRepository loadModificationRepository,
ShuntCompensatorModificationRepository shuntCompensatorModificationRepository,
SubstationModificationRepository substationModificationRepository,
TwoWindingsTransformerModificationRepository twoWindingsTransformerModificationRepository,
VoltageLevelModificationRepository voltageLevelModificationRepository
) {
this.modificationRepository = modificationRepository;
this.batteryModificationRepository = batteryModificationRepository;
this.generatorModificationRepository = generatorModificationRepository;
this.lineModificationRepository = lineModificationRepository;
Expand All @@ -62,20 +64,16 @@ public void deleteAll() {
voltageLevelModificationRepository.deleteAll();
}

public GeneratorModificationRepository getGeneratorModificationRepository() {
return generatorModificationRepository;
}

public List<ModificationEntity> findSubEntities(ModificationType modificationType, List<UUID> ids) {
List<ModificationEntity> subEntities = new ArrayList<>();
switch (modificationType) {
case GENERATOR_MODIFICATION -> subEntities.addAll(generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids));
default -> subEntities.addAll(getRepositoryByModificationType(modificationType).findAllById(ids));
}
return subEntities;
public void fillSubEntities(TabularModificationEntity tabularModificationEntity) {
List<UUID> ids = modificationRepository.findSubModificationsIds(tabularModificationEntity.getId());
List<ModificationEntity> modifications = getRepositoryByModificationType(tabularModificationEntity.getModificationType()).findAllEagerlyByIdIn(ids)
.stream()
.map(m -> (ModificationEntity) m)
.toList();
tabularModificationEntity.setModifications(modifications);
}

public JpaRepository<? extends ModificationEntity, UUID> getRepositoryByModificationType(ModificationType modificationType) {
private EagerNetworkModificationRepository<? extends ModificationEntity> getRepositoryByModificationType(ModificationType modificationType) {
switch (modificationType) {
case BATTERY_MODIFICATION -> {
return batteryModificationRepository;
Expand All @@ -101,9 +99,7 @@ public JpaRepository<? extends ModificationEntity, UUID> getRepositoryByModifica
case VOLTAGE_LEVEL_MODIFICATION -> {
return voltageLevelModificationRepository;
}
default -> {
return null;
}
default -> throw new TabularModificationRepositoryException("Type " + modificationType + " should not be present in tabular modifications.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.gridsuite.modification.server.repositories;

/**
* @author Joris Mancini <joris.mancini_externe at rte-france.com>
*/
public class TabularModificationRepositoryException extends RuntimeException {

public TabularModificationRepositoryException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.equipment.modification.BatteryModificationEntity;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface BatteryModificationRepository extends JpaRepository<BatteryModificationEntity, UUID> {
public interface BatteryModificationRepository extends JpaRepository<BatteryModificationEntity, UUID>, EagerNetworkModificationRepository<BatteryModificationEntity> {

@Override
@EntityGraph(attributePaths = "properties", type = EntityGraph.EntityGraphType.LOAD)
List<BatteryModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.ModificationEntity;

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

/**
* @author Joris Mancini <joris.mancini_externe at rte-france.com>
*/
public interface EagerNetworkModificationRepository<T extends ModificationEntity> {

List<T> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface GeneratorModificationRepository extends JpaRepository<GeneratorModificationEntity, UUID> {
public interface GeneratorModificationRepository extends JpaRepository<GeneratorModificationEntity, UUID>, EagerNetworkModificationRepository<GeneratorModificationEntity> {

@EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
List<GeneratorModificationEntity> findAllWithReactiveCapabilityCurvePointsByIdIn(List<UUID> ids);
@Override
@EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints", "properties"}, type = EntityGraph.EntityGraphType.LOAD)
List<GeneratorModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.equipment.modification.LineModificationEntity;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface LineModificationRepository extends JpaRepository<LineModificationEntity, UUID> {
public interface LineModificationRepository extends JpaRepository<LineModificationEntity, UUID>, EagerNetworkModificationRepository<LineModificationEntity> {

@Override
@EntityGraph(attributePaths = "properties", type = EntityGraph.EntityGraphType.LOAD)
List<LineModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.equipment.modification.LoadModificationEntity;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface LoadModificationRepository extends JpaRepository<LoadModificationEntity, UUID> {
public interface LoadModificationRepository extends JpaRepository<LoadModificationEntity, UUID>, EagerNetworkModificationRepository<LoadModificationEntity> {

@Override
@EntityGraph(attributePaths = "properties", type = EntityGraph.EntityGraphType.LOAD)
List<LoadModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.equipment.modification.ShuntCompensatorModificationEntity;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface ShuntCompensatorModificationRepository extends JpaRepository<ShuntCompensatorModificationEntity, UUID> {
public interface ShuntCompensatorModificationRepository extends JpaRepository<ShuntCompensatorModificationEntity, UUID>, EagerNetworkModificationRepository<ShuntCompensatorModificationEntity> {

@Override
@EntityGraph(attributePaths = "properties", type = EntityGraph.EntityGraphType.LOAD)
List<ShuntCompensatorModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.equipment.modification.SubstationModificationEntity;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface SubstationModificationRepository extends JpaRepository<SubstationModificationEntity, UUID> {
public interface SubstationModificationRepository extends JpaRepository<SubstationModificationEntity, UUID>, EagerNetworkModificationRepository<SubstationModificationEntity> {

@Override
@EntityGraph(attributePaths = "properties", type = EntityGraph.EntityGraphType.LOAD)
List<SubstationModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package org.gridsuite.modification.server.repositories.equipmentmodification;

import org.gridsuite.modification.server.entities.equipment.modification.TwoWindingsTransformerModificationEntity;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface TwoWindingsTransformerModificationRepository extends JpaRepository<TwoWindingsTransformerModificationEntity, UUID> {
public interface TwoWindingsTransformerModificationRepository extends JpaRepository<TwoWindingsTransformerModificationEntity, UUID>, EagerNetworkModificationRepository<TwoWindingsTransformerModificationEntity> {

@Override
@EntityGraph(attributePaths = {"properties", "tapChangerSteps"}, type = EntityGraph.EntityGraphType.LOAD)
List<TwoWindingsTransformerModificationEntity> findAllEagerlyByIdIn(List<UUID> ids);
}
Loading

0 comments on commit 24d7e72

Please sign in to comment.