From 697d34179c1163bf6a44ca3a24a3d0a2bfe16f41 Mon Sep 17 00:00:00 2001 From: Ghazoua Rehili Date: Thu, 14 Dec 2023 16:53:01 +0100 Subject: [PATCH] handle the get count filters ids (#54) add and handle the sensitivity analysis factors count Signed-off-by: Ghazwa REHILI ghazwa.rehili@rte-france.com --- .../server/SensitivityAnalysisController.java | 14 ++++++- .../server/service/FilterService.java | 28 +++++++++++++- ...ensitivityAnalysisInputBuilderService.java | 38 +++++++++++++++++++ .../SensitivityAnalysisWorkerService.java | 4 ++ .../SensitivityAnalysisControllerTest.java | 21 +++++++++- .../server/service/FilterServiceTest.java | 24 ++++++++++++ 6 files changed, 126 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java index c2082888..5330e643 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java @@ -31,6 +31,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; import java.util.UUID; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @@ -97,12 +98,23 @@ public ResponseEntity runAndSave(@Parameter(description = "Network UUID") return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid); } + @PostMapping(value = "/networks/{networkUuid}/factors-count", produces = APPLICATION_JSON_VALUE) + @Operation(summary = "Get factors count") + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis factors count"), + @ApiResponse(responseCode = "404", description = "Filters or contingencies has not been found")}) + public ResponseEntity getFactorsCount(@Parameter(description = "Network UUID") @PathVariable("networkUuid") UUID networkUuid, + @Parameter(description = "Variant Id") @RequestParam(name = "variantId", required = false) String variantId, + @Parameter(description = "Is Injections Set") @RequestParam(name = "isInjectionsSet", required = false) Boolean isInjectionsSet, + @RequestBody Map> ids) { + return ResponseEntity.ok().body(workerService.getFactorsCount(ids, networkUuid, variantId, isInjectionsSet)); + } + @GetMapping(value = "/results/{resultUuid}", produces = APPLICATION_JSON_VALUE) @Operation(summary = "Get a sensitivity analysis result from the database") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis result"), @ApiResponse(responseCode = "404", description = "Sensitivity analysis result has not been found")}) public ResponseEntity getResult(@Parameter(description = "Result UUID") - @PathVariable("resultUuid") UUID resultUuid, + @PathVariable("resultUuid") UUID resultUuid, @RequestParam(name = "selector", required = false) String selectorJson) { try { diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/FilterService.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/FilterService.java index a69b5284..c35bef1f 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/FilterService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/FilterService.java @@ -11,12 +11,16 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.UUID; @@ -30,6 +34,7 @@ public class FilterService { private static final String DELIMITER = "/"; private final RestTemplate restTemplate = new RestTemplate(); private String filterServerBaseUri; + public static final String NETWORK_UUID = "networkUuid"; private static final String QUERY_PARAM_VARIANT_ID = "variantId"; @Autowired @@ -43,7 +48,7 @@ public List getIdentifiablesFromFilter(UUID uuid, UUID n var uriComponentsBuilder = UriComponentsBuilder .fromPath(DELIMITER + FILTER_API_VERSION + "/filters/{id}/export") - .queryParam("networkUuid", networkUuid.toString()); + .queryParam(NETWORK_UUID, networkUuid.toString()); if (!StringUtils.isBlank(variantId)) { uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId); } @@ -53,4 +58,25 @@ public List getIdentifiablesFromFilter(UUID uuid, UUID n new ParameterizedTypeReference>() { }).getBody(); } + + public Map> getIdentifiablesCount(Map> ids, UUID networkUuid, String variantId) { + var uriComponentsBuilder = UriComponentsBuilder + .fromPath(DELIMITER + FILTER_API_VERSION + "/filters/identifiables-count") + .queryParam(NETWORK_UUID, networkUuid); + if (!StringUtils.isBlank(variantId)) { + uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId); + } + var path = uriComponentsBuilder + .build() + .toUriString(); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity>> httpEntity = new HttpEntity<>(ids, headers); + + return restTemplate.exchange(filterServerBaseUri + path, HttpMethod.POST, httpEntity, + new ParameterizedTypeReference>>() { + }).getBody(); + } } diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisInputBuilderService.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisInputBuilderService.java index daad5d19..465ba9e2 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisInputBuilderService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisInputBuilderService.java @@ -32,6 +32,8 @@ public class SensitivityAnalysisInputBuilderService { private static final String EXPECTED_TYPE = "expectedType"; private static final Logger LOGGER = LoggerFactory.getLogger(SensitivityAnalysisInputBuilderService.class); + public static final String INJECTIONS = "injections"; + public static final String CONTINGENCIES = "contingencies"; private final ActionsService actionsService; private final FilterService filterService; @@ -113,6 +115,32 @@ private List goGetIdentifiables(EquipmentsContainer filt } } + private Integer getContingenciesCount(List ids, UUID networkUuid, String variantId) { + return ids.stream() + .mapToInt(uuid -> actionsService.getContingencyList(uuid, networkUuid, variantId).size()) + .sum(); + } + + private Long getFactorsCount(Map> ids, UUID networkUuid, String variantId, Long containersAttributesCount) { + Long contAttributesCountTemp = containersAttributesCount; + if (ids.containsKey(CONTINGENCIES) && !ids.get(CONTINGENCIES).isEmpty()) { + int sumContingencyListSizes = getContingenciesCount(ids.get(CONTINGENCIES), networkUuid, variantId); + sumContingencyListSizes = Math.max(sumContingencyListSizes, 1); + contAttributesCountTemp *= sumContingencyListSizes; + ids.remove(CONTINGENCIES); + } + ids.entrySet().removeIf(entry -> Objects.isNull(entry.getValue())); + Map> map = filterService.getIdentifiablesCount(ids, networkUuid, null); + for (List valueList : map.values()) { + int sensiFactorCount = valueList.stream().mapToInt(Long::intValue).sum(); + if (sensiFactorCount != 0) { + contAttributesCountTemp *= sensiFactorCount; + } + } + + return contAttributesCountTemp; + } + private Stream getIdentifiablesFromContainer(SensitivityAnalysisRunContext context, EquipmentsContainer filter, List equipmentsTypesAllowed, Reporter reporter) { @@ -430,6 +458,16 @@ private void buildSensitivityNodes(SensitivityAnalysisRunContext context, Networ }); } + public Long getFactorsCount(Map> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) { + Long containersAttributesCount = 1L; + if (Boolean.TRUE.equals(isInjectionsSet)) { + containersAttributesCount *= ids.get(INJECTIONS).size(); + ids.remove(INJECTIONS); + } + containersAttributesCount = getFactorsCount(ids, networkUuid, variantId, containersAttributesCount); + return containersAttributesCount; + } + public void build(SensitivityAnalysisRunContext context, Network network, Reporter reporter) { try { buildSensitivityInjectionsSets(context, network, reporter); diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java index ffd9e069..d9a1b227 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java @@ -261,4 +261,8 @@ public Consumer> consumeRun() { public Consumer> consumeCancel() { return message -> cancelSensitivityAnalysisAsync(SensitivityAnalysisCancelContext.fromMessage(message)); } + + public Long getFactorsCount(Map> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) { + return sensitivityAnalysisInputBuilderService.getFactorsCount(ids, networkUuid, variantId, isInjectionsSet); + } } diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java index f66fef35..097b3673 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java @@ -133,6 +133,14 @@ public class SensitivityAnalysisControllerTest { private static final UUID MONITORED_VOLTAGE_LEVELS_FILTERS_NODES_UUID = UUID.randomUUID(); private static final UUID EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID = UUID.randomUUID(); private static final UUID CONTINGENCIES_NODES_UUID = UUID.randomUUID(); + private static final List MONITORED_BRANCHES_FILTERS_UUID = List.of(UUID.randomUUID()); + private static final List INJECTIONS_FILTERS_UUID = List.of(UUID.randomUUID()); + private static final List CONTINGENCIES_FILTERS_UUID = List.of(UUID.randomUUID()); + public static final String MONITORED_BRANCHS_KEY = "monitoredBranchs"; + public static final String INJECTIONS_KEY = "injections"; + public static final String CONTINGENCIES_KEY = "contingencies"; + private static final Map> IDS = Map.of(MONITORED_BRANCHS_KEY, MONITORED_BRANCHES_FILTERS_UUID, INJECTIONS_KEY, INJECTIONS_FILTERS_UUID, CONTINGENCIES_KEY, CONTINGENCIES_FILTERS_UUID); + private static final Map> IDS_1 = Map.of(MONITORED_BRANCHS_KEY, MONITORED_BRANCHES_FILTERS_UUID, INJECTIONS_KEY, INJECTIONS_FILTERS_UUID); private static final List CONTINGENCIES = List.of( new Contingency("l1", new BranchContingency("l1")), @@ -402,6 +410,7 @@ public void setUp() { given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_1_ID)).willReturn(CONTINGENCIES); given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_3_ID)).willReturn(CONTINGENCIES_VARIANT); given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_2_ID)).willReturn(CONTINGENCIES); + given(actionsService.getContingencyList(CONTINGENCIES_FILTERS_UUID.get(0), NETWORK_UUID, VARIANT_1_ID)).willReturn(CONTINGENCIES_VARIANT); // filter service mocking given(filterService.getIdentifiablesFromFilter(MONITORED_BRANCHES_FILTERS_INJECTIONS_SET_UUID, NETWORK_UUID, VARIANT_1_ID)).willReturn(BRANCHES); @@ -465,7 +474,7 @@ public void setUp() { given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_UUID, VARIANT_2_ID)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION); given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_UUID, null)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION); given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_STOP_UUID, VARIANT_2_ID)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION); - + given(filterService.getIdentifiablesCount(IDS_1, NETWORK_UUID, null)).willReturn(Map.>of(MONITORED_BRANCHS_KEY, List.of(6L), INJECTIONS_KEY, List.of(6L))); // report service mocking doAnswer(i -> null).when(reportService).sendReport(any(), any()); @@ -743,6 +752,16 @@ public void testStatus() { assertEquals(SensitivityAnalysisStatus.NOT_DONE.name(), result.getResponse().getContentAsString()); } + @Test + public void testGetFactorsCount() throws Exception { + MvcResult result = mockMvc.perform(post("/" + VERSION + "/networks/{networkUuid}/factors-count?variantId={variantId}", NETWORK_UUID, VARIANT_1_ID) + .contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(IDS))) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + assertEquals("72", result.getResponse().getContentAsString()); + } + @Test public void stopTest() throws Exception { mockMvc.perform(post( diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/service/FilterServiceTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/service/FilterServiceTest.java index 5d182bf0..51a66acd 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/service/FilterServiceTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/service/FilterServiceTest.java @@ -15,6 +15,7 @@ import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; import org.gridsuite.sensitivityanalysis.server.dto.IdentifiableAttributes; +import org.jetbrains.annotations.NotNull; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -27,6 +28,7 @@ import java.io.IOException; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.stream.Collectors; @@ -50,6 +52,8 @@ public class FilterServiceTest { private static final UUID LIST_UUID = UUID.randomUUID(); + private static final Map> IDENTIFIABLES_UUID = Map.of("0", List.of(LIST_UUID), "1", List.of(LIST_UUID), "2", List.of(LIST_UUID)); + private static final UUID VERY_LARGE_LIST_UUID = UUID.randomUUID(); private static final IdentifiableAttributes IDENTIFIABLE = new IdentifiableAttributes("gen1", IdentifiableType.GENERATOR, null); @@ -84,6 +88,7 @@ private String initMockWebServer() throws IOException { String jsonExpected = objectMapper.writeValueAsString(List.of(IDENTIFIABLE)); String veryLargeJsonExpected = objectMapper.writeValueAsString(createVeryLargeList()); String jsonVariantExpected = objectMapper.writeValueAsString(List.of(IDENTIFIABLE_VARIANT)); + String jsonIdentifiablesExpected = objectMapper.writeValueAsString(countResultMap()); final Dispatcher dispatcher = new Dispatcher() { @Override @@ -102,6 +107,11 @@ public MockResponse dispatch(RecordedRequest request) { return new MockResponse().setResponseCode(HttpStatus.OK.value()) .setBody(veryLargeJsonExpected) .addHeader("Content-Type", "application/json; charset=utf-8"); + } else if (requestPath.equals(String.format("/v1/filters/identifiables-count?networkUuid=%s&variantId=%s", NETWORK_UUID, VARIANT_ID)) + || requestPath.equals(String.format("/v1/filters/identifiables-count?networkUuid=%s", NETWORK_UUID))) { + return new MockResponse().setResponseCode(HttpStatus.OK.value()) + .setBody(jsonIdentifiablesExpected) + .addHeader("Content-Type", "application/json; charset=utf-8"); } else { return new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value()).setBody("Path not supported: " + request.getPath()); } @@ -115,6 +125,11 @@ public MockResponse dispatch(RecordedRequest request) { return baseHttpUrl.toString().substring(0, baseHttpUrl.toString().length() - 1); } + @NotNull + private static Map> countResultMap() { + return Map.of("0", List.of(6), "1", List.of(6), "2", List.of(6)); + } + private List createVeryLargeList() { return IntStream.range(0, DATA_BUFFER_LIMIT).mapToObj(i -> new IdentifiableAttributes("l" + i, IdentifiableType.GENERATOR, null)).collect(Collectors.toList()); } @@ -137,4 +152,13 @@ public void testVeryLargeList() { list = filterService.getIdentifiablesFromFilter(VERY_LARGE_LIST_UUID, UUID.fromString(NETWORK_UUID), VARIANT_ID); assertEquals(objectMapper.writeValueAsString(createVeryLargeList()), objectMapper.writeValueAsString(list)); } + + @SneakyThrows + @Test + public void testGetFactorsCount() { + Map> list = filterService.getIdentifiablesCount(IDENTIFIABLES_UUID, UUID.fromString(NETWORK_UUID), null); + assertEquals(objectMapper.writeValueAsString(countResultMap()), objectMapper.writeValueAsString(list)); + list = filterService.getIdentifiablesCount(IDENTIFIABLES_UUID, UUID.fromString(NETWORK_UUID), VARIANT_ID); + assertEquals(objectMapper.writeValueAsString(countResultMap()), objectMapper.writeValueAsString(list)); + } }