Skip to content

Commit

Permalink
Add flatten_result_index_mapping field to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiehanyang committed Sep 18, 2024
1 parent 7b5d7ce commit a01b52f
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/opensearch/ad/model/ADTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ public static ADTask parse(XContentParser parser, String taskId) throws IOExcept
detector.getRules(),
detector.getCustomResultIndexMinSize(),
detector.getCustomResultIndexMinAge(),
detector.getCustomResultIndexTTL()
detector.getCustomResultIndexTTL(),
detector.getFlattenResultIndexMapping()
);
return new Builder()
.taskId(parsedTaskId)
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/org/opensearch/ad/model/AnomalyDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public Integer getShingleSize(Integer customShingleSize) {
* @param customResultIndexMinSize custom result index lifecycle management min size condition
* @param customResultIndexMinAge custom result index lifecycle management min age condition
* @param customResultIndexTTL custom result index lifecycle management ttl
* @param flattenResultIndexMapping flag to indicate whether to flatten result index mapping or not
*/
public AnomalyDetector(
String detectorId,
Expand All @@ -176,7 +177,8 @@ public AnomalyDetector(
List<Rule> rules,
Integer customResultIndexMinSize,
Integer customResultIndexMinAge,
Integer customResultIndexTTL
Integer customResultIndexTTL,
Boolean flattenResultIndexMapping
) {
super(
detectorId,
Expand All @@ -203,7 +205,8 @@ public AnomalyDetector(
historyIntervals,
customResultIndexMinSize,
customResultIndexMinAge,
customResultIndexTTL
customResultIndexTTL,
flattenResultIndexMapping
);

checkAndThrowValidationErrors(ValidationAspect.DETECTOR);
Expand Down Expand Up @@ -280,6 +283,7 @@ public AnomalyDetector(StreamInput input) throws IOException {
this.customResultIndexMinSize = input.readOptionalInt();
this.customResultIndexMinAge = input.readOptionalInt();
this.customResultIndexTTL = input.readOptionalInt();
this.flattenResultIndexMapping = input.readOptionalBoolean();
}

public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
Expand Down Expand Up @@ -345,6 +349,7 @@ public void writeTo(StreamOutput output) throws IOException {
output.writeOptionalInt(customResultIndexMinSize);
output.writeOptionalInt(customResultIndexMinAge);
output.writeOptionalInt(customResultIndexTTL);
output.writeOptionalBoolean(flattenResultIndexMapping);
}

@Override
Expand Down Expand Up @@ -441,6 +446,7 @@ public static AnomalyDetector parse(
Integer customResultIndexMinSize = null;
Integer customResultIndexMinAge = null;
Integer customResultIndexTTL = null;
Boolean flattenResultIndexMapping = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -575,6 +581,9 @@ public static AnomalyDetector parse(
case RESULT_INDEX_FIELD_TTL:
customResultIndexTTL = onlyParseNumberValue(parser);
break;
case FLATTEN_RESULT_INDEX_MAPPING:
flattenResultIndexMapping = onlyParseBooleanValue(parser);
break;
default:
parser.skipChildren();
break;
Expand Down Expand Up @@ -605,7 +614,8 @@ public static AnomalyDetector parse(
rules,
customResultIndexMinSize,
customResultIndexMinAge,
customResultIndexTTL
customResultIndexTTL,
flattenResultIndexMapping
);
detector.setDetectionDateRange(detectionDateRange);
return detector;
Expand Down Expand Up @@ -692,4 +702,12 @@ private static Integer onlyParseNumberValue(XContentParser parser) throws IOExce
}
return null;
}


private static Boolean onlyParseBooleanValue(XContentParser parser) throws IOException {
if (parser.currentToken() == XContentParser.Token.VALUE_BOOLEAN) {
return parser.booleanValue();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ protected AnomalyDetector copyConfig(User user, Config config) {
detector.getRules(),
config.getCustomResultIndexMinSize(),
config.getCustomResultIndexMinAge(),
config.getCustomResultIndexTTL()
config.getCustomResultIndexTTL(),
config.getFlattenResultIndexMapping()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ public static ForecastTask parse(XContentParser parser, String taskId) throws IO
forecaster.getHistoryIntervals(),
forecaster.getCustomResultIndexMinSize(),
forecaster.getCustomResultIndexMinAge(),
forecaster.getCustomResultIndexTTL()
forecaster.getCustomResultIndexTTL(),
forecaster.getFlattenResultIndexMapping()
);
return new Builder()
.taskId(parsedTaskId)
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/org/opensearch/forecast/model/Forecaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public Forecaster(
Integer historyIntervals,
Integer customResultIndexMinSize,
Integer customResultIndexMinAge,
Integer customResultIndexTTL
Integer customResultIndexTTL,
Boolean flattenResultIndexMapping
) {
super(
forecasterId,
Expand All @@ -161,7 +162,8 @@ public Forecaster(
historyIntervals,
customResultIndexMinSize,
customResultIndexMinAge,
customResultIndexTTL
customResultIndexTTL,
flattenResultIndexMapping
);

checkAndThrowValidationErrors(ValidationAspect.FORECASTER);
Expand Down Expand Up @@ -303,6 +305,7 @@ public static Forecaster parse(
Integer customResultIndexMinSize = null;
Integer customResultIndexMinAge = null;
Integer customResultIndexTTL = null;
Boolean flattenResultIndexMapping = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -431,6 +434,9 @@ public static Forecaster parse(
case RESULT_INDEX_FIELD_TTL:
customResultIndexTTL = parser.intValue();
break;
case FLATTEN_RESULT_INDEX_MAPPING:
flattenResultIndexMapping = parser.booleanValue();
break;
default:
parser.skipChildren();
break;
Expand Down Expand Up @@ -461,7 +467,8 @@ public static Forecaster parse(
historyIntervals,
customResultIndexMinSize,
customResultIndexMinAge,
customResultIndexTTL
customResultIndexTTL,
flattenResultIndexMapping
);
return forecaster;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ protected Config copyConfig(User user, Config config) {
config.getHistoryIntervals(),
config.getCustomResultIndexMinSize(),
config.getCustomResultIndexMinAge(),
config.getCustomResultIndexTTL()
config.getCustomResultIndexTTL(),
config.getFlattenResultIndexMapping()
);
}

Expand Down
22 changes: 19 additions & 3 deletions src/main/java/org/opensearch/timeseries/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public abstract class Config implements Writeable, ToXContentObject {
public static final String RESULT_INDEX_FIELD_MIN_SIZE = "result_index_min_size";
public static final String RESULT_INDEX_FIELD_MIN_AGE = "result_index_min_age";
public static final String RESULT_INDEX_FIELD_TTL = "result_index_ttl";
public static final String FLATTEN_RESULT_INDEX_MAPPING = "flatten_result_index_mapping";

protected String id;
protected Long version;
Expand Down Expand Up @@ -118,6 +119,7 @@ public abstract class Config implements Writeable, ToXContentObject {
protected Integer customResultIndexMinSize;
protected Integer customResultIndexMinAge;
protected Integer customResultIndexTTL;
protected Boolean flattenResultIndexMapping;

public static String INVALID_RESULT_INDEX_NAME_SIZE = "Result index name size must contains less than "
+ MAX_RESULT_INDEX_NAME_SIZE
Expand Down Expand Up @@ -148,7 +150,8 @@ protected Config(
Integer historyIntervals,
Integer customResultIndexMinSize,
Integer customResultIndexMinAge,
Integer customResultIndexTTL
Integer customResultIndexTTL,
Boolean flattenResultIndexMapping
) {
if (Strings.isBlank(name)) {
errorMessage = CommonMessages.EMPTY_NAME;
Expand Down Expand Up @@ -287,6 +290,7 @@ protected Config(
this.customResultIndexMinSize = Strings.trimToNull(resultIndex) == null ? null : customResultIndexMinSize;
this.customResultIndexMinAge = Strings.trimToNull(resultIndex) == null ? null : customResultIndexMinAge;
this.customResultIndexTTL = Strings.trimToNull(resultIndex) == null ? null : customResultIndexTTL;
this.flattenResultIndexMapping = Strings.trimToNull(resultIndex) == null ? null : flattenResultIndexMapping;
}

public int suggestHistory() {
Expand Down Expand Up @@ -330,6 +334,7 @@ public Config(StreamInput input) throws IOException {
this.customResultIndexMinSize = input.readOptionalInt();
this.customResultIndexMinAge = input.readOptionalInt();
this.customResultIndexTTL = input.readOptionalInt();
this.flattenResultIndexMapping = input.readOptionalBoolean();
}

/*
Expand Down Expand Up @@ -382,6 +387,7 @@ public void writeTo(StreamOutput output) throws IOException {
output.writeOptionalInt(customResultIndexMinSize);
output.writeOptionalInt(customResultIndexMinAge);
output.writeOptionalInt(customResultIndexTTL);
output.writeOptionalBoolean(flattenResultIndexMapping);
}

public boolean invalidShingleSizeRange(Integer shingleSizeToTest) {
Expand Down Expand Up @@ -438,7 +444,8 @@ public boolean equals(Object o) {
&& Objects.equal(historyIntervals, config.historyIntervals)
&& Objects.equal(customResultIndexMinSize, config.customResultIndexMinSize)
&& Objects.equal(customResultIndexMinAge, config.customResultIndexMinAge)
&& Objects.equal(customResultIndexTTL, config.customResultIndexTTL);
&& Objects.equal(customResultIndexTTL, config.customResultIndexTTL)
&& Objects.equal(flattenResultIndexMapping, config.flattenResultIndexMapping);
}

@Generated
Expand All @@ -465,7 +472,8 @@ public int hashCode() {
historyIntervals,
customResultIndexMinSize,
customResultIndexMinAge,
customResultIndexTTL
customResultIndexTTL,
flattenResultIndexMapping
);
}

Expand Down Expand Up @@ -514,6 +522,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (customResultIndexTTL != null) {
builder.field(RESULT_INDEX_FIELD_TTL, customResultIndexTTL);
}
if (flattenResultIndexMapping != null) {
builder.field(FLATTEN_RESULT_INDEX_MAPPING, flattenResultIndexMapping);
}
return builder;
}

Expand Down Expand Up @@ -722,6 +733,10 @@ public Integer getCustomResultIndexTTL() {
return customResultIndexTTL;
}

public Boolean getFlattenResultIndexMapping() {
return flattenResultIndexMapping;
}

/**
* Identifies redundant feature names.
*
Expand Down Expand Up @@ -774,6 +789,7 @@ public String toString() {
.append("customResultIndexMinSize", customResultIndexMinSize)
.append("customResultIndexMinAge", customResultIndexMinAge)
.append("customResultIndexTTL", customResultIndexTTL)
.append("flattenResultIndexMapping", flattenResultIndexMapping)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ public ToXContentObject[] getConfig(String detectorId, BasicHeader header, boole
null,
detector.getCustomResultIndexMinSize(),
detector.getCustomResultIndexMinAge(),
detector.getCustomResultIndexTTL()
detector.getCustomResultIndexTTL(),
detector.getFlattenResultIndexMapping()
),
detectorJob,
historicalAdTask,
Expand Down Expand Up @@ -640,7 +641,8 @@ protected AnomalyDetector cloneDetector(AnomalyDetector anomalyDetector, String
null,
anomalyDetector.getCustomResultIndexMinSize(),
anomalyDetector.getCustomResultIndexMinAge(),
anomalyDetector.getCustomResultIndexTTL()
anomalyDetector.getCustomResultIndexTTL(),
anomalyDetector.getFlattenResultIndexMapping()
);
return detector;
}
Expand Down
Loading

0 comments on commit a01b52f

Please sign in to comment.