Skip to content

Commit

Permalink
Use default model parameters (for security analysis, sensitivity anal…
Browse files Browse the repository at this point in the history
…ysis, short circuit, voltage init) from user profile, when available (#647)

* Use default model parameters (for security analysis, sensitivity analysis, short circuit, voltage init) from user profile, when available.

Signed-off-by: Franck LECUYER <[email protected]>
  • Loading branch information
FranckLecuyer authored Dec 13, 2024
1 parent 57d5583 commit 0c22b05
Show file tree
Hide file tree
Showing 13 changed files with 927 additions and 176 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,13 +748,13 @@ public ResponseEntity<String> getVoltageInitStatus(@Parameter(description = "Stu

@PostMapping(value = "/studies/{studyUuid}/voltage-init/parameters")
@Operation(summary = "Set voltage init parameters on study")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The voltage init parameters are set")})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The voltage init parameters are set"),
@ApiResponse(responseCode = "204", description = "Reset with user profile cannot be done")})
public ResponseEntity<Void> setVoltageInitParameters(
@PathVariable("studyUuid") UUID studyUuid,
@RequestBody(required = false) StudyVoltageInitParameters voltageInitParameters,
@RequestHeader(HEADER_USER_ID) String userId) {
studyService.setVoltageInitParameters(studyUuid, voltageInitParameters, userId);
return ResponseEntity.ok().build();
return studyService.setVoltageInitParameters(studyUuid, voltageInitParameters, userId) ? ResponseEntity.noContent().build() : ResponseEntity.ok().build();
}

@GetMapping(value = "/studies/{studyUuid}/voltage-init/parameters")
Expand Down Expand Up @@ -919,13 +919,13 @@ public ResponseEntity<String> getDynamicSimulationProvider(@PathVariable("studyU

@PostMapping(value = "/studies/{studyUuid}/short-circuit-analysis/parameters", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "set short-circuit analysis parameters on study, reset to default ones if empty body")
@ApiResponse(responseCode = "200", description = "The short-circuit analysis parameters are set")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The short-circuit analysis parameters are set"),
@ApiResponse(responseCode = "204", description = "Reset with user profile cannot be done")})
public ResponseEntity<Void> setShortCircuitParameters(
@PathVariable("studyUuid") UUID studyUuid,
@RequestBody(required = false) String shortCircuitParametersInfos,
@RequestHeader(HEADER_USER_ID) String userId) {
studyService.setShortCircuitParameters(studyUuid, shortCircuitParametersInfos, userId);
return ResponseEntity.ok().build();
return studyService.setShortCircuitParameters(studyUuid, shortCircuitParametersInfos, userId) ? ResponseEntity.noContent().build() : ResponseEntity.ok().build();
}

@GetMapping(value = "/studies/{studyUuid}/short-circuit-analysis/parameters", produces = MediaType.APPLICATION_JSON_VALUE)
Expand Down Expand Up @@ -1669,13 +1669,13 @@ public ResponseEntity<String> getSecurityAnalysisParametersValues(

@PostMapping(value = "/studies/{studyUuid}/security-analysis/parameters")
@Operation(summary = "set security analysis parameters on study, reset to default ones if empty body")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The security analysis parameters are set")})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The security analysis parameters are set"),
@ApiResponse(responseCode = "204", description = "Reset with user profile cannot be done")})
public ResponseEntity<Void> setSecurityAnalysisParametersValues(
@PathVariable("studyUuid") UUID studyUuid,
@RequestBody(required = false) String securityAnalysisParametersValues,
@RequestHeader(HEADER_USER_ID) String userId) {
studyService.setSecurityAnalysisParametersValues(studyUuid, securityAnalysisParametersValues, userId);
return ResponseEntity.ok().build();
return studyService.setSecurityAnalysisParametersValues(studyUuid, securityAnalysisParametersValues, userId) ? ResponseEntity.noContent().build() : ResponseEntity.ok().build();
}

@GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/voltage-init/modifications", produces = MediaType.TEXT_PLAIN_VALUE)
Expand Down Expand Up @@ -1748,13 +1748,13 @@ public ResponseEntity<String> getSensitivityAnalysisParameters(

@PostMapping(value = "/studies/{studyUuid}/sensitivity-analysis/parameters")
@Operation(summary = "set sensitivity analysis parameters on study, reset to default ones if empty body")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis parameters are set")})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis parameters are set"),
@ApiResponse(responseCode = "204", description = "Reset with user profile cannot be done")})
public ResponseEntity<Void> setSensitivityAnalysisParameters(
@PathVariable("studyUuid") UUID studyUuid,
@RequestBody(required = false) String sensitivityAnalysisParameters,
@RequestHeader(HEADER_USER_ID) String userId) {
studyService.setSensitivityAnalysisParameters(studyUuid, sensitivityAnalysisParameters, userId);
return ResponseEntity.ok().build();
return studyService.setSensitivityAnalysisParameters(studyUuid, sensitivityAnalysisParameters, userId) ? ResponseEntity.noContent().build() : ResponseEntity.ok().build();
}

@GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/sensitivity-analysis/factors-count")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ public class UserProfileInfos {

private UUID loadFlowParameterId;

private UUID securityAnalysisParameterId;

private UUID sensitivityAnalysisParameterId;

private UUID shortcircuitParameterId;

private UUID voltageInitParameterId;

Integer maxAllowedBuilds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ConsumerService {
private final NetworkModificationTreeService networkModificationTreeService;
private final ShortCircuitService shortCircuitService;
private final RootNetworkNodeInfoService rootNetworkNodeInfoService;
private final VoltageInitService voltageInitService;

@Autowired
public ConsumerService(ObjectMapper objectMapper,
Expand All @@ -79,7 +80,8 @@ public ConsumerService(ObjectMapper objectMapper,
UserAdminService userAdminService,
NetworkModificationTreeService networkModificationTreeService,
SensitivityAnalysisService sensitivityAnalysisService,
RootNetworkNodeInfoService rootNetworkNodeInfoService) {
RootNetworkNodeInfoService rootNetworkNodeInfoService,
VoltageInitService voltageInitService) {
this.objectMapper = objectMapper;
this.notificationService = notificationService;
this.studyService = studyService;
Expand All @@ -91,6 +93,7 @@ public ConsumerService(ObjectMapper objectMapper,
this.sensitivityAnalysisService = sensitivityAnalysisService;
this.shortCircuitService = shortCircuitService;
this.rootNetworkNodeInfoService = rootNetworkNodeInfoService;
this.voltageInitService = voltageInitService;
}

@Bean
Expand Down Expand Up @@ -233,12 +236,19 @@ public Consumer<Message<String>> consumeCaseImportSucceeded() {

private void insertStudy(UUID studyUuid, String userId, NetworkInfos networkInfos, CaseInfos caseInfos,
Map<String, String> importParameters, UUID importReportUuid) {
UserProfileInfos userProfileInfos = getUserProfile(userId);

DynamicSimulationParametersInfos dynamicSimulationParameters = DynamicSimulationService.getDefaultDynamicSimulationParameters();
UUID loadFlowParametersUuid = createDefaultLoadFlowParameters(userId, getUserProfile(userId));
UUID shortCircuitParametersUuid = createDefaultShortCircuitAnalysisParameters();
UUID securityAnalysisParametersUuid = createDefaultSecurityAnalysisParameters();
UUID sensitivityAnalysisParametersUuid = createDefaultSensitivityAnalysisParameters();
studyService.insertStudy(studyUuid, userId, networkInfos, caseInfos, loadFlowParametersUuid, shortCircuitParametersUuid, DynamicSimulationService.toEntity(dynamicSimulationParameters, objectMapper), null, securityAnalysisParametersUuid, sensitivityAnalysisParametersUuid, importParameters, importReportUuid);
UUID loadFlowParametersUuid = createDefaultLoadFlowParameters(userId, userProfileInfos);
UUID shortCircuitParametersUuid = createDefaultShortCircuitAnalysisParameters(userId, userProfileInfos);
UUID securityAnalysisParametersUuid = createDefaultSecurityAnalysisParameters(userId, userProfileInfos);
UUID sensitivityAnalysisParametersUuid = createDefaultSensitivityAnalysisParameters(userId, userProfileInfos);
UUID voltageInitParametersUuid = createDefaultVoltageInitParameters(userId, userProfileInfos);

studyService.insertStudy(studyUuid, userId, networkInfos, caseInfos, loadFlowParametersUuid,
shortCircuitParametersUuid, DynamicSimulationService.toEntity(dynamicSimulationParameters, objectMapper),
voltageInitParametersUuid, securityAnalysisParametersUuid, sensitivityAnalysisParametersUuid,
importParameters, importReportUuid);
}

private UserProfileInfos getUserProfile(String userId) {
Expand Down Expand Up @@ -270,7 +280,18 @@ private UUID createDefaultLoadFlowParameters(String userId, UserProfileInfos use
}
}

private UUID createDefaultShortCircuitAnalysisParameters() {
private UUID createDefaultShortCircuitAnalysisParameters(String userId, UserProfileInfos userProfileInfos) {
if (userProfileInfos != null && userProfileInfos.getShortcircuitParameterId() != null) {
// try to access/duplicate the user profile shortcircuit parameters
try {
return shortCircuitService.duplicateParameters(userProfileInfos.getShortcircuitParameterId());
} catch (Exception e) {
// TODO try to report a log in Root subreporter ?
LOGGER.error(String.format("Could not duplicate shortcircuit parameters with id '%s' from user/profile '%s/%s'. Using default parameters",
userProfileInfos.getShortcircuitParameterId(), userId, userProfileInfos.getName()), e);
}
}
// no profile, or no/bad shortcircuit parameters in profile => use default values
try {
return shortCircuitService.createParameters(null);
} catch (final Exception e) {
Expand All @@ -279,7 +300,18 @@ private UUID createDefaultShortCircuitAnalysisParameters() {
}
}

private UUID createDefaultSensitivityAnalysisParameters() {
private UUID createDefaultSensitivityAnalysisParameters(String userId, UserProfileInfos userProfileInfos) {
if (userProfileInfos != null && userProfileInfos.getSensitivityAnalysisParameterId() != null) {
// try to access/duplicate the user profile sensitivity analysis parameters
try {
return sensitivityAnalysisService.duplicateSensitivityAnalysisParameters(userProfileInfos.getSensitivityAnalysisParameterId());
} catch (Exception e) {
// TODO try to report a log in Root subreporter ?
LOGGER.error(String.format("Could not duplicate sensitivity analysis parameters with id '%s' from user/profile '%s/%s'. Using default parameters",
userProfileInfos.getSensitivityAnalysisParameterId(), userId, userProfileInfos.getName()), e);
}
}
// no profile, or no/bad sensitivity analysis parameters in profile => use default values
try {
return sensitivityAnalysisService.createDefaultSensitivityAnalysisParameters();
} catch (final Exception e) {
Expand All @@ -288,7 +320,18 @@ private UUID createDefaultSensitivityAnalysisParameters() {
}
}

private UUID createDefaultSecurityAnalysisParameters() {
private UUID createDefaultSecurityAnalysisParameters(String userId, UserProfileInfos userProfileInfos) {
if (userProfileInfos != null && userProfileInfos.getSecurityAnalysisParameterId() != null) {
// try to access/duplicate the user profile security analysis parameters
try {
return securityAnalysisService.duplicateSecurityAnalysisParameters(userProfileInfos.getSecurityAnalysisParameterId());
} catch (Exception e) {
// TODO try to report a log in Root subreporter ?
LOGGER.error(String.format("Could not duplicate security analysis parameters with id '%s' from user/profile '%s/%s'. Using default parameters",
userProfileInfos.getSecurityAnalysisParameterId(), userId, userProfileInfos.getName()), e);
}
}
// no profile, or no/bad security analysis parameters in profile => use default values
try {
return securityAnalysisService.createDefaultSecurityAnalysisParameters();
} catch (final Exception e) {
Expand All @@ -297,6 +340,26 @@ private UUID createDefaultSecurityAnalysisParameters() {
}
}

private UUID createDefaultVoltageInitParameters(String userId, UserProfileInfos userProfileInfos) {
if (userProfileInfos != null && userProfileInfos.getVoltageInitParameterId() != null) {
// try to access/duplicate the user profile voltage init parameters
try {
return voltageInitService.duplicateVoltageInitParameters(userProfileInfos.getVoltageInitParameterId());
} catch (Exception e) {
// TODO try to report a log in Root subreporter ?
LOGGER.error(String.format("Could not duplicate voltage init parameters with id '%s' from user/profile '%s/%s'. Using default parameters",
userProfileInfos.getVoltageInitParameterId(), userId, userProfileInfos.getName()), e);
}
}
// no profile, or no/bad voltage init parameters in profile => use default values
try {
return voltageInitService.createVoltageInitParameters(null);
} catch (final Exception e) {
LOGGER.error("Error while creating default parameters for voltage init", e);
return null;
}
}

@Bean
public Consumer<Message<String>> consumeCaseImportFailed() {
return message -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -257,8 +258,7 @@ public void assertSecurityAnalysisNotRunning(UUID resultUuid) {
}
}

public void updateSecurityAnalysisParameters(UUID parametersUuid, String parameters) {

public void updateSecurityAnalysisParameters(UUID parametersUuid, @Nullable String parameters) {
var uriBuilder = UriComponentsBuilder.fromPath(DELIMITER + SECURITY_ANALYSIS_API_VERSION + "/parameters/{uuid}");
String path = uriBuilder.buildAndExpand(parametersUuid).toUriString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.gridsuite.study.server.repository.StudyEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -339,10 +340,7 @@ public UUID duplicateSensitivityAnalysisParameters(UUID sourceParametersUuid) {
}
}

public void updateSensitivityAnalysisParameters(UUID parametersUuid, String parameters) {

Objects.requireNonNull(parameters);

public void updateSensitivityAnalysisParameters(UUID parametersUuid, @Nullable String parameters) {
var path = UriComponentsBuilder
.fromPath(DELIMITER + SENSITIVITY_ANALYSIS_API_VERSION + PARAMETERS_URI)
.buildAndExpand(parametersUuid)
Expand Down
Loading

0 comments on commit 0c22b05

Please sign in to comment.