Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle userId and shorten error messages #53

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.UUID;

import static org.gridsuite.sensitivityanalysis.server.service.NotificationService.HEADER_USER_ID;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;

Expand Down Expand Up @@ -74,8 +75,9 @@ public ResponseEntity<SensitivityAnalysisResult> run(@Parameter(description = "N
@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, reportType));
@RequestBody SensitivityAnalysisInputData sensitivityAnalysisInputData,
@RequestHeader(HEADER_USER_ID) String userId) {
SensitivityAnalysisResult result = workerService.run(new SensitivityAnalysisRunContext(networkUuid, variantId, sensitivityAnalysisInputData, null, provider, reportUuid, reporterId, reportType, userId));
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result);
}

Expand All @@ -92,8 +94,9 @@ public ResponseEntity<UUID> runAndSave(@Parameter(description = "Network UUID")
@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, reportType));
@RequestBody SensitivityAnalysisInputData sensitivityAnalysisInputData,
@RequestHeader(HEADER_USER_ID) String userId) {
UUID resultUuid = service.runAndSaveResult(new SensitivityAnalysisRunContext(networkUuid, variantId, sensitivityAnalysisInputData, receiver, provider, reportUuid, reporterId, reportType, userId));
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,28 @@ public class NotificationService {
private static final Logger STOP_MESSAGE_LOGGER = LoggerFactory.getLogger(STOP_CATEGORY_BROKER_OUTPUT);
private static final Logger RESULT_MESSAGE_LOGGER = LoggerFactory.getLogger(RESULT_CATEGORY_BROKER_OUTPUT);
private static final Logger FAILED_MESSAGE_LOGGER = LoggerFactory.getLogger(FAILED_CATEGORY_BROKER_OUTPUT);
private static final String LOGGER_PREFIX = "Sending message : {}";

public static final String CANCEL_MESSAGE = "Sensitivity analysis was canceled";
public static final String FAIL_MESSAGE = "Sensitivity analysis has failed";

public static final String HEADER_RESULT_UUID = "resultUuid";
public static final String HEADER_RECEIVER = "receiver";
public static final String HEADER_MESSAGE = "message";
public static final String HEADER_USER_ID = "userId";

private static final int MSG_MAX_LENGTH = 256;

@Autowired
private StreamBridge publisher;

public void sendRunMessage(Message<String> message) {
RUN_MESSAGE_LOGGER.debug("Sending message : {}", message);
RUN_MESSAGE_LOGGER.debug(LOGGER_PREFIX, message);
publisher.send("publishRun-out-0", message);
}

public void sendCancelMessage(Message<String> message) {
CANCEL_MESSAGE_LOGGER.debug("Sending message : {}", message);
CANCEL_MESSAGE_LOGGER.debug(LOGGER_PREFIX, message);
publisher.send("publishCancel-out-0", message);
}

Expand All @@ -61,7 +65,7 @@ public void sendResultMessage(UUID resultUuid, String receiver) {
.setHeader(HEADER_RESULT_UUID, resultUuid.toString())
.setHeader(HEADER_RECEIVER, receiver)
.build();
RESULT_MESSAGE_LOGGER.debug("Sending message : {}", message);
RESULT_MESSAGE_LOGGER.debug(LOGGER_PREFIX, message);
publisher.send("publishResult-out-0", message);
}

Expand All @@ -73,19 +77,36 @@ public void publishStop(UUID resultUuid, String receiver) {
.setHeader(HEADER_RECEIVER, receiver)
.setHeader(HEADER_MESSAGE, CANCEL_MESSAGE)
.build();
STOP_MESSAGE_LOGGER.debug("Sending message : {}", message);
STOP_MESSAGE_LOGGER.debug(LOGGER_PREFIX, message);
publisher.send("publishStopped-out-0", message);
}

@PostCompletion
public void publishFail(UUID resultUuid, String receiver, String causeMessage) {
public void publishFail(UUID resultUuid, String receiver, String causeMessage, String userId) {
Message<String> message = MessageBuilder
.withPayload("")
.setHeader(HEADER_RESULT_UUID, resultUuid.toString())
.setHeader(HEADER_RECEIVER, receiver)
.setHeader(HEADER_MESSAGE, FAIL_MESSAGE + " : " + causeMessage)
.setHeader(HEADER_USER_ID, userId)
.setHeader(HEADER_MESSAGE, shortenMessage(FAIL_MESSAGE + " : " + causeMessage))
.build();
FAILED_MESSAGE_LOGGER.debug("Sending message : {}", message);
FAILED_MESSAGE_LOGGER.debug(LOGGER_PREFIX, message);
publisher.send("publishFailed-out-0", message);
}

// the message is shortened before being sent to the study-server.
// I keep the beginning and ending, it should make it easier to identify
public String shortenMessage(String msg) {
if (msg == null) {
return msg;
}

String shortMsg = msg;
if (shortMsg.length() > MSG_MAX_LENGTH) {
shortMsg = msg.substring(0, MSG_MAX_LENGTH / 2) +
" ... " +
msg.substring(msg.length() - MSG_MAX_LENGTH / 2, msg.length() - 1);
}
return shortMsg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.io.UncheckedIOException;
import java.util.*;

import static org.gridsuite.sensitivityanalysis.server.service.NotificationService.HEADER_USER_ID;

/**
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
*/
Expand Down Expand Up @@ -63,6 +65,7 @@ public static SensitivityAnalysisResultContext fromMessage(Message<String> messa

String receiver = (String) headers.get("receiver");
String provider = (String) headers.get("provider");
String userId = (String) headers.get(HEADER_USER_ID);
SensitivityAnalysisInputData sensitivityAnalysisInputData;
try {
sensitivityAnalysisInputData = objectMapper.readValue(message.getPayload(), new TypeReference<>() { });
Expand All @@ -73,7 +76,7 @@ public static SensitivityAnalysisResultContext fromMessage(Message<String> messa
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, reportType);
variantId, sensitivityAnalysisInputData, receiver, provider, reportUuid, reporterId, reportType, userId);
return new SensitivityAnalysisResultContext(resultUuid, runContext);
}

Expand All @@ -93,6 +96,7 @@ public Message<String> toMessage(ObjectMapper objectMapper) {
.setHeader(REPORT_UUID, runContext.getReportUuid())
.setHeader(REPORTER_ID_HEADER, runContext.getReporterId())
.setHeader(REPORT_TYPE_HEADER, runContext.getReportType())
.setHeader(HEADER_USER_ID, runContext.getUserId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.gridsuite.sensitivityanalysis.server.service;

import lombok.Getter;
import org.gridsuite.sensitivityanalysis.server.dto.SensitivityAnalysisInputData;

import java.util.Objects;
Expand All @@ -14,6 +15,7 @@
/**
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
*/
@Getter
public class SensitivityAnalysisRunContext {

private final UUID networkUuid;
Expand All @@ -32,11 +34,14 @@ public class SensitivityAnalysisRunContext {

private final String reporterId;

private final String userId;

private final String reportType;

public SensitivityAnalysisRunContext(UUID networkUuid, String variantId,
SensitivityAnalysisInputData sensitivityAnalysisInputData,
String receiver, String provider, UUID reportUuid, String reporterId, String reportType) {
String receiver, String provider, UUID reportUuid,
String reporterId, String reportType, String userId) {
this.networkUuid = Objects.requireNonNull(networkUuid);
this.variantId = variantId;
this.sensitivityAnalysisInputData = Objects.requireNonNull(sensitivityAnalysisInputData);
Expand All @@ -46,41 +51,6 @@ public SensitivityAnalysisRunContext(UUID networkUuid, String variantId,
this.reportUuid = reportUuid;
this.reporterId = reporterId;
this.reportType = reportType;
}

public UUID getNetworkUuid() {
return networkUuid;
}

public String getVariantId() {
return variantId;
}

public SensitivityAnalysisInputData getSensitivityAnalysisInputData() {
return sensitivityAnalysisInputData;
}

public SensitivityAnalysisInputs getSensitivityAnalysisInputs() {
return sensitivityAnalysisInputs;
}

public String getReceiver() {
return receiver;
}

public String getProvider() {
return provider;
}

public UUID getReportUuid() {
return reportUuid;
}

public String getReporterId() {
return reporterId;
}

public String getReportType() {
return reportType;
this.userId = userId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ public Consumer<Message<String>> consumeRun() {
} catch (Exception | OutOfMemoryError e) {
if (!(e instanceof CancellationException)) {
LOGGER.error(FAIL_MESSAGE, e);
notificationService.publishFail(resultContext.getResultUuid(), resultContext.getRunContext().getReceiver(), e.getMessage());
notificationService.publishFail(resultContext.getResultUuid(), resultContext.getRunContext().getReceiver(),
e.getMessage(), resultContext.getRunContext().getUserId());
resultRepository.delete(resultContext.getResultUuid());
resultRepository.insertStatus(List.of(resultContext.getResultUuid()), SensitivityAnalysisStatus.FAILED.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import static com.powsybl.network.store.model.NetworkStoreApi.VERSION;
import static org.gridsuite.sensitivityanalysis.server.service.NotificationService.CANCEL_MESSAGE;
import static org.gridsuite.sensitivityanalysis.server.service.NotificationService.FAIL_MESSAGE;
import static org.gridsuite.sensitivityanalysis.server.service.NotificationService.HEADER_USER_ID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -511,6 +512,7 @@ public void runTest() throws Exception {
MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis&variantId=" + VARIANT_3_ID, NETWORK_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_1))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand All @@ -522,6 +524,7 @@ public void runTest() throws Exception {
result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis", NETWORK_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(sensitivityInput))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand All @@ -533,6 +536,7 @@ public void runTest() throws Exception {
result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis&provider=OpenLoadFlow", NETWORK_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_HVDC_DELTA_A))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand All @@ -545,6 +549,7 @@ public void runAndSaveTest() throws Exception {
MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_1))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -708,6 +713,7 @@ public void deleteResultsTest() {
MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis", NETWORK_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_1))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -748,6 +754,7 @@ public void stopTest() throws Exception {
mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_STOP_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_1))
.andExpect(status().isOk());

Expand All @@ -768,6 +775,7 @@ public void runTestWithError() {
MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=SensitivityAnalysis&receiver=me&variantId=" + VARIANT_1_ID, NETWORK_ERROR_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_1))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand All @@ -791,6 +799,7 @@ public void runWithReportTest() {
MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run?reportType=SensitivityAnalysis&reportUuid=" + REPORT_UUID + "&reporterId=" + UUID.randomUUID(), NETWORK_UUID)
.contentType(MediaType.APPLICATION_JSON)
.header(HEADER_USER_ID, "testUserId")
.content(SENSITIVITY_INPUT_1))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
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, null);
context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, 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, null);
context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, 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, null);
context = new SensitivityAnalysisRunContext(NETWORK_UUID, VARIANT_ID, inputData, null, 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 @@ -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, null);
getDummyInputData(), null, 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, null);
inputData, null, "OpenLoadFlow", null, 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, null));
getDummyInputData(), null, 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, null));
getDummyInputData(), null, null, null, null, null, null));
assertThat(gottenResultUuid, not(nullValue()));
assertThat(gottenResultUuid, is(resultUuid));

Expand Down
Loading