diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityAnalysisResultRepository.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityAnalysisResultRepository.java index 9d2fe1d9..c2f10f31 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityAnalysisResultRepository.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityAnalysisResultRepository.java @@ -151,15 +151,14 @@ public SensitivityResultFilterOptions getSensitivityResultFilterOptions(UUID res } SensitivityResultFilterOptions.SensitivityResultFilterOptionsBuilder sensitivityResultOptionsBuilder = SensitivityResultFilterOptions.builder() - .allFunctionIds(sensitivityRepository.findFunctionByResultResultUuidAndFactorFunctionType(sas.getResultUuid(), selector.getFunctionType())) - .allVariableIds(sensitivityRepository.findVariableByResultResultUuidAndFactorFunctionType(sas.getResultUuid(), selector.getFunctionType())); + .allFunctionIds(sensitivityRepository.getDistinctFunctionIds(sas.getResultUuid(), selector.getFunctionType(), !selector.getIsJustBefore())) + .allVariableIds(sensitivityRepository.getDistinctVariableIds(sas.getResultUuid(), selector.getFunctionType(), !selector.getIsJustBefore())); if (!selector.getIsJustBefore()) { - sensitivityResultOptionsBuilder.allContingencyIds(sensitivityRepository.findContingencyByResultResultUuidAndFactorFunctionType(sas.getResultUuid(), selector.getFunctionType()) + sensitivityResultOptionsBuilder.allContingencyIds(sensitivityRepository.getDistinctContingencyIds(sas.getResultUuid(), selector.getFunctionType()) .stream() .filter(Objects::nonNull) - .collect(Collectors.toList())) - .build(); + .collect(Collectors.toList())); } return sensitivityResultOptionsBuilder.build(); diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityRepository.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityRepository.java index 29d756b3..23c726ff 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityRepository.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/repositories/SensitivityRepository.java @@ -38,14 +38,24 @@ public interface SensitivityRepository extends JpaRepository findAll(Specification specification, Pageable pageable); - @Query(value = "SELECT distinct s.factor.functionId from SensitivityEntity as s") - List findFunctionByResultResultUuidAndFactorFunctionType(UUID resultUuid, SensitivityFunctionType sensitivityFunctionType); + @Query(value = "SELECT distinct s.factor.functionId from SensitivityEntity as s " + + "where s.result.resultUuid = :resultUuid " + + "and s.factor.functionType = :sensitivityFunctionType " + + "and ((:withContingency = true and s.contingency is not null ) or (:withContingency = false and s.contingency is null )) " + + "order by s.factor.functionId") + List getDistinctFunctionIds(UUID resultUuid, SensitivityFunctionType sensitivityFunctionType, boolean withContingency); - @Query(value = "SELECT distinct s.factor.variableId from SensitivityEntity as s") - List findVariableByResultResultUuidAndFactorFunctionType(UUID resultUuid, SensitivityFunctionType sensitivityFunctionType); + @Query(value = "SELECT distinct s.factor.variableId from SensitivityEntity as s " + + "where s.result.resultUuid = :resultUuid " + + "and s.factor.functionType = :sensitivityFunctionType " + + "and ((:withContingency = true and s.contingency is not null ) or (:withContingency = false and s.contingency is null )) " + + "order by s.factor.variableId") + List getDistinctVariableIds(UUID resultUuid, SensitivityFunctionType sensitivityFunctionType, boolean withContingency); - @Query(value = "SELECT distinct s.contingency.contingencyId from SensitivityEntity as s") - List findContingencyByResultResultUuidAndFactorFunctionType(UUID resultUuid, SensitivityFunctionType sensitivityFunctionType); + @Query(value = "SELECT distinct s.contingency.contingencyId from SensitivityEntity as s " + + "where s.result.resultUuid = :resultUuid and s.factor.functionType = :sensitivityFunctionType " + + "order by s.contingency.contingencyId") + List getDistinctContingencyIds(UUID resultUuid, SensitivityFunctionType sensitivityFunctionType); static Specification getSpecification(AnalysisResultEntity sas, SensitivityFunctionType functionType, diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java index 96f8e7fa..850e35d6 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java @@ -629,7 +629,7 @@ public void runAndSaveTest() throws Exception { assertEquals(2, filterOptions.getAllVariableIds().size()); ResultsSelector filterOptionsSelector2 = ResultsSelector.builder().isJustBefore(true) - .functionType(SensitivityFunctionType.BRANCH_ACTIVE_POWER_2).build(); + .functionType(SensitivityFunctionType.BRANCH_CURRENT_1).build(); result = mockMvc.perform(get("/" + VERSION + "/results/{resultUuid}/filter-options?selector={selector}", RESULT_UUID, mapper.writeValueAsString(filterOptionsSelector2))) .andExpect(status().isOk()) @@ -637,8 +637,8 @@ public void runAndSaveTest() throws Exception { .andReturn(); SensitivityResultFilterOptions filterOptions2 = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() { }); assertNull(filterOptions2.getAllContingencyIds()); - assertEquals(3, filterOptions2.getAllFunctionIds().size()); - assertEquals(2, filterOptions2.getAllVariableIds().size()); + assertEquals(0, filterOptions2.getAllFunctionIds().size()); + assertEquals(0, filterOptions2.getAllVariableIds().size()); // check that a request for not present contingency does not crash and just brings nothing ResultsSelector selectorNKz1 = ResultsSelector.builder().isJustBefore(false)