From 1b4cf0bfdb22a4bc58358db46c54ea751eb73bcb Mon Sep 17 00:00:00 2001 From: Slimane AMAR Date: Thu, 23 Nov 2023 10:05:59 +0100 Subject: [PATCH] Finalize report paging Signed-off-by: Slimane AMAR --- .../study/server/StudyConstants.java | 5 ---- .../study/server/StudyController.java | 15 +++++++----- .../study/server/service/ReportService.java | 24 +++++++++++++++---- .../study/server/service/StudyService.java | 22 +++++++++-------- .../server/service/ReportServiceTest.java | 2 +- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/gridsuite/study/server/StudyConstants.java b/src/main/java/org/gridsuite/study/server/StudyConstants.java index 905e8d3f6..53bbb345c 100644 --- a/src/main/java/org/gridsuite/study/server/StudyConstants.java +++ b/src/main/java/org/gridsuite/study/server/StudyConstants.java @@ -49,11 +49,6 @@ private StudyConstants() { public static final String REPORT_UUID = "reportUuid"; public static final String UUIDS = "uuids"; public static final String QUERY_PARAM_ERROR_ON_GROUP_NOT_FOUND = "errorOnGroupNotFound"; - public static final String QUERY_PARAM_ERROR_ON_REPORT_NOT_FOUND = "errorOnReportNotFound"; - public static final String QUERY_PARAM_REPORT_DEFAULT_NAME = "defaultName"; - public static final String QUERY_PARAM_REPORT_SEVERITY_LEVEL = "severityLevels"; - public static final String QUERY_PARAM_REPORT_WITH_ELEMENTS = "withElements"; - public static final String QUERY_PARAM_REPORT_TASKKEY_FILTER = "taskKeyFilter"; public static final String QUERY_PARAM_RECEIVER = "receiver"; public static final String HEADER_RECEIVER = "receiver"; public static final String HEADER_BUS_ID = "busId"; diff --git a/src/main/java/org/gridsuite/study/server/StudyController.java b/src/main/java/org/gridsuite/study/server/StudyController.java index 93c27ab53..3cedb0e98 100644 --- a/src/main/java/org/gridsuite/study/server/StudyController.java +++ b/src/main/java/org/gridsuite/study/server/StudyController.java @@ -1004,9 +1004,10 @@ public ResponseEntity stopSecurityAnalysis(@Parameter(description = "Study public ResponseEntity> getParentNodesReport(@Parameter(description = "Study uuid") @PathVariable("studyUuid") UUID studyUuid, @Parameter(description = "Node uuid") @PathVariable("nodeUuid") UUID nodeUuid, @Parameter(description = "Node only report") @RequestParam(value = "nodeOnlyReport", required = false, defaultValue = "true") boolean nodeOnlyReport, - @Parameter(description = "Severity levels") @RequestParam(name = "severityLevels", required = false) Set severityLevels) { + @Parameter(description = "Severity levels") @RequestParam(name = "severityLevels", required = false) Set severityLevels, + @Parameter(description = "Pagination parameters") Pageable pageable) { studyService.assertIsStudyAndNodeExist(studyUuid, nodeUuid); - return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getParentNodesReport(nodeUuid, nodeOnlyReport, severityLevels)); + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getParentNodesReport(nodeUuid, nodeOnlyReport, severityLevels, pageable)); } @GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/report", produces = MediaType.APPLICATION_JSON_VALUE) @@ -1015,9 +1016,10 @@ public ResponseEntity> getParentNodesReport(@Parameter(descr public ResponseEntity> getNodeReport(@Parameter(description = "Study uuid") @PathVariable("studyUuid") UUID studyUuid, @Parameter(description = "Node uuid") @PathVariable("nodeUuid") UUID nodeUuid, @Parameter(description = "The report Id") @RequestParam(name = "reportId", required = false) String reportId, - @Parameter(description = "Severity levels") @RequestParam(name = "severityLevels", required = false) Set severityLevels) { + @Parameter(description = "Severity levels") @RequestParam(name = "severityLevels", required = false) Set severityLevels, + @Parameter(description = "Pagination parameters") Pageable pageable) { studyService.assertIsStudyAndNodeExist(studyUuid, nodeUuid); - return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getNodeReport(nodeUuid, reportId, severityLevels)); + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getNodeReport(nodeUuid, reportId, severityLevels, pageable)); } @GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/subreport", produces = MediaType.APPLICATION_JSON_VALUE) @@ -1026,9 +1028,10 @@ public ResponseEntity> getNodeReport(@Parameter(description public ResponseEntity getSubReport(@Parameter(description = "Study uuid") @PathVariable("studyUuid") UUID studyUuid, @Parameter(description = "Node uuid") @PathVariable("nodeUuid") UUID nodeUuid, @Parameter(description = "The report Id") @RequestParam(name = "reportId") String reportId, - @Parameter(description = "Severity levels") @RequestParam(name = "severityLevels", required = false) Set severityLevels) { + @Parameter(description = "Severity levels") @RequestParam(name = "severityLevels", required = false) Set severityLevels, + @Parameter(description = "Pagination parameters") Pageable pageable) { studyService.assertIsStudyAndNodeExist(studyUuid, nodeUuid); - return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getSubReport(reportId, severityLevels)); + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getSubReport(reportId, severityLevels, pageable)); } @DeleteMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/report") diff --git a/src/main/java/org/gridsuite/study/server/service/ReportService.java b/src/main/java/org/gridsuite/study/server/service/ReportService.java index abcd55461..09d0fde89 100644 --- a/src/main/java/org/gridsuite/study/server/service/ReportService.java +++ b/src/main/java/org/gridsuite/study/server/service/ReportService.java @@ -14,6 +14,7 @@ import lombok.NonNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -37,6 +38,15 @@ public class ReportService { private static final String DELIMITER = "/"; + public static final String QUERY_PARAM_ERROR_ON_REPORT_NOT_FOUND = "errorOnReportNotFound"; + public static final String QUERY_PARAM_REPORT_DEFAULT_NAME = "defaultName"; + public static final String QUERY_PARAM_REPORT_SEVERITY_LEVEL = "severityLevels"; + public static final String QUERY_PARAM_REPORT_WITH_ELEMENTS = "withElements"; + public static final String QUERY_PARAM_REPORT_TASK_KEY_FILTER = "taskKeyFilter"; + public static final String QUERY_PARAM_REPORT_PAGE_NUMBER = "page"; + public static final String QUERY_PARAM_REPORT_PAGE_SIZE = "size"; + public static final String REPORTER_ID_VALUE_KEY = "id"; + private String reportServerBaseUri; @Autowired @@ -65,20 +75,24 @@ private String getSubReportsServerURI() { return this.reportServerBaseUri + DELIMITER + REPORT_API_VERSION + DELIMITER + "subreports" + DELIMITER; } - public ReporterModel getReport(@NonNull UUID id, @NonNull String defaultName, String taskKeyFilter, Set severityLevels) { + public ReporterModel getReport(@NonNull UUID id, @NonNull String defaultName, String taskKeyFilter, Set severityLevels, Pageable pageable) { var uriBuilder = UriComponentsBuilder.fromPath("{id}") .queryParam(QUERY_PARAM_REPORT_DEFAULT_NAME, defaultName) .queryParam(QUERY_PARAM_REPORT_WITH_ELEMENTS, true) - .queryParam(QUERY_PARAM_REPORT_SEVERITY_LEVEL, severityLevels); + .queryParam(QUERY_PARAM_REPORT_SEVERITY_LEVEL, severityLevels) + .queryParam(QUERY_PARAM_REPORT_PAGE_NUMBER, pageable.getPageNumber()) + .queryParam(QUERY_PARAM_REPORT_PAGE_SIZE, pageable.getPageSize()); if (taskKeyFilter != null && !taskKeyFilter.isEmpty()) { - uriBuilder.queryParam(QUERY_PARAM_REPORT_TASKKEY_FILTER, taskKeyFilter); + uriBuilder.queryParam(QUERY_PARAM_REPORT_TASK_KEY_FILTER, taskKeyFilter); } return reportServerCall(id, this.getReportsServerURI(), uriBuilder); } - public ReporterModel getSubReport(@NonNull UUID id, Set severityLevels) { + public ReporterModel getSubReport(@NonNull UUID id, Set severityLevels, Pageable pageable) { var uriBuilder = UriComponentsBuilder.fromPath("{id}") - .queryParam(QUERY_PARAM_REPORT_SEVERITY_LEVEL, severityLevels); + .queryParam(QUERY_PARAM_REPORT_SEVERITY_LEVEL, severityLevels) + .queryParam(QUERY_PARAM_REPORT_PAGE_NUMBER, pageable.getPageNumber()) + .queryParam(QUERY_PARAM_REPORT_PAGE_SIZE, pageable.getPageSize()); return reportServerCall(id, this.getSubReportsServerURI(), uriBuilder); } diff --git a/src/main/java/org/gridsuite/study/server/service/StudyService.java b/src/main/java/org/gridsuite/study/server/service/StudyService.java index a07ab1f82..8ddac4dae 100644 --- a/src/main/java/org/gridsuite/study/server/service/StudyService.java +++ b/src/main/java/org/gridsuite/study/server/service/StudyService.java @@ -51,6 +51,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.client.elc.QueryBuilders; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; @@ -71,6 +72,7 @@ import static org.gridsuite.study.server.StudyException.Type.*; import static org.gridsuite.study.server.elasticsearch.EquipmentInfosService.EQUIPMENT_TYPE_SCORES; import static org.gridsuite.study.server.service.NetworkModificationTreeService.ROOT_NODE_NAME; +import static org.gridsuite.study.server.service.ReportService.REPORTER_ID_VALUE_KEY; import static org.gridsuite.study.server.utils.StudyUtils.handleHttpError; /** @@ -1747,23 +1749,23 @@ private void checkStudyContainsNode(UUID studyUuid, UUID nodeUuid) { } @Transactional(readOnly = true) - public ReporterModel getSubReport(String subReportId, Set severityLevels) { - return reportService.getSubReport(UUID.fromString(subReportId), severityLevels); + public ReporterModel getSubReport(String subReportId, Set severityLevels, Pageable pageable) { + return reportService.getSubReport(UUID.fromString(subReportId), severityLevels, pageable); } @Transactional(readOnly = true) - public List getNodeReport(UUID nodeUuid, String reportId, Set severityLevels) { + public List getNodeReport(UUID nodeUuid, String reportId, Set severityLevels, Pageable pageable) { // Hack: filtering Root node with its nodeId in report db does not work // TODO : Remove this hack when the taskKey of the root node will be replaced by the node uuid AbstractNode nodeInfos = networkModificationTreeService.getNode(nodeUuid); String taskKeyFilter = nodeInfos.getType() == NodeType.ROOT ? null : nodeUuid.toString(); - return getSubReporters(nodeUuid, UUID.fromString(reportId), severityLevels, taskKeyFilter); + return getSubReporters(nodeUuid, UUID.fromString(reportId), severityLevels, taskKeyFilter, pageable); } @Transactional(readOnly = true) - public List getParentNodesReport(UUID nodeUuid, boolean nodeOnlyReport, Set severityLevels) { + public List getParentNodesReport(UUID nodeUuid, boolean nodeOnlyReport, Set severityLevels, Pageable pageable) { AbstractNode nodeInfos = networkModificationTreeService.getNode(nodeUuid); - List subReporters = getSubReporters(nodeUuid, nodeInfos.getReportUuid(), severityLevels, null); + List subReporters = getSubReporters(nodeUuid, nodeInfos.getReportUuid(), severityLevels, null, pageable); if (subReporters.isEmpty()) { return subReporters; } else if (nodeOnlyReport) { @@ -1776,13 +1778,13 @@ public List getParentNodesReport(UUID nodeUuid, boolean nodeOnlyR if (parentUuid.isEmpty()) { return subReporters; } - List parentReporters = self.getParentNodesReport(parentUuid.get(), false, severityLevels); + List parentReporters = self.getParentNodesReport(parentUuid.get(), false, severityLevels, pageable); return Stream.concat(parentReporters.stream(), subReporters.stream()).collect(Collectors.toList()); } } - private List getSubReporters(UUID nodeUuid, UUID reportUuid, Set severityLevels, String taskKeyfilter) { - ReporterModel reporter = reportService.getReport(reportUuid, nodeUuid.toString(), taskKeyfilter, severityLevels); + private List getSubReporters(UUID nodeUuid, UUID reportUuid, Set severityLevels, String taskKeyfilter, Pageable pageable) { + ReporterModel reporter = reportService.getReport(reportUuid, nodeUuid.toString(), taskKeyfilter, severityLevels, pageable); Map> subReportersByNode = new LinkedHashMap<>(); reporter.getSubReporters().forEach(subReporter -> subReportersByNode.putIfAbsent(getNodeIdFromReportKey(subReporter), new ArrayList<>())); reporter.getSubReporters().forEach(subReporter -> @@ -1790,7 +1792,7 @@ private List getSubReporters(UUID nodeUuid, UUID reportUuid, Set< ); return subReportersByNode.keySet().stream().map(nodeId -> { // For a node report, pass the reportId to the Front as taskValues, to allow direct access - Map taskValues = Map.of("id", new TypedValue(reportUuid.toString(), "ID")); + Map taskValues = Map.of(REPORTER_ID_VALUE_KEY, new TypedValue(reportUuid.toString(), TypedValue.UNTYPED)); ReporterModel newSubReporter = new ReporterModel(nodeId, nodeId, taskValues); subReportersByNode.get(nodeId).forEach(newSubReporter::addSubReporter); return newSubReporter; diff --git a/src/test/java/org/gridsuite/study/server/service/ReportServiceTest.java b/src/test/java/org/gridsuite/study/server/service/ReportServiceTest.java index 94fb17bf1..e85b6bcfa 100644 --- a/src/test/java/org/gridsuite/study/server/service/ReportServiceTest.java +++ b/src/test/java/org/gridsuite/study/server/service/ReportServiceTest.java @@ -49,7 +49,7 @@ import java.util.*; import java.util.stream.Stream; -import static org.gridsuite.study.server.StudyConstants.QUERY_PARAM_REPORT_DEFAULT_NAME; +import static org.gridsuite.study.server.service.ReportService.QUERY_PARAM_REPORT_DEFAULT_NAME; import static org.gridsuite.study.server.utils.TestUtils.createModificationNodeInfo; import static org.junit.Assert.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;