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

[WIP] lazy loading of operational limits group #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ private ResponseEntity<ExtensionAttributesTopLevelDocument> getExtensionAttribut
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(ExtensionAttributesTopLevelDocument.empty()));
}

private ResponseEntity<OperationalLimitsGroupAttributesTopLevelDocument> getOperationalLimitsGroupAttributes(Supplier<Optional<OperationalLimitsGroupAttributes>> f) {
return f.get()
.map(resource -> ResponseEntity.ok(OperationalLimitsGroupAttributesTopLevelDocument.of(resource)))
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(OperationalLimitsGroupAttributesTopLevelDocument.empty()));
}

private <T extends IdentifiableAttributes> ResponseEntity<Void> createAll(Consumer<List<Resource<T>>> f, List<Resource<T>> resources) {
f.accept(resources);
return ResponseEntity.status(HttpStatus.CREATED).build();
Expand Down Expand Up @@ -1522,4 +1528,48 @@ public ResponseEntity<Void> removeExtensionAttributes(@Parameter(description = "
repository.removeExtensionAttributes(networkId, variantNum, identifiableId, extensionName);
return ResponseEntity.ok().build();
}

@GetMapping(value = "{networkId}/{variantNum}/branch/{branchId}/types/{resourceType}/operationalLimitsGroup/{operationalLimitsGroupId}/side/{side}")
@Operation(summary = "Get an operational limit group on attributes by its identifiable id and extension name")
@ApiResponses(@ApiResponse(responseCode = "200", description = "Successfully get operational limits group attributes"))
public ResponseEntity<OperationalLimitsGroupAttributesTopLevelDocument> getOperationalLimitsGroupAttributes(@Parameter(description = "Network ID", required = true) @PathVariable("networkId") UUID networkId,
@Parameter(description = "Variant number", required = true) @PathVariable("variantNum") int variantNum,
@Parameter(description = "Resource type", required = true) @PathVariable("resourceType") ResourceType type,
@Parameter(description = "Branch id", required = true) @PathVariable("branchId") String branchId,
@Parameter(description = "Operational Limits Group id", required = true) @PathVariable("operationalLimitsGroupId") String operationalLimitsGroupId,
@Parameter(description = "Branch side", required = true) @PathVariable("side") int side) {
return getOperationalLimitsGroupAttributes(() -> repository.getOperationalLimitsGroup(networkId, variantNum, branchId, type, operationalLimitsGroupId, side));
}

@GetMapping(value = "{networkId}/{variantNum}/branch/types/{resourceType}/operationalLimitsGroup/")
@Operation(summary = "Get all operational limits group attributes for a specific type of equipment")
@ApiResponses(@ApiResponse(responseCode = "200", description = "Successfully get extension attributes"))
public ResponseEntity<Map<String, Map<OperationalLimitsGroupIdentifier, OperationalLimitsGroupAttributes>>> getAllOperationalLimitsGroupsAttributesByResourceType(@Parameter(description = "Network ID", required = true) @PathVariable("networkId") UUID networkId,
@Parameter(description = "Variant number", required = true) @PathVariable("variantNum") int variantNum,
@Parameter(description = "Resource type", required = true) @PathVariable("resourceType") ResourceType type) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(
repository.getAllOperationalLimitsGroupAttributesByResourceType(networkId, variantNum, type));
}

@GetMapping(value = "{networkId}/{variantNum}/branch/{branchId}/types/{resourceType}/operationalLimitsGroup/currentLimits/{operationalLimitsGroupId}/side/{side}")
@Operation(summary = "Get an operational limit group on attributes by its identifiable id and extension name")
@ApiResponses(@ApiResponse(responseCode = "200", description = "Successfully get operational limits group attributes"))
public ResponseEntity<OperationalLimitsGroupAttributesTopLevelDocument> getSelectedCurrentLimitsGroupAttributes(@Parameter(description = "Network ID", required = true) @PathVariable("networkId") UUID networkId,
@Parameter(description = "Variant number", required = true) @PathVariable("variantNum") int variantNum,
@Parameter(description = "Resource type", required = true) @PathVariable("resourceType") ResourceType type,
@Parameter(description = "Branch id", required = true) @PathVariable("branchId") String branchId,
@Parameter(description = "Operational Limits Group id", required = true) @PathVariable("operationalLimitsGroupId") String operationalLimitsGroupId,
@Parameter(description = "Branch side", required = true) @PathVariable("side") int side) {
return getOperationalLimitsGroupAttributes(() -> repository.getSelectedCurrentLimitsGroup(networkId, variantNum, branchId, type, operationalLimitsGroupId, side));
}

@GetMapping(value = "{networkId}/{variantNum}/branch/types/{resourceType}/operationalLimitsGroup/currentLimits/")
@Operation(summary = "Get all current limits for selected operational groups for a specific type of equipment")
@ApiResponses(@ApiResponse(responseCode = "200", description = "Successfully get extension attributes"))
public ResponseEntity<Map<String, Map<OperationalLimitsGroupIdentifier, OperationalLimitsGroupAttributes>>> getAllCurrentLimitsAttributesByResourceType(@Parameter(description = "Network ID", required = true) @PathVariable("networkId") UUID networkId,
@Parameter(description = "Variant number", required = true) @PathVariable("variantNum") int variantNum,
@Parameter(description = "Resource type", required = true) @PathVariable("resourceType") ResourceType type) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(
repository.getAllCurrentLimitsGroupAttributesByResourceType(networkId, variantNum, type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public class NetworkStoreRepository {

private static final Logger LOGGER = LoggerFactory.getLogger(NetworkStoreRepository.class);

public NetworkStoreRepository(DataSource dataSource, ObjectMapper mapper, Mappings mappings, ExtensionHandler extensionHandler) {
public NetworkStoreRepository(DataSource dataSource, ObjectMapper mapper, Mappings mappings, ExtensionHandler extensionHandler, LimitsHandler limitsHandler) {
this.dataSource = dataSource;
this.mappings = mappings;
this.mapper = mapper.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
this.extensionHandler = extensionHandler;
this.limitsHandler = limitsHandler;
}

@Getter
Expand All @@ -74,6 +75,8 @@ public NetworkStoreRepository(DataSource dataSource, ObjectMapper mapper, Mappin

private final ExtensionHandler extensionHandler;

private final LimitsHandler limitsHandler;

static final int BATCH_SIZE = 1000;

private static final String SUBSTATION_ID = "substationid";
Expand Down Expand Up @@ -588,7 +591,6 @@ private <T extends IdentifiableAttributes> Resource<T> completeResourceInfos(Res
return switch (resource.getType()) {
case GENERATOR -> completeGeneratorInfos(resource, networkUuid, variantNum, equipmentId);
case BATTERY -> completeBatteryInfos(resource, networkUuid, variantNum, equipmentId);
case LINE -> completeLineInfos(resource, networkUuid, variantNum, equipmentId);
case TWO_WINDINGS_TRANSFORMER ->
completeTwoWindingsTransformerInfos(resource, networkUuid, variantNum, equipmentId);
case THREE_WINDINGS_TRANSFORMER ->
Expand Down Expand Up @@ -616,17 +618,8 @@ private <T extends IdentifiableAttributes> Resource<T> completeBatteryInfos(Reso
return resource;
}

private <T extends IdentifiableAttributes> Resource<T> completeLineInfos(Resource<T> resource, UUID networkUuid, int variantNum, String equipmentId) {
Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfos(networkUuid, variantNum, EQUIPMENT_ID_COLUMN, equipmentId);
insertLimitsInEquipments(networkUuid, List.of((Resource<LineAttributes>) resource), limitsInfos);
return resource;
}

private <T extends IdentifiableAttributes> Resource<T> completeTwoWindingsTransformerInfos(Resource<T> resource, UUID networkUuid, int variantNum, String equipmentId) {
Resource<TwoWindingsTransformerAttributes> twoWindingsTransformerResource = (Resource<TwoWindingsTransformerAttributes>) resource;
Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfos(networkUuid, variantNum, EQUIPMENT_ID_COLUMN, equipmentId);
insertLimitsInEquipments(networkUuid, List.of(twoWindingsTransformerResource), limitsInfos);

Map<OwnerInfo, List<TapChangerStepAttributes>> tapChangerSteps = getTapChangerSteps(networkUuid, variantNum, EQUIPMENT_ID_COLUMN, equipmentId);
insertTapChangerStepsInEquipments(networkUuid, List.of(twoWindingsTransformerResource), tapChangerSteps);
insertRegulatingPointIntoTwoWindingsTransformer(networkUuid, variantNum, equipmentId, twoWindingsTransformerResource);
Expand Down Expand Up @@ -1339,9 +1332,6 @@ public Optional<Resource<TwoWindingsTransformerAttributes>> getTwoWindingsTransf
public List<Resource<TwoWindingsTransformerAttributes>> getTwoWindingsTransformers(UUID networkUuid, int variantNum) {
List<Resource<TwoWindingsTransformerAttributes>> twoWindingsTransformers = getIdentifiables(networkUuid, variantNum, mappings.getTwoWindingsTransformerMappings());

Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfos(networkUuid, variantNum, EQUIPMENT_TYPE_COLUMN, ResourceType.TWO_WINDINGS_TRANSFORMER.toString());
insertLimitsInEquipments(networkUuid, twoWindingsTransformers, limitsInfos);

Map<OwnerInfo, List<TapChangerStepAttributes>> tapChangerSteps = getTapChangerSteps(networkUuid, variantNum, EQUIPMENT_TYPE_COLUMN, ResourceType.TWO_WINDINGS_TRANSFORMER.toString());
insertTapChangerStepsInEquipments(networkUuid, twoWindingsTransformers, tapChangerSteps);
// regulating points
Expand All @@ -1355,9 +1345,6 @@ public List<Resource<TwoWindingsTransformerAttributes>> getVoltageLevelTwoWindin

List<String> equipmentsIds = twoWindingsTransformers.stream().map(Resource::getId).collect(Collectors.toList());

Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfosWithInClause(networkUuid, variantNum, EQUIPMENT_ID_COLUMN, equipmentsIds);
insertLimitsInEquipments(networkUuid, twoWindingsTransformers, limitsInfos);

Map<OwnerInfo, List<TapChangerStepAttributes>> tapChangerSteps = getTapChangerStepsWithInClause(networkUuid, variantNum, EQUIPMENT_ID_COLUMN, equipmentsIds);
insertTapChangerStepsInEquipments(networkUuid, twoWindingsTransformers, tapChangerSteps);

Expand Down Expand Up @@ -1514,9 +1501,6 @@ public Optional<Resource<LineAttributes>> getLine(UUID networkUuid, int variantN
public List<Resource<LineAttributes>> getLines(UUID networkUuid, int variantNum) {
List<Resource<LineAttributes>> lines = getIdentifiables(networkUuid, variantNum, mappings.getLineMappings());

Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfos(networkUuid, variantNum, EQUIPMENT_TYPE_COLUMN, ResourceType.LINE.toString());
insertLimitsInEquipments(networkUuid, lines, limitsInfos);

setRegulatingEquipments(lines, networkUuid, variantNum, ResourceType.LINE);

return lines;
Expand All @@ -1527,9 +1511,6 @@ public List<Resource<LineAttributes>> getVoltageLevelLines(UUID networkUuid, int

List<String> equipmentsIds = lines.stream().map(Resource::getId).collect(Collectors.toList());

Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfosWithInClause(networkUuid, variantNum, EQUIPMENT_ID_COLUMN, equipmentsIds);
insertLimitsInEquipments(networkUuid, lines, limitsInfos);

setRegulatingEquipmentsWithIds(lines, networkUuid, variantNum, ResourceType.LINE, equipmentsIds);

return lines;
Expand Down Expand Up @@ -3118,4 +3099,30 @@ public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesB
public void removeExtensionAttributes(UUID networkId, int variantNum, String identifiableId, String extensionName) {
extensionHandler.deleteExtensionsFromIdentifiables(networkId, variantNum, Map.of(identifiableId, Set.of(extensionName)));
}

// operational limits groups
public Optional<OperationalLimitsGroupAttributes> getOperationalLimitsGroup(UUID networkId, int variantNum, String branchId, ResourceType type, String operationalLimitsGroupName, int side) {
return limitsHandler.getOperationalLimitsGroup(networkId, variantNum, branchId, type, operationalLimitsGroupName, side);
}

public Optional<OperationalLimitsGroupAttributes> getSelectedCurrentLimitsGroup(UUID networkId, int variantNum, String branchId, ResourceType type, String operationalLimitsGroupName, int side) {
return limitsHandler.getSelectedCurrentLimitsGroup(networkId, variantNum, branchId, type, operationalLimitsGroupName, side);
}

public Map<String, Map<OperationalLimitsGroupIdentifier, OperationalLimitsGroupAttributes>> getAllOperationalLimitsGroupAttributesByResourceType(
UUID networkId, int variantNum, ResourceType type) {
Map<OwnerInfo, LimitsInfos> limitsInfos = getLimitsInfos(networkId, variantNum, EQUIPMENT_TYPE_COLUMN, type.toString());
Map<String, Map<OperationalLimitsGroupIdentifier, OperationalLimitsGroupAttributes>> map = new HashMap<>();
limitsInfos.forEach((owner, limitsInfo) -> {
Map<OperationalLimitsGroupIdentifier, OperationalLimitsGroupAttributes> operationalLimitsGroupAttributesMap = limitsHandler.convertLimitInfosToOperationalLimitsGroup(owner, limitsInfo);
map.put(owner.getEquipmentId(), operationalLimitsGroupAttributesMap);
});
return map;
}

public Map<String, Map<OperationalLimitsGroupIdentifier, OperationalLimitsGroupAttributes>> getAllCurrentLimitsGroupAttributesByResourceType(
UUID networkId, int variantNum, ResourceType type) {
return limitsHandler.getAllCurrentLimitsGroupAttributesByResourceType(networkId, variantNum, type,
getLimitsInfos(networkId, variantNum, EQUIPMENT_TYPE_COLUMN, type.toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public final class QueryCatalog {
static final String REGULATION_MODE = "regulationMode";
public static final String SIDE_COLUMN = "side";
static final String REGULATING = "regulating";
public static final String SELECTED_OPERATIONAL_LIMITS_GROUP_ID1 = "selectedoperationallimitsgroupid1";
public static final String SELECTED_OPERATIONAL_LIMITS_GROUP_ID2 = "selectedoperationallimitsgroupid2";

private QueryCatalog() {
}
Expand All @@ -62,6 +64,14 @@ public static String buildGetIdentifiableQuery(String tableName, Collection<Stri
" and " + ID_COLUMN + " = ?";
}

public static String buildGetIdentifiablesSpecificColumnsQuery(String tableName, Collection<String> columns) {
return "select " +
String.join(", ", columns) +
" from " + tableName +
" where " + NETWORK_UUID_COLUMN + " = ?" +
" and " + VARIANT_NUM_COLUMN + " = ?";
}

public static String buildGetNetworkQuery(Collection<String> columns) {
return "select " + ID_COLUMN + ", " +
String.join(", ", columns) +
Expand Down Expand Up @@ -305,6 +315,15 @@ public static String buildTemporaryLimitQuery(String columnNameForWhereClause) {
columnNameForWhereClause + " = ?";
}

public static String buildTemporaryLimitQuery() {
return "select " + TEMPORARY_LIMITS_COLUMN +
" from " + TEMPORARY_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
EQUIPMENT_TYPE_COLUMN + " = ? and " +
EQUIPMENT_ID_COLUMN + " = ?";
}

public static String buildTemporaryLimitWithInClauseQuery(String columnNameForInClause, int numberOfValues) {
if (numberOfValues < 1) {
throw new IllegalArgumentException(MINIMAL_VALUE_REQUIREMENT_ERROR);
Expand Down Expand Up @@ -371,6 +390,15 @@ public static String buildPermanentLimitQuery(String columnNameForWhereClause) {
columnNameForWhereClause + " = ?";
}

public static String buildPermanentLimitQuery() {
return "select " + PERMANENT_LIMITS_COLUMN +
" from " + PERMANENT_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
EQUIPMENT_TYPE_COLUMN + " = ? and " +
EQUIPMENT_ID_COLUMN + " = ?";
}

public static String buildPermanentLimitWithInClauseQuery(String columnNameForInClause, int numberOfValues) {
if (numberOfValues < 1) {
throw new IllegalArgumentException(MINIMAL_VALUE_REQUIREMENT_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.network.store.model.ResourceType;
import com.powsybl.network.store.model.TemporaryLimitAttributes;
import com.powsybl.network.store.server.ExtensionHandler;
import com.powsybl.network.store.server.LimitsHandler;
import com.powsybl.network.store.server.Mappings;
import com.powsybl.network.store.server.NetworkStoreRepository;
import com.powsybl.network.store.server.dto.OwnerInfo;
Expand Down Expand Up @@ -49,7 +50,8 @@ public class V211LimitsMigration implements CustomTaskChange {
public void init(Database database) {
DataSource dataSource = new SingleConnectionDataSource(((JdbcConnection) database.getConnection()).getUnderlyingConnection(), true);
ObjectMapper mapper = new ObjectMapper();
this.repository = new NetworkStoreRepository(dataSource, mapper, new Mappings(), new ExtensionHandler(dataSource, mapper));
this.repository = new NetworkStoreRepository(dataSource, mapper, new Mappings(),
new ExtensionHandler(dataSource, mapper), new LimitsHandler(dataSource, mapper));
}

@Override
Expand Down
Loading
Loading