From e6a950c7522d67110766f3472c9d7c4016cb4202 Mon Sep 17 00:00:00 2001 From: dbraquart <107846716+dbraquart@users.noreply.github.com> Date: Thu, 23 Nov 2023 12:46:15 +0100 Subject: [PATCH] remove previous sensi logs when run starts (#48) Signed-off-by: David BRAQUART --- .../server/SensitivityAnalysisController.java | 6 ++++-- .../server/service/ReportService.java | 15 +++++++++++++++ .../SensitivityAnalysisResultContext.java | 6 +++++- .../service/SensitivityAnalysisRunContext.java | 9 ++++++++- .../SensitivityAnalysisWorkerService.java | 9 +++++---- .../SensitivityAnalysisControllerTest.java | 16 ++++++++-------- .../dto/SensitivityAnalysisInputDataTest.java | 6 +++--- .../server/service/ReportServiceTest.java | 11 ++++++++++- .../service/SensitivityAnalysisServiceTest.java | 8 ++++---- 9 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java index facddd92..c2082888 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisController.java @@ -73,8 +73,9 @@ public ResponseEntity run(@Parameter(description = "N @Parameter(description = "Provider") @RequestParam(name = "provider", required = false) String provider, @Parameter(description = "reportUuid") @RequestParam(name = "reportUuid", required = false) UUID reportUuid, @Parameter(description = "reporterId") @RequestParam(name = "reporterId", required = false) String reporterId, + @Parameter(description = "The type name for the report") @RequestParam(name = "reportType", required = false, defaultValue = "SensitivityAnalysis") String reportType, @RequestBody SensitivityAnalysisInputData sensitivityAnalysisInputData) { - SensitivityAnalysisResult result = workerService.run(new SensitivityAnalysisRunContext(networkUuid, variantId, sensitivityAnalysisInputData, null, provider, reportUuid, reporterId)); + SensitivityAnalysisResult result = workerService.run(new SensitivityAnalysisRunContext(networkUuid, variantId, sensitivityAnalysisInputData, null, provider, reportUuid, reporterId, reportType)); return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result); } @@ -90,8 +91,9 @@ public ResponseEntity runAndSave(@Parameter(description = "Network UUID") @Parameter(description = "Provider") @RequestParam(name = "provider", required = false) String provider, @Parameter(description = "reportUuid") @RequestParam(name = "reportUuid", required = false) UUID reportUuid, @Parameter(description = "reporterId") @RequestParam(name = "reporterId", required = false) String reporterId, + @Parameter(description = "The type name for the report") @RequestParam(name = "reportType", required = false, defaultValue = "SensitivityAnalysis") String reportType, @RequestBody SensitivityAnalysisInputData sensitivityAnalysisInputData) { - UUID resultUuid = service.runAndSaveResult(new SensitivityAnalysisRunContext(networkUuid, variantId, sensitivityAnalysisInputData, receiver, provider, reportUuid, reporterId)); + UUID resultUuid = service.runAndSaveResult(new SensitivityAnalysisRunContext(networkUuid, variantId, sensitivityAnalysisInputData, receiver, provider, reportUuid, reporterId, reportType)); return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid); } diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/ReportService.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/ReportService.java index 2b0a5166..eb1292bc 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/ReportService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/ReportService.java @@ -32,6 +32,8 @@ public class ReportService { static final String REPORT_API_VERSION = "v1"; private static final String DELIMITER = "/"; + private static final String QUERY_PARAM_REPORT_TYPE_FILTER = "reportTypeFilter"; + private static final String QUERY_PARAM_REPORT_THROW_ERROR = "errorOnReportNotFound"; private String reportServerBaseUri; @Autowired @@ -71,4 +73,17 @@ public void sendReport(UUID reportUuid, Reporter reporter) { throw new PowsyblException("Error sending report", error); } } + + public void deleteReport(UUID reportUuid, String reportType) { + Objects.requireNonNull(reportUuid); + + var path = UriComponentsBuilder.fromPath("{reportUuid}") + .queryParam(QUERY_PARAM_REPORT_TYPE_FILTER, reportType) + .queryParam(QUERY_PARAM_REPORT_THROW_ERROR, false) + .buildAndExpand(reportUuid) + .toUriString(); + var headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + restTemplate.exchange(getReportServerURI() + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class); + } } diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisResultContext.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisResultContext.java index 9d9efc1a..cd0aafe1 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisResultContext.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisResultContext.java @@ -27,6 +27,8 @@ public class SensitivityAnalysisResultContext { public static final String REPORTER_ID_HEADER = "reporterId"; + public static final String REPORT_TYPE_HEADER = "reportType"; + private final UUID resultUuid; private final SensitivityAnalysisRunContext runContext; @@ -69,8 +71,9 @@ public static SensitivityAnalysisResultContext fromMessage(Message messa } UUID reportUuid = headers.containsKey(REPORT_UUID) ? UUID.fromString((String) headers.get(REPORT_UUID)) : null; String reporterId = headers.containsKey(REPORTER_ID_HEADER) ? (String) headers.get(REPORTER_ID_HEADER) : null; + String reportType = headers.containsKey(REPORT_TYPE_HEADER) ? (String) headers.get(REPORT_TYPE_HEADER) : null; SensitivityAnalysisRunContext runContext = new SensitivityAnalysisRunContext(networkUuid, - variantId, sensitivityAnalysisInputData, receiver, provider, reportUuid, reporterId); + variantId, sensitivityAnalysisInputData, receiver, provider, reportUuid, reporterId, reportType); return new SensitivityAnalysisResultContext(resultUuid, runContext); } @@ -89,6 +92,7 @@ public Message toMessage(ObjectMapper objectMapper) { .setHeader("provider", runContext.getProvider()) .setHeader(REPORT_UUID, runContext.getReportUuid()) .setHeader(REPORTER_ID_HEADER, runContext.getReporterId()) + .setHeader(REPORT_TYPE_HEADER, runContext.getReportType()) .build(); } } diff --git a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisRunContext.java b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisRunContext.java index 7766a1da..6d5ba298 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisRunContext.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisRunContext.java @@ -32,9 +32,11 @@ public class SensitivityAnalysisRunContext { private final String reporterId; + private final String reportType; + public SensitivityAnalysisRunContext(UUID networkUuid, String variantId, SensitivityAnalysisInputData sensitivityAnalysisInputData, - String receiver, String provider, UUID reportUuid, String reporterId) { + String receiver, String provider, UUID reportUuid, String reporterId, String reportType) { this.networkUuid = Objects.requireNonNull(networkUuid); this.variantId = variantId; this.sensitivityAnalysisInputData = Objects.requireNonNull(sensitivityAnalysisInputData); @@ -43,6 +45,7 @@ public SensitivityAnalysisRunContext(UUID networkUuid, String variantId, this.provider = provider; this.reportUuid = reportUuid; this.reporterId = reporterId; + this.reportType = reportType; } public UUID getNetworkUuid() { @@ -76,4 +79,8 @@ public UUID getReportUuid() { public String getReporterId() { return reporterId; } + + public String getReportType() { + return reportType; + } } 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 9cefe89a..dc00c4b1 100644 --- a/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java +++ b/src/main/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisWorkerService.java @@ -51,8 +51,6 @@ public class SensitivityAnalysisWorkerService { private static final Logger LOGGER = LoggerFactory.getLogger(SensitivityAnalysisWorkerService.class); - private static final String SENSI_TYPE_REPORT = "SensitivityAnalysis"; - private final NetworkStoreService networkStoreService; private final ReportService reportService; @@ -130,9 +128,12 @@ private SensitivityAnalysisResult run(SensitivityAnalysisRunContext context, UUI Reporter rootReporter = Reporter.NO_OP; Reporter reporter = Reporter.NO_OP; if (context.getReportUuid() != null) { - String rootReporterId = context.getReporterId() == null ? SENSI_TYPE_REPORT : context.getReporterId() + "@" + SENSI_TYPE_REPORT; + final String reportType = context.getReportType(); + String rootReporterId = context.getReporterId() == null ? reportType : context.getReporterId() + "@" + reportType; rootReporter = new ReporterModel(rootReporterId, rootReporterId); - reporter = rootReporter.createSubReporter(SENSI_TYPE_REPORT, SENSI_TYPE_REPORT + " (${providerToUse})", "providerToUse", sensitivityAnalysisRunner.getName()); + reporter = rootReporter.createSubReporter(reportType, reportType + " (${providerToUse})", "providerToUse", sensitivityAnalysisRunner.getName()); + // Delete any previous sensi computation logs + reportService.deleteReport(context.getReportUuid(), reportType); } CompletableFuture future = runSensitivityAnalysisAsync(context, sensitivityAnalysisRunner, reporter, resultUuid); diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java index d487e922..f66fef35 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/SensitivityAnalysisControllerTest.java @@ -509,7 +509,7 @@ public void runTest() throws Exception { // run with specific variant MvcResult result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run?variantId=" + VARIANT_3_ID, NETWORK_UUID) + "/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis&variantId=" + VARIANT_3_ID, NETWORK_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_1)) .andExpect(status().isOk()) @@ -520,7 +520,7 @@ public void runTest() throws Exception { // run with implicit initial variant for (String sensitivityInput : List.of(SENSITIVITY_INPUT_1, SENSITIVITY_INPUT_2, SENSITIVITY_INPUT_3, SENSITIVITY_INPUT_4, SENSITIVITY_INPUT_5, SENSITIVITY_INPUT_6, SENSITIVITY_INPUT_LOAD_PROPORTIONAL_MAXP, SENSITIVITY_INPUT_VENTILATION)) { result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run", NETWORK_UUID) + "/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis", NETWORK_UUID) .contentType(MediaType.APPLICATION_JSON) .content(sensitivityInput)) .andExpect(status().isOk()) @@ -531,7 +531,7 @@ public void runTest() throws Exception { // run with OpenLoadFlow provider and sensitivityType DELTA_A for HVDC result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run?provider=OpenLoadFlow", NETWORK_UUID) + "/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis&provider=OpenLoadFlow", NETWORK_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_HVDC_DELTA_A)) .andExpect(status().isOk()) @@ -543,7 +543,7 @@ public void runTest() throws Exception { @Test public void runAndSaveTest() throws Exception { MvcResult result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID) + "/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_1)) .andExpect(status().isOk()) @@ -706,7 +706,7 @@ public void runAndSaveTest() throws Exception { @Test public void deleteResultsTest() { MvcResult result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run-and-save", NETWORK_UUID) + "/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis", NETWORK_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_1)) .andExpect(status().isOk()) @@ -746,7 +746,7 @@ public void testStatus() { @Test public void stopTest() throws Exception { mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_STOP_UUID) + "/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_STOP_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_1)) .andExpect(status().isOk()); @@ -766,7 +766,7 @@ public void stopTest() throws Exception { @Test public void runTestWithError() { MvcResult result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_1_ID, NETWORK_ERROR_UUID) + "/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis&receiver=me&variantId=" + VARIANT_1_ID, NETWORK_ERROR_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_1)) .andExpect(status().isOk()) @@ -789,7 +789,7 @@ public void runTestWithError() { @Test public void runWithReportTest() { MvcResult result = mockMvc.perform(post( - "/" + VERSION + "/networks/{networkUuid}/run?reportUuid=" + REPORT_UUID + "&reporterId=" + UUID.randomUUID(), NETWORK_UUID) + "/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis&reportUuid=" + REPORT_UUID + "&reporterId=" + UUID.randomUUID(), NETWORK_UUID) .contentType(MediaType.APPLICATION_JSON) .content(SENSITIVITY_INPUT_1)) .andExpect(status().isOk()) diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/dto/SensitivityAnalysisInputDataTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/dto/SensitivityAnalysisInputDataTest.java index 5f3c8a72..90eed101 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/dto/SensitivityAnalysisInputDataTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/dto/SensitivityAnalysisInputDataTest.java @@ -127,7 +127,7 @@ public void testEmptyInputTranslation() { .build(); ReporterModel reporter = new ReporterModel("a", "b"); SensitivityAnalysisRunContext context; - context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, null, null, null); + context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, null, null, null, null); inputBuilderService.build(context, NETWORK, reporter); Collection reports; reports = reporter.getReports(); @@ -157,7 +157,7 @@ public void testFilterPbInputTranslation() { .contingencies(List.of(new EquipmentsContainer(UUID.randomUUID(), "u10"), new EquipmentsContainer(UUID.randomUUID(), "u11"))) .build())) .build(); - context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, null, null, null); + context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, null, null, null, null); inputBuilderService.build(context, NETWORK, reporter); Collection reports = reporter.getReports(); assertThat(reports, not(nullValue())); @@ -177,7 +177,7 @@ public void testFilterWiderPbInputTranslation() { SensitivityAnalysisInputData inputData = inputBuilder .build(); - context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, null, null, null); + context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, null, null, null, null); final ReporterModel reporter = new ReporterModel("a", "b"); var thrown = assertThrows(NullPointerException.class, () -> inputBuilderService.build(context, NETWORK, reporter)); assertThat(thrown, Matchers.instanceOf(NullPointerException.class)); diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/service/ReportServiceTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/service/ReportServiceTest.java index 9d1c09b7..949d87fd 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/service/ReportServiceTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/service/ReportServiceTest.java @@ -74,6 +74,9 @@ public MockResponse dispatch(RecordedRequest request) { if (requestPath.equals(String.format("/v1/reports/%s", REPORT_UUID))) { assertEquals(REPORT_JSON, request.getBody().readUtf8()); return new MockResponse().setResponseCode(HttpStatus.OK.value()); + } else if (requestPath.equals(String.format("/v1/reports/%s?reportTypeFilter=SensitivityAnalysis&errorOnReportNotFound=false", REPORT_UUID))) { + assertEquals("", request.getBody().readUtf8()); + return new MockResponse().setResponseCode(HttpStatus.OK.value()); } else if (requestPath.equals(String.format("/v1/reports/%s", REPORT_ERROR_UUID))) { return new MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value()); } else { @@ -90,9 +93,15 @@ public MockResponse dispatch(RecordedRequest request) { } @Test - public void test() { + public void testSendReport() { Reporter reporter = new ReporterModel("test", "a test"); reportService.sendReport(REPORT_UUID, reporter); assertThrows(RestClientException.class, () -> reportService.sendReport(REPORT_ERROR_UUID, reporter)); } + + @Test + public void testDeleteReport() { + reportService.deleteReport(REPORT_UUID, "SensitivityAnalysis"); + assertThrows(RestClientException.class, () -> reportService.deleteReport(REPORT_ERROR_UUID, "SensitivityAnalysis")); + } } diff --git a/src/test/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisServiceTest.java b/src/test/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisServiceTest.java index 59923388..0897d5db 100644 --- a/src/test/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisServiceTest.java +++ b/src/test/java/org/gridsuite/sensitivityanalysis/server/service/SensitivityAnalysisServiceTest.java @@ -141,7 +141,7 @@ private static SensitivityAnalysisInputData getDummyInputData() { @Test public void test0() { SensitivityAnalysisRunContext context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, - getDummyInputData(), null, null, null, null); + getDummyInputData(), null, null, null, null, null); testBasic(true, context); testBasic(false, context); } @@ -158,7 +158,7 @@ public void testWithLFParams() { .loadFlowSpecificParameters(null) .build(); SensitivityAnalysisRunContext context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, - inputData, null, "OpenLoadFlow", null, null); + inputData, null, "OpenLoadFlow", null, null, null); testBasic(true, context); // with non-null LF params @@ -306,7 +306,7 @@ public void testNoNKStillOK() { UUID gottenResultUuid = analysisService.runAndSaveResult( new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, - getDummyInputData(), null, null, null, null)); + getDummyInputData(), null, null, null, null, null)); assertThat(gottenResultUuid, not(nullValue())); assertThat(gottenResultUuid, is(resultUuid)); @@ -378,7 +378,7 @@ private void testNoN(boolean specific) { UUID gottenResultUuid = analysisService.runAndSaveResult( new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, - getDummyInputData(), null, null, null, null)); + getDummyInputData(), null, null, null, null, null)); assertThat(gottenResultUuid, not(nullValue())); assertThat(gottenResultUuid, is(resultUuid));