Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix that preview exits with null pointer exception (#74)
Browse files Browse the repository at this point in the history
Recently, we changed the anomaly result index’s schema and toXContent method by introducing a few fields. During the preview, some of the fields in anomaly result index can be null. Thus, toXContent can fail with a null pointer exception.

This PR fixes the issue by protecting a field’s serialization with a null check.

Testing done:
- Manually verified the issue is gone
- gradle build
  • Loading branch information
kaituo authored Mar 24, 2020
1 parent 091c367 commit 7534c89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand Down Expand Up @@ -692,7 +691,6 @@ public List<ThresholdingResult> getPreviewResults(double[][] dataPoints) {
throw new IllegalArgumentException("Insufficient data for preview results. Minimum required: " + minPreviewSize);
}
// Train RCF models and collect non-zero scores
Random random = new Random();
int rcfNumFeatures = dataPoints[0].length;
RandomCutForest forest = RandomCutForest
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,20 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
XContentBuilder xContentBuilder = builder
.startObject()
.field(DETECTOR_ID_FIELD, detectorId)
.field(FEATURE_DATA_FIELD, featureData.toArray())
.field(DATA_START_TIME_FIELD, dataStartTime.toEpochMilli())
.field(DATA_END_TIME_FIELD, dataEndTime.toEpochMilli())
.field(EXECUTION_START_TIME_FIELD, executionStartTime.toEpochMilli())
.field(EXECUTION_END_TIME_FIELD, executionEndTime.toEpochMilli());
.field(DATA_END_TIME_FIELD, dataEndTime.toEpochMilli());
if (featureData != null) {
// can be null during preview
xContentBuilder.field(FEATURE_DATA_FIELD, featureData.toArray());
}
if (executionStartTime != null) {
// can be null during preview
xContentBuilder.field(EXECUTION_START_TIME_FIELD, executionStartTime.toEpochMilli());
}
if (executionEndTime != null) {
// can be null during preview
xContentBuilder.field(EXECUTION_END_TIME_FIELD, executionEndTime.toEpochMilli());
}
if (anomalyScore != null && !anomalyScore.isNaN()) {
xContentBuilder.field(ANOMALY_SCORE_FIELD, anomalyScore);
}
Expand Down

0 comments on commit 7534c89

Please sign in to comment.