Skip to content

Commit

Permalink
fix:Serializing subreports to JSON only once even if the global repor…
Browse files Browse the repository at this point in the history
…t is decorated..

Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
  • Loading branch information
ehsavoie committed Jul 21, 2023
1 parent cf6ca7a commit 5894e8c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonSerializer;
import com.redhat.insights.config.InsightsConfiguration;
import com.redhat.insights.logging.InsightsLogger;
import java.io.IOException;
import java.lang.management.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -80,6 +81,7 @@ public abstract class AbstractTopLevelReportBase implements InsightsReport {
private final JsonSerializer<InsightsReport> serializer;
private final InsightsLogger logger;
private final InsightsConfiguration config;
private byte[] subReport;

// Can't be set properly until after report has been generated
private String idHash = "";
Expand Down Expand Up @@ -204,6 +206,19 @@ public void generateReport(Filtering masking) {

protected abstract Package[] getPackages();

@Override
public byte[] getSubModulesReport() {
if (subReport == null) {
subReport = InsightsReport.super.getSubModulesReport();
}
return subReport;
}

@Override
public void close() throws IOException {
subReport = null;
}

/**
* This is the top-level name - implementors that may have to care about multi-tenancy should
* handle that in product-specific code.
Expand Down
36 changes: 35 additions & 1 deletion api/src/main/java/com/redhat/insights/InsightsReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

import static com.redhat.insights.InsightsErrorCode.ERROR_SERIALIZING_TO_JSON;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.Map;

Expand All @@ -13,7 +16,8 @@
*
* @see InsightsSubreport for runtime-specific sub-reports
*/
public interface InsightsReport {
public interface InsightsReport extends Closeable {
static final byte[] EMPTY_BYTE_ARRAY = new byte[0];

Map<String, InsightsSubreport> getSubreports();

Expand Down Expand Up @@ -66,4 +70,34 @@ default String serialize() {
throw new InsightsException(ERROR_SERIALIZING_TO_JSON, "JSON serialization exception", e);
}
}

/**
* Serializes this report to JSON for transport
*
* @return JSON serialized report
*/
default byte[] getSubModulesReport() {
ObjectMapper mapper = ObjectMappers.createFor(this);

try (ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator generator = mapper.writerWithDefaultPrettyPrinter().createGenerator(out)) {
generator.writeStartObject();
for (Map.Entry<String, InsightsSubreport> entry : getSubreports().entrySet()) {
generator.writeObjectField(entry.getKey(), entry.getValue());
}
generator.writeEndObject();
generator.flush();
byte[] report = out.toByteArray();
boolean notEmptyArray = report.length > 3;
if (notEmptyArray) {
byte[] finalReport = new byte[report.length - 1];
finalReport[0] = ',';
System.arraycopy(report, 1, finalReport, 1, report.length - 2);
return finalReport;
}
return EMPTY_BYTE_ARRAY;
} catch (IOException e) {
throw new InsightsException(ERROR_SERIALIZING_TO_JSON, "JSON serialization exception", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ public void generate() {
InsightsHttpClient httpClient = httpClientSupplier.get();
if (httpClient.isReadyToSend()) {
generateConnectReport();
httpClient.sendInsightsReport(getIdHash() + "_connect", report);
try {
httpClient.sendInsightsReport(getIdHash() + "_connect", report);
} finally {
try {
report.close();
} catch (IOException ioex) {
// Nothing to be done there
}
}
} else {
logger.debug("Insights is not configured to send: " + configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Map;
import java.nio.charset.StandardCharsets;

/** JSON serializer for an {@link InsightsReport} object. */
public class InsightsReportSerializer extends JsonSerializer<InsightsReport> {
Expand All @@ -23,9 +23,8 @@ public void serialize(
if (!insightsReport.getBasic().isEmpty()) {
generator.writeObjectField("basic", insightsReport.getBasic());
}
for (Map.Entry<String, InsightsSubreport> entry : insightsReport.getSubreports().entrySet()) {
generator.writeObjectField(entry.getKey(), entry.getValue());
}
byte[] subReport = insightsReport.getSubModulesReport();
generator.writeRaw(new String(subReport, StandardCharsets.UTF_8));
generator.writeEndObject();
generator.flush();
}
Expand Down
4 changes: 4 additions & 0 deletions api/src/main/java/com/redhat/insights/UpdateReportImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.redhat.insights.jars.JarInfo;
import com.redhat.insights.jars.JarInfoSubreport;
import com.redhat.insights.logging.InsightsLogger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -77,4 +78,7 @@ public void decorate(String key, String value) {
logger.debug(
String.format("Attempt to add %s => %s to an update report. Ignored.", key, value));
}

@Override
public void close() throws IOException {}
}

0 comments on commit 5894e8c

Please # to comment.