Skip to content

Commit c32010b

Browse files
authored
remove previous courcirc logs when run starts (#53)
Signed-off-by: David BRAQUART <[email protected]>
1 parent 0c0ff9f commit c32010b

File tree

10 files changed

+60
-21
lines changed

10 files changed

+60
-21
lines changed

src/main/java/org/gridsuite/shortcircuit/server/ShortCircuitController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ public ResponseEntity<UUID> runAndSave(@Parameter(description = "Network UUID")
5959
@Parameter(description = "Result receiver") @RequestParam(name = "receiver", required = false) String receiver,
6060
@Parameter(description = "reportUuid") @RequestParam(name = "reportUuid", required = false) UUID reportUuid,
6161
@Parameter(description = "reporterId") @RequestParam(name = "reporterId", required = false) String reporterId,
62+
@Parameter(description = "The type name for the report") @RequestParam(name = "reportType", required = false) String reportType,
6263
@Parameter(description = "Bus Id - Used for analysis targeting one bus") @RequestParam(name = "busId", required = false) String busId,
6364
@RequestBody(required = false) ShortCircuitParameters parameters,
6465
@RequestHeader(HEADER_USER_ID) String userId) {
6566
ShortCircuitParameters nonNullParameters = getNonNullParameters(parameters);
66-
UUID resultUuid = shortCircuitService.runAndSaveResult(new ShortCircuitRunContext(networkUuid, variantId, receiver, nonNullParameters, reportUuid, reporterId, userId, busId));
67+
UUID resultUuid = shortCircuitService.runAndSaveResult(new ShortCircuitRunContext(networkUuid, variantId, receiver, nonNullParameters, reportUuid, reporterId, reportType, userId, busId));
6768
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid);
6869
}
6970

src/main/java/org/gridsuite/shortcircuit/server/reports/AbstractReportMapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public abstract class AbstractReportMapper {
4444
* @implNote currently support only some implementations of {@link Reporter}
4545
*/
4646
public Reporter processReporter(@NonNull final Reporter reporter) {
47-
if (reporter instanceof ReporterModel reporterModel && reporterModel.getTaskKey().matches("^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}@)?ShortCircuitAnalysis$")) {
47+
if (reporter instanceof ReporterModel reporterModel && reporterModel.getTaskKey()
48+
.matches("^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}@)?.*ShortCircuitAnalysis$")) {
4849
log.debug("ShortCircuitAnalysis root node found, will modify it!");
4950
return forUuidAtShortCircuitAnalysis(reporterModel);
5051
} else {
@@ -62,7 +63,7 @@ protected Reporter forUuidAtShortCircuitAnalysis(@NonNull final ReporterModel re
6263
final ReporterModel newReporter = new ReporterModel(reporterModel.getTaskKey(), reporterModel.getDefaultName(), reporterModel.getTaskValues());
6364
reporterModel.getReports().forEach(newReporter::report);
6465
reporterModel.getSubReporters().forEach(reporter -> {
65-
if ("ShortCircuitAnalysis".equals(reporter.getTaskKey())) {
66+
if (reporter.getTaskKey() != null && reporter.getTaskKey().endsWith("ShortCircuitAnalysis")) {
6667
newReporter.addSubReporter(forShortCircuitAnalysis(reporter));
6768
} else {
6869
newReporter.addSubReporter(reporter);

src/main/java/org/gridsuite/shortcircuit/server/reports/ReportMapperCourcirc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected Reporter forUuidAtShortCircuitAnalysis(@NonNull final ReporterModel re
4747
final ReporterModel newReporter = new ReporterModel(reporterModel.getTaskKey(), reporterModel.getDefaultName(), reporterModel.getTaskValues());
4848
reporterModel.getReports().forEach(newReporter::report);
4949
reporterModel.getSubReporters().forEach(reporter -> {
50-
if ("ShortCircuitAnalysis".equals(reporter.getTaskKey())
50+
if (reporter.getTaskKey() != null && reporter.getTaskKey().endsWith("ShortCircuitAnalysis")
5151
&& "Courcirc".equals(reporter.getTaskValues().getOrDefault("providerToUse", new TypedValue("", "")).getValue())) {
5252
newReporter.addSubReporter(forShortCircuitAnalysis(reporter));
5353
} else {

src/main/java/org/gridsuite/shortcircuit/server/service/ReportService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class ReportService {
3232

3333
static final String REPORT_API_VERSION = "v1";
3434
private static final String DELIMITER = "/";
35+
private static final String QUERY_PARAM_REPORT_TYPE_FILTER = "reportTypeFilter";
36+
private static final String QUERY_PARAM_REPORT_THROW_ERROR = "errorOnReportNotFound";
3537
private String reportServerBaseUri;
3638

3739
@Autowired
@@ -71,4 +73,17 @@ public void sendReport(UUID reportUuid, Reporter reporter) {
7173
throw new PowsyblException("Error sending report", error);
7274
}
7375
}
76+
77+
public void deleteReport(UUID reportUuid, String reportType) {
78+
Objects.requireNonNull(reportUuid);
79+
80+
var path = UriComponentsBuilder.fromPath("{reportUuid}")
81+
.queryParam(QUERY_PARAM_REPORT_TYPE_FILTER, reportType)
82+
.queryParam(QUERY_PARAM_REPORT_THROW_ERROR, false)
83+
.buildAndExpand(reportUuid)
84+
.toUriString();
85+
var headers = new HttpHeaders();
86+
headers.setContentType(MediaType.APPLICATION_JSON);
87+
restTemplate.exchange(getReportServerURI() + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class);
88+
}
7489
}

src/main/java/org/gridsuite/shortcircuit/server/service/ShortCircuitResultContext.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ShortCircuitResultContext {
3131
public static final String VARIANT_ID_HEADER = "variantId";
3232

3333
public static final String REPORTER_ID_HEADER = "reporterId";
34+
public static final String REPORT_TYPE_HEADER = "reporterType";
3435

3536
private static final String MESSAGE_ROOT_NAME = "parameters";
3637

@@ -71,9 +72,9 @@ public static ShortCircuitResultContext fromMessage(Message<String> message, Obj
7172
}
7273
UUID reportUuid = headers.containsKey(REPORT_UUID_HEADER) ? UUID.fromString((String) headers.get(REPORT_UUID_HEADER)) : null;
7374
String reporterId = headers.containsKey(REPORTER_ID_HEADER) ? (String) headers.get(REPORTER_ID_HEADER) : null;
74-
ShortCircuitRunContext runContext = new ShortCircuitRunContext(networkUuid,
75-
variantId, receiver,
76-
parameters, reportUuid, reporterId, userId, busId);
75+
String reportType = headers.containsKey(REPORT_TYPE_HEADER) ? (String) headers.get(REPORT_TYPE_HEADER) : null;
76+
ShortCircuitRunContext runContext = new ShortCircuitRunContext(networkUuid, variantId, receiver, parameters,
77+
reportUuid, reporterId, reportType, userId, busId);
7778
return new ShortCircuitResultContext(resultUuid, runContext);
7879
}
7980

@@ -94,6 +95,7 @@ public Message<String> toMessage(ObjectMapper objectMapper) {
9495
.setHeader(HEADER_USER_ID, runContext.getUserId())
9596
.setHeader(REPORT_UUID_HEADER, runContext.getReportUuid() != null ? runContext.getReportUuid().toString() : null)
9697
.setHeader(REPORTER_ID_HEADER, runContext.getReporterId())
98+
.setHeader(REPORT_TYPE_HEADER, runContext.getReportType())
9799
.setHeader(HEADER_BUS_ID, runContext.getBusId())
98100
.build();
99101
}

src/main/java/org/gridsuite/shortcircuit/server/service/ShortCircuitRunContext.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ public class ShortCircuitRunContext {
3333

3434
private final String reporterId;
3535

36+
private final String reportType;
37+
3638
private final String userId;
3739

3840
private final String busId;
3941

4042
private Map<String, ShortCircuitLimits> shortCircuitLimits = new HashMap<>();
4143

42-
public ShortCircuitRunContext(UUID networkUuid, String variantId, String receiver, ShortCircuitParameters parameters, UUID reportUuid, String reporterId, String userId, String busId) {
44+
public ShortCircuitRunContext(UUID networkUuid, String variantId, String receiver, ShortCircuitParameters parameters,
45+
UUID reportUuid, String reporterId, String reportType, String userId, String busId) {
4346
this.networkUuid = Objects.requireNonNull(networkUuid);
4447
this.variantId = variantId;
4548
this.receiver = receiver;
4649
this.parameters = Objects.requireNonNull(parameters);
4750
this.reportUuid = reportUuid;
4851
this.reporterId = reporterId;
52+
this.reportType = reportType;
4953
this.userId = userId;
5054
this.busId = busId;
5155
}

src/main/java/org/gridsuite/shortcircuit/server/service/ShortCircuitWorkerService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
public class ShortCircuitWorkerService {
4949

5050
private static final Logger LOGGER = LoggerFactory.getLogger(ShortCircuitWorkerService.class);
51-
52-
private static final String SHORTCIRCUIT_TYPE_REPORT = "ShortCircuitAnalysis";
51+
private static final String SHORTCIRCUIT_ALL_BUSES_DEFAULT_TYPE_REPORT = "AllBusesShortCircuitAnalysis";
52+
private static final String SHORTCIRCUIT_ONE_BUS_DEFAULT_TYPE_REPORT = "OneBusShortCircuitAnalysis";
5353

5454
private NetworkStoreService networkStoreService;
5555
private ReportService reportService;
@@ -99,9 +99,15 @@ private ShortCircuitAnalysisResult run(ShortCircuitRunContext context, UUID resu
9999
Reporter rootReporter = Reporter.NO_OP;
100100
Reporter reporter = Reporter.NO_OP;
101101
if (context.getReportUuid() != null) {
102-
String rootReporterId = context.getReporterId() == null ? SHORTCIRCUIT_TYPE_REPORT : context.getReporterId() + "@" + SHORTCIRCUIT_TYPE_REPORT;
102+
String reportType = context.getReportType();
103+
if (StringUtils.isEmpty(reportType)) {
104+
reportType = StringUtils.isEmpty(context.getBusId()) ? SHORTCIRCUIT_ALL_BUSES_DEFAULT_TYPE_REPORT : SHORTCIRCUIT_ONE_BUS_DEFAULT_TYPE_REPORT;
105+
}
106+
String rootReporterId = context.getReporterId() == null ? reportType : context.getReporterId() + "@" + reportType;
103107
rootReporter = new ReporterModel(rootReporterId, rootReporterId);
104-
reporter = rootReporter.createSubReporter(SHORTCIRCUIT_TYPE_REPORT, SHORTCIRCUIT_TYPE_REPORT + " (${providerToUse})", "providerToUse", ShortCircuitAnalysis.find().getName());
108+
reporter = rootReporter.createSubReporter(reportType, reportType + " (${providerToUse})", "providerToUse", ShortCircuitAnalysis.find().getName());
109+
// Delete any previous short-circuit computation logs
110+
reportService.deleteReport(context.getReportUuid(), reportType);
105111
}
106112

107113
CompletableFuture<ShortCircuitAnalysisResult> future = runShortCircuitAnalysisAsync(context, network, reporter, resultUuid);

src/test/java/org/gridsuite/shortcircuit/server/ReportServiceTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public MockResponse dispatch(RecordedRequest request) {
7575
if (requestPath.equals(String.format("/v1/reports/%s", REPORT_UUID))) {
7676
assertEquals(REPORT_JSON, request.getBody().readUtf8());
7777
return new MockResponse().setResponseCode(HttpStatus.OK.value());
78+
} else if (requestPath.equals(String.format("/v1/reports/%s?reportTypeFilter=AllBusesShortCircuitAnalysis&errorOnReportNotFound=false", REPORT_UUID))) {
79+
assertEquals("", request.getBody().readUtf8());
80+
return new MockResponse().setResponseCode(HttpStatus.OK.value());
7881
} else if (requestPath.equals(String.format("/v1/reports/%s", REPORT_ERROR_UUID))) {
7982
return new MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
8083
} else {
@@ -91,9 +94,15 @@ public MockResponse dispatch(RecordedRequest request) {
9194
}
9295

9396
@Test
94-
public void test() {
97+
public void testSendReport() {
9598
Reporter reporter = new ReporterModel("test", "a test");
9699
reportService.sendReport(REPORT_UUID, reporter);
97100
assertThrows(RestClientException.class, () -> reportService.sendReport(REPORT_ERROR_UUID, reporter));
98101
}
102+
103+
@Test
104+
public void testDeleteReport() {
105+
reportService.deleteReport(REPORT_UUID, "AllBusesShortCircuitAnalysis");
106+
assertThrows(RestClientException.class, () -> reportService.deleteReport(REPORT_ERROR_UUID, "AllBusesShortCircuitAnalysis"));
107+
}
99108
}

src/test/java/org/gridsuite/shortcircuit/server/ShortCircuitAnalysisControllerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void runTest() throws Exception {
289289
shortCircuitParameters.setWithFortescueResult(false);
290290
String parametersJson = mapper.writeValueAsString(shortCircuitParameters);
291291
MvcResult result = mockMvc.perform(post(
292-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
292+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=AllBusesShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
293293
.header(HEADER_USER_ID, "userId")
294294
.contentType(MediaType.APPLICATION_JSON)
295295
.content(parametersJson))
@@ -402,7 +402,7 @@ public void runWithBusIdTest() throws Exception {
402402
shortCircuitParameters.setWithFortescueResult(true);
403403
String parametersJson = mapper.writeValueAsString(shortCircuitParameters);
404404
MvcResult result = mockMvc.perform(post(
405-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
405+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=OneBusShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
406406
.param(HEADER_BUS_ID, "NGEN")
407407
.header(HEADER_USER_ID, "userId")
408408
.contentType(MediaType.APPLICATION_JSON)
@@ -460,7 +460,7 @@ public void runWithBusBarSectionIdTest() throws Exception {
460460
shortCircuitParameters.setWithFortescueResult(true);
461461
String parametersJson = mapper.writeValueAsString(shortCircuitParameters);
462462
MvcResult result = mockMvc.perform(post(
463-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + NODE_BREAKER_NETWORK_VARIANT_ID, NODE_BREAKER_NETWORK_UUID)
463+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=OneBusShortCircuitAnalysis&receiver=me&variantId=" + NODE_BREAKER_NETWORK_VARIANT_ID, NODE_BREAKER_NETWORK_UUID)
464464
.param(HEADER_BUS_ID, "S1VL2_BBS1")
465465
.header(HEADER_USER_ID, "userId")
466466
.contentType(MediaType.APPLICATION_JSON)
@@ -499,7 +499,7 @@ public void runWithBusBarSectionIdAndErrorTest() throws Exception {
499499
.thenReturn(CompletableFuture.completedFuture(ShortCircuitAnalysisResultMock.RESULT_MAGNITUDE_FULL));
500500

501501
MvcResult result = mockMvc.perform(post(
502-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + NODE_BREAKER_NETWORK_VARIANT_ID, NODE_BREAKER_NETWORK_UUID)
502+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=OneBusShortCircuitAnalysis&receiver=me&variantId=" + NODE_BREAKER_NETWORK_VARIANT_ID, NODE_BREAKER_NETWORK_UUID)
503503
.param(HEADER_BUS_ID, "BUSBARSECTION_ID_NOT_EXISTING")
504504
.header(HEADER_USER_ID, "userId"))
505505
.andExpect(status().isOk())
@@ -533,7 +533,7 @@ public void stopTest() throws Exception {
533533
.thenReturn(CompletableFuture.completedFuture(RESULT));
534534

535535
mockMvc.perform(post(
536-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
536+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=AllBusesShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
537537
.header(HEADER_USER_ID, "userId"))
538538
.andExpect(status().isOk())
539539
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
@@ -583,7 +583,7 @@ public void runWithReportTest() {
583583
.thenReturn(CompletableFuture.completedFuture(RESULT));
584584

585585
mockMvc.perform(post(
586-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
586+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportType=AllBusesShortCircuitAnalysis&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
587587
.header(HEADER_USER_ID, "user"))
588588
.andExpect(status().isOk())
589589
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
@@ -607,7 +607,7 @@ public void runWithNoShortcircuitDataTest() {
607607
.thenReturn(CompletableFuture.completedFuture(RESULT));
608608

609609
mockMvc.perform(post(
610-
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
610+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportType=AllBusesShortCircuitAnalysis&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
611611
.header(HEADER_USER_ID, "user"))
612612
.andExpect(status().isOk())
613613
.andExpect(content().contentType(MediaType.APPLICATION_JSON))

src/test/java/org/gridsuite/shortcircuit/server/service/ShortCircuitServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ void testLogsMappersIsCalled() throws Exception {
5757
final UUID reportUuid = UUID.fromString("22222222-2222-2222-2222-222222222222");
5858
final UUID resultUuid = UUID.fromString("33333333-3333-3333-3333-333333333333");
5959
final String reporterId = "44444444-4444-4444-4444-444444444444";
60-
final ShortCircuitRunContext runContext = new ShortCircuitRunContext(networkUuid, null, null, new ShortCircuitParameters(), reportUuid, reporterId, null, null);
60+
final ShortCircuitRunContext runContext = new ShortCircuitRunContext(networkUuid, null, null,
61+
new ShortCircuitParameters(), reportUuid, reporterId, "AllBusesShortCircuitAnalysis", null, null);
6162
final ShortCircuitResultContext resultContext = new ShortCircuitResultContext(resultUuid, runContext);
6263
final Network networkMocked = Mockito.mock(Network.class);
6364
final VariantManager variantManagerMocked = Mockito.mock(VariantManager.class);

0 commit comments

Comments
 (0)