Skip to content

Commit

Permalink
fix: truncate report message when it is too big and log error (#94)
Browse files Browse the repository at this point in the history
Signed-off-by: Joris Mancini <[email protected]>
  • Loading branch information
TheMaskedTurtle authored Oct 18, 2024
1 parent 6ca57d9 commit 94404b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/main/java/org/gridsuite/report/server/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class ReportService {
// the maximum number of parameters allowed in an In query. Prevents the number of parameters to reach the maximum allowed (65,535)
private static final int SQL_QUERY_MAX_PARAM_NUMBER = 10000;

static final int MAX_MESSAGE_CHAR = 500;

private final ReportNodeRepository reportNodeRepository;

static {
Expand Down Expand Up @@ -153,14 +155,23 @@ private void appendReportElements(ReportNodeEntity reportEntity, ReportNode repo

private void createNewReport(UUID id, ReportNode reportNode) {
var persistedReport = reportNodeRepository.save(
new ReportNodeEntity(id, reportNode.getMessage(), System.nanoTime() - NANOS_FROM_EPOCH_TO_START, null, severities(reportNode))
new ReportNodeEntity(id, truncatedMessage(reportNode.getMessage()), System.nanoTime() - NANOS_FROM_EPOCH_TO_START, null, severities(reportNode))
);
reportNode.getChildren().forEach(c -> saveReportNodeRecursively(persistedReport, c));
}

private static String truncatedMessage(String message) {
if (message.length() <= MAX_MESSAGE_CHAR) {
return message;
}
String truncatedMessage = message.substring(0, MAX_MESSAGE_CHAR);
LOGGER.error("Message {}... exceeds max character length ({}). It will be truncated", truncatedMessage, MAX_MESSAGE_CHAR);
return truncatedMessage;
}

private void saveReportNodeRecursively(ReportNodeEntity parentReportNodeEntity, ReportNode reportNode) {
var reportNodeEntity = new ReportNodeEntity(
reportNode.getMessage(),
truncatedMessage(reportNode.getMessage()),
System.nanoTime() - NANOS_FROM_EPOCH_TO_START,
parentReportNodeEntity,
severities(reportNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public ReportNodeEntity(UUID id, String message, long nanos, ReportNodeEntity pa
this.severities = severities;
}

public ReportNodeEntity(UUID id, long nanos) {
this.id = id;
this.nanos = nanos;
}

public void addSeverities(Set<String> severities) {
this.severities.addAll(severities);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/gridsuite/report/server/ReportServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collections;
import java.util.Set;
import java.util.UUID;

import static org.gridsuite.report.server.ReportService.MAX_MESSAGE_CHAR;
import static org.gridsuite.report.server.utils.TestUtils.*;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -210,6 +212,28 @@ void testParentReportsSeverityListIsUpdatedAfterAppendingNewReport() {
assertEquals(Set.of("ERROR"), reportNodeEntityBis.getSeverities());
}

@Test
void testCreateReportWithTooLongMessage() {
String veryLongString = String.join("", Collections.nCopies(1000, "verylongstring"));

var rootReportNode = ReportNode.newRootReportNode()
.withMessageTemplate("test", veryLongString)
.build();

rootReportNode.newReportNode()
.withMessageTemplate("test", veryLongString)
.add();

var reportUuid = UUID.randomUUID();
reportService.createReport(reportUuid, rootReportNode);

var rootReportNodeEntity = reportService.getReportNodeEntity(reportUuid).orElseThrow();
assertEquals(veryLongString.substring(0, MAX_MESSAGE_CHAR), rootReportNodeEntity.getMessage());

var reportNodeEntity = reportService.getReportNodeEntity(rootReportNodeEntity.getChildren().get(0).getId()).orElseThrow();
assertEquals(veryLongString.substring(0, MAX_MESSAGE_CHAR), reportNodeEntity.getMessage());
}

private static void assertReportsAreEqual(ReportNodeEntity entity, ReportNode reportNode, Set<String> severityList) {
assertEquals(reportNode.getMessage(), entity.getMessage());
assertEquals(severityList, entity.getSeverities());
Expand Down

0 comments on commit 94404b8

Please sign in to comment.