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

optimisation: group requests to filter server #55

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gridsuite.sensitivityanalysis.server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class FilterEquipments {
@Schema(description = "filter id")
private UUID filterId;

@Schema(description = "equipments of filter")
private List<IdentifiableAttributes> identifiableAttributes;

@Schema(description = "equipments not found in network")
private List<String> notFoundEquipments;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.gridsuite.sensitivityanalysis.server.service;

import org.apache.commons.lang3.StringUtils;
import org.gridsuite.sensitivityanalysis.server.dto.FilterEquipments;
import org.gridsuite.sensitivityanalysis.server.dto.IdentifiableAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -19,6 +20,7 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -35,28 +37,42 @@ public class FilterService {
private final RestTemplate restTemplate = new RestTemplate();
private String filterServerBaseUri;
public static final String NETWORK_UUID = "networkUuid";

public static final String IDS = "ids";
private static final String QUERY_PARAM_VARIANT_ID = "variantId";

@Autowired
public FilterService(@Value("${gridsuite.services.filter-server.base-uri:http://filter-server/}") String filterServerBaseUri) {
this.filterServerBaseUri = filterServerBaseUri;
}

public List<IdentifiableAttributes> getIdentifiablesFromFilter(UUID uuid, UUID networkUuid, String variantId) {
Objects.requireNonNull(uuid);
public List<IdentifiableAttributes> getIdentifiablesFromFilters(List<UUID> filterUuids, UUID networkUuid, String variantId) {
List<FilterEquipments> filterEquipments = getFilterEquipements(filterUuids, networkUuid, variantId);

List<IdentifiableAttributes> mergedIdentifiables = new ArrayList<>();
for (FilterEquipments filterEquipment : filterEquipments) {
mergedIdentifiables.addAll(filterEquipment.getIdentifiableAttributes());
}

return mergedIdentifiables;
}

public List<FilterEquipments> getFilterEquipements(List<UUID> filterUuids, UUID networkUuid, String variantId) {
Objects.requireNonNull(filterUuids);
Objects.requireNonNull(networkUuid);

var uriComponentsBuilder = UriComponentsBuilder
.fromPath(DELIMITER + FILTER_API_VERSION + "/filters/{id}/export")
.queryParam(NETWORK_UUID, networkUuid.toString());
.fromPath(DELIMITER + FILTER_API_VERSION + "/filters/export")
.queryParam(IDS, filterUuids)
.queryParam(NETWORK_UUID, networkUuid.toString());
if (!StringUtils.isBlank(variantId)) {
uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId);
}
var path = uriComponentsBuilder.buildAndExpand(uuid).toUriString();
var path = uriComponentsBuilder.build().toUriString();

return restTemplate.exchange(filterServerBaseUri + path, HttpMethod.GET, null,
new ParameterizedTypeReference<List<IdentifiableAttributes>>() {
}).getBody();
new ParameterizedTypeReference<List<FilterEquipments>>() {
}).getBody();
}

public Map<String, List<Long>> getIdentifiablesCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class SensitivityAnalysisInputBuilderService {
private final ActionsService actionsService;
private final FilterService filterService;

private static final String NAMES = "names";

public SensitivityAnalysisInputBuilderService(ActionsService actionsService, FilterService filterService) {
this.actionsService = actionsService;
this.filterService = filterService;
Expand Down Expand Up @@ -99,17 +101,20 @@ private double getLoadWeight(Load load, SensitivityAnalysisInputData.Distributio
}
}

private List<IdentifiableAttributes> goGetIdentifiables(EquipmentsContainer filter, UUID networkUuid, String variantId, Reporter reporter) {
private List<IdentifiableAttributes> goGetIdentifiables(List<EquipmentsContainer> filters, UUID networkUuid, String variantId, Reporter reporter) {
String containersNames = getContainerNames(filters);
try {
return filterService.getIdentifiablesFromFilter(filter.getContainerId(), networkUuid, variantId);
//extract container id from filters
List<UUID> filterIds = filters.stream().map(EquipmentsContainer::getContainerId).toList();
return filterService.getIdentifiablesFromFilters(filterIds, networkUuid, variantId);
} catch (Exception ex) {
LOGGER.error("Could not get identifiables from filter " + filter.getContainerName(), ex);
LOGGER.error("Could not get identifiables from filter " + containersNames, ex);
reporter.report(Report.builder()
.withKey("filterTranslationFailure")
.withDefaultMessage("Could not get identifiables from filter ${name} : ${exception}")
.withDefaultMessage("Could not get identifiables from filters ${names} : ${exception}")
.withSeverity(TypedValue.ERROR_SEVERITY)
.withValue("exception", ex.getMessage())
.withValue("name", filter.getContainerName())
.withValue(NAMES, containersNames)
.build());
return List.of();
}
Expand Down Expand Up @@ -141,36 +146,41 @@ private Long getFactorsCount(Map<String, List<UUID>> ids, UUID networkUuid, Stri
return contAttributesCountTemp;
}

private Stream<IdentifiableAttributes> getIdentifiablesFromContainer(SensitivityAnalysisRunContext context, EquipmentsContainer filter,
List<IdentifiableType> equipmentsTypesAllowed, Reporter reporter) {

List<IdentifiableAttributes> listIdentAttributes = goGetIdentifiables(filter, context.getNetworkUuid(), context.getVariantId(), reporter);
private Stream<IdentifiableAttributes> getIdentifiablesFromContainers(SensitivityAnalysisRunContext context, List<EquipmentsContainer> filters,
List<IdentifiableType> equipmentsTypesAllowed, Reporter reporter) {
String containersNames = getContainerNames(filters);
List<IdentifiableAttributes> listIdentifiableAttributes = goGetIdentifiables(filters, context.getNetworkUuid(), context.getVariantId(), reporter);

// check that monitored equipments type is allowed
if (!listIdentAttributes.stream().allMatch(i -> equipmentsTypesAllowed.contains(i.getType()))) {
if (!listIdentifiableAttributes.stream().allMatch(i -> equipmentsTypesAllowed.contains(i.getType()))) {
reporter.report(Report.builder()
.withKey("badEquipmentType")
.withDefaultMessage("Equipments type in filter with name=${name} should be ${expectedType} : filter is ignored")
.withValue("name", filter.getContainerName())
.withDefaultMessage("Equipments type in filter with name=${names} should be ${expectedType} : filter is ignored")
.withValue(NAMES, containersNames)
.withValue(EXPECTED_TYPE, equipmentsTypesAllowed.toString())
.withSeverity(TypedValue.WARN_SEVERITY)
.build());
return Stream.empty();
}

return listIdentAttributes.stream();
return listIdentifiableAttributes.stream();
}

private Stream<IdentifiableAttributes> getMonitoredIdentifiablesFromContainer(SensitivityAnalysisRunContext context, Network network, EquipmentsContainer filter, List<IdentifiableType> equipmentsTypesAllowed, Reporter reporter) {
private String getContainerNames(List<EquipmentsContainer> filters) {
List<String> containerNamesList = filters.stream().map(EquipmentsContainer::getContainerName).toList();
return "[" + String.join(", ", containerNamesList) + "]";
}

List<IdentifiableAttributes> listIdentAttributes = goGetIdentifiables(filter, context.getNetworkUuid(), context.getVariantId(), reporter);
private Stream<IdentifiableAttributes> getMonitoredIdentifiablesFromContainers(SensitivityAnalysisRunContext context, Network network, List<EquipmentsContainer> filters, List<IdentifiableType> equipmentsTypesAllowed, Reporter reporter) {
String containersNames = getContainerNames(filters);
List<IdentifiableAttributes> listIdentAttributes = goGetIdentifiables(filters, context.getNetworkUuid(), context.getVariantId(), reporter);

// check that monitored equipments type is allowed
if (!listIdentAttributes.stream().allMatch(i -> equipmentsTypesAllowed.contains(i.getType()))) {
reporter.report(Report.builder()
.withKey("badMonitoredEquipmentType")
.withDefaultMessage("Monitored equipments type in filter with name=${name} should be ${expectedType} : filter is ignored")
.withValue("name", filter.getContainerName())
.withDefaultMessage("Monitored equipments type in filter with name=${names} should be ${expectedType} : filter is ignored")
.withValue(NAMES, containersNames)
.withValue(EXPECTED_TYPE, equipmentsTypesAllowed.toString())
.withSeverity(TypedValue.WARN_SEVERITY)
.build());
Expand Down Expand Up @@ -230,10 +240,11 @@ private List<SensitivityVariableSet> buildSensitivityVariableSets(SensitivityAna
List<EquipmentsContainer> filters,
SensitivityAnalysisInputData.DistributionType distributionType) {
List<SensitivityVariableSet> result = new ArrayList<>();

Stream<Pair<String, List<IdentifiableAttributes>>> variablesContainersLists = filters.stream()
.map(filter -> Pair.of(filter.getContainerName(), getIdentifiablesFromContainer(context, filter, variablesTypesAllowed, reporter).collect(Collectors.toList())))
.filter(list -> !list.getRight().isEmpty());
List<IdentifiableAttributes> monitoredVariablesContainersLists = getIdentifiablesFromContainers(context, filters, variablesTypesAllowed, reporter)
.toList();
String containerNames = Arrays.toString(filters.stream().map(EquipmentsContainer::getContainerName).toList().toArray());
Stream<Pair<String, List<IdentifiableAttributes>>> variablesContainersLists = Stream.of(Pair.of(containerNames, monitoredVariablesContainersLists))
.filter(list -> !list.getRight().isEmpty());

variablesContainersLists.forEach(variablesList -> {
List<WeightedSensitivityVariable> variables = new ArrayList<>();
Expand Down Expand Up @@ -296,9 +307,7 @@ private List<SensitivityFactor> buildSensitivityFactorsFromVariablesSets(Sensiti
return List.of();
}

List<IdentifiableAttributes> monitoredEquipments = monitoredEquipmentsContainers.stream()
.flatMap(filter -> getMonitoredIdentifiablesFromContainer(context, network, filter, monitoredEquipmentsTypesAllowed, reporter))
.collect(Collectors.toList());
List<IdentifiableAttributes> monitoredEquipments = getMonitoredIdentifiablesFromContainers(context, network, monitoredEquipmentsContainers, monitoredEquipmentsTypesAllowed, reporter).collect(Collectors.toList());

return getSensitivityFactorsFromEquipments(variablesSets.stream().map(SensitivityVariableSet::getId).collect(Collectors.toList()),
monitoredEquipments, contingencies, sensitivityFunctionType, sensitivityVariableType, true);
Expand All @@ -312,20 +321,16 @@ private List<SensitivityFactor> buildSensitivityFactorsFromEquipments(Sensitivit
List<Contingency> contingencies,
SensitivityFunctionType sensitivityFunctionType,
SensitivityVariableType sensitivityVariableType) {
List<IdentifiableAttributes> equipments = filters.stream()
.flatMap(filter -> getIdentifiablesFromContainer(context, filter, equipmentsTypesAllowed, reporter))
.collect(Collectors.toList());
List<IdentifiableAttributes> equipments = getIdentifiablesFromContainers(context, filters, equipmentsTypesAllowed, reporter).toList();

if (equipments.isEmpty()) {
return List.of();
}

List<IdentifiableAttributes> monitoredEquipments = monitoredEquipmentsContainers.stream()
.flatMap(filter -> getMonitoredIdentifiablesFromContainer(context, network, filter, monitoredEquipmentsTypesAllowed, reporter))
.collect(Collectors.toList());
List<EquipmentsContainer> monitoredEquipementContainer = monitoredEquipmentsContainers.stream().toList();
List<IdentifiableAttributes> monitoredEquipments = getMonitoredIdentifiablesFromContainers(context, network, monitoredEquipementContainer, monitoredEquipmentsTypesAllowed, reporter).toList();

return getSensitivityFactorsFromEquipments(equipments.stream().map(IdentifiableAttributes::getId).collect(Collectors.toList()),
monitoredEquipments, contingencies, sensitivityFunctionType, sensitivityVariableType, false);
monitoredEquipments, contingencies, sensitivityFunctionType, sensitivityVariableType, false);
}

private void buildSensitivityInjectionsSets(SensitivityAnalysisRunContext context, Network network, Reporter reporter) {
Expand Down
Loading
Loading