Skip to content

Commit

Permalink
remove previous sensi logs when run starts (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: David BRAQUART <[email protected]>
  • Loading branch information
dbraquart authored Nov 23, 2023
1 parent 6fc472f commit e6a950c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public ResponseEntity<SensitivityAnalysisResult> 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);
}

Expand All @@ -90,8 +91,9 @@ public ResponseEntity<UUID> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,8 +71,9 @@ public static SensitivityAnalysisResultContext fromMessage(Message<String> 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);
}

Expand All @@ -89,6 +92,7 @@ public Message<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -76,4 +79,8 @@ public UUID getReportUuid() {
public String getReporterId() {
return reporterId;
}

public String getReportType() {
return reportType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SensitivityAnalysisResult> future = runSensitivityAnalysisAsync(context, sensitivityAnalysisRunner, reporter, resultUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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());
Expand All @@ -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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Report> reports;
reports = reporter.getReports();
Expand Down Expand Up @@ -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<Report> reports = reporter.getReports();
assertThat(reports, not(nullValue()));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit e6a950c

Please sign in to comment.