diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java index f4f60ae3..daefe5a3 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java @@ -102,10 +102,11 @@ public ResponseEntity runAndSave(@Parameter(description = "Network UUID") @Operation(summary = "Get computation count of the total of identifiables for a list of filters") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis identifiables count"), @ApiResponse(responseCode = "404", description = "Sensitivity analysis identifiables count result has not been found")}) - public ResponseEntity getComputationCount(@Parameter(description = "Result UUID") @PathVariable("networkUuid") UUID networkUuid, + public ResponseEntity getComputationCount(@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().contentType(MediaType.APPLICATION_JSON).body(workerService.getComputationCount(ids, networkUuid, isInjectionsSet)); + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(workerService.getComputationCount(ids, networkUuid, variantId, isInjectionsSet)); } @GetMapping(value = "/results/{resultUuid}", produces = APPLICATION_JSON_VALUE) 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 8adb0221..f1e2771f 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisInputBuilderService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisInputBuilderService.java @@ -33,6 +33,7 @@ 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; @@ -114,15 +115,27 @@ private List goGetIdentifiables(EquipmentsContainer filt } } - private Integer getComputationCount(Map> ids, UUID networkUuid, Integer containersAttributesCount) { + private Integer getContingenciesCount(List ids, UUID networkUuid, String variantId) { + return ids.stream() + .mapToInt(uuid -> actionsService.getContingencyList(uuid, networkUuid, variantId).size()) + .sum(); + } + + private Integer getComputationCount(Map> ids, UUID networkUuid, String variantId, Integer containersAttributesCount) { + int contAttributesCountTemp = containersAttributesCount; + if (ids.containsKey(CONTINGENCIES)) { + int sumContingencyListSizes = Optional.of(getContingenciesCount(ids.get(CONTINGENCIES), networkUuid, variantId)).orElse(1); + contAttributesCountTemp *= sumContingencyListSizes; + ids.remove(CONTINGENCIES); + } Map> map = filterService.getIdentifiablesCount(ids, networkUuid, null); - Integer contAttributesCountTemp = containersAttributesCount; - for (String key : map.keySet()) { - int sensiFactorCount = map.get(key).stream().mapToInt(Integer::intValue).sum(); + for (List valueList : map.values()) { + int sensiFactorCount = valueList.stream().mapToInt(Integer::intValue).sum(); if (sensiFactorCount != 0) { contAttributesCountTemp *= sensiFactorCount; } } + return contAttributesCountTemp; } @@ -443,13 +456,13 @@ private void buildSensitivityNodes(SensitivityAnalysisRunContext context, Networ }); } - public Integer getComputationCount(Map> ids, UUID networkUuid, Boolean isInjectionsSet) { + public Integer getComputationCount(Map> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) { Integer containersAttributesCount = 1; if (Boolean.TRUE.equals(isInjectionsSet)) { containersAttributesCount *= ids.get(INJECTIONS).size(); ids.remove(INJECTIONS); } - containersAttributesCount = getComputationCount(ids, networkUuid, containersAttributesCount); + containersAttributesCount = getComputationCount(ids, networkUuid, variantId, containersAttributesCount); return containersAttributesCount; } 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 0462ca2a..4e14de9b 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java @@ -260,7 +260,7 @@ public Consumer> consumeCancel() { return message -> cancelSensitivityAnalysisAsync(SensitivityAnalysisCancelContext.fromMessage(message)); } - public Integer getComputationCount(Map> ids, UUID networkUuid, Boolean isInjectionsSet) { - return sensitivityAnalysisInputBuilderService.getComputationCount(ids, networkUuid, isInjectionsSet); + public Integer getComputationCount(Map> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) { + return sensitivityAnalysisInputBuilderService.getComputationCount(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 c73154bf..8393f94e 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java @@ -136,7 +136,11 @@ public class SensitivityAnalysisControllerTest { 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()); - private static final Map> IDS = Map.of("0", MONITORED_BRANCHES_FILTERS_UUID, "1", INJECTIONS_FILTERS_UUID, "2", CONTINGENCIES_FILTERS_UUID); + 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")), @@ -406,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); @@ -469,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, NETWORK_UUID, null)).willReturn(Map.of("0", List.of(6), "1", List.of(6), "2", List.of(6))); + given(filterService.getIdentifiablesCount(IDS_1, NETWORK_UUID, null)).willReturn(Map.of(MONITORED_BRANCHS_KEY, List.of(6), INJECTIONS_KEY, List.of(6))); // report service mocking doAnswer(i -> null).when(reportService).sendReport(any(), any()); @@ -749,11 +754,12 @@ public void testStatus() { @Test public void testGetComputationCount() throws Exception { - MvcResult result = mockMvc.perform(post("/" + VERSION + "/networks/{networkUuid}/computation-count", NETWORK_UUID) + MvcResult result = mockMvc.perform(post("/" + VERSION + "/networks/{networkUuid}/computation-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("216", result.getResponse().getContentAsString()); + assertEquals("72", result.getResponse().getContentAsString()); } @Test