-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
2,564 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
common/src/main/java/org/opensearch/ml/common/exception/ExecuteException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.opensearch.ml.common.exception; | ||
|
||
public class ExecuteException extends MLException{ | ||
public ExecuteException(String msg) { super(msg); } | ||
public ExecuteException(Throwable cause) { super(cause); } | ||
public ExecuteException(String msg, Throwable cause) { super(msg, cause); } | ||
} |
114 changes: 114 additions & 0 deletions
114
...va/org/opensearch/ml/common/input/execute/metricscorrelation/MetricsCorrelationInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.input.execute.metricscorrelation; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.core.ParseField; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.ml.common.FunctionName; | ||
import org.opensearch.ml.common.annotation.ExecuteInput; | ||
import org.opensearch.ml.common.input.Input; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
@ExecuteInput(algorithms={FunctionName.METRICS_CORRELATION}) | ||
@Data | ||
public class MetricsCorrelationInput implements Input { | ||
public static final String PARSE_FIELD_NAME = FunctionName.METRICS_CORRELATION.name(); | ||
public static final NamedXContentRegistry.Entry XCONTENT_REGISTRY = new NamedXContentRegistry.Entry( | ||
Input.class, | ||
new ParseField(PARSE_FIELD_NAME), | ||
it -> parse(it) | ||
); | ||
|
||
public static final String METRICS_FIELD = "metrics"; | ||
|
||
List<float[]> inputData; | ||
|
||
@Builder | ||
public MetricsCorrelationInput(List<float[]> inputData) { | ||
if (inputData == null || inputData.size() == 0) { | ||
throw new IllegalArgumentException("empty input data"); | ||
} | ||
int expectedLength = inputData.get(0).length; | ||
for (int i = 1; i < inputData.size(); i++) { | ||
float[] array = inputData.get(i); | ||
if (array.length != expectedLength) { | ||
// found an array with different length | ||
throw new IllegalArgumentException("All the input metrics sizes should be same"); | ||
} | ||
} | ||
if (inputData.size() >= expectedLength) { | ||
throw new IllegalArgumentException("The number of metrics to correlate must be smaller than the length of each time series."); | ||
} | ||
this.inputData = inputData; | ||
} | ||
|
||
public MetricsCorrelationInput(StreamInput in) throws IOException { | ||
this.inputData = in.readList(StreamInput::readFloatArray); | ||
} | ||
|
||
public static MetricsCorrelationInput parse(XContentParser parser) throws IOException { | ||
List<float[]> inputData = new ArrayList<>(); | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
|
||
switch (fieldName) { | ||
case METRICS_FIELD: | ||
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); | ||
List<Float> inputItem = new ArrayList<>(); | ||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
inputItem.add(parser.floatValue()); | ||
} | ||
float[] floatArray = new float[inputItem.size()]; | ||
int i = 0; | ||
|
||
for (Float f : inputItem) { | ||
floatArray[i++] = (f != null ? f : Float.NaN); | ||
} | ||
inputData.add(floatArray); | ||
} | ||
break; | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
return new MetricsCorrelationInput(inputData); | ||
} | ||
|
||
@Override | ||
public FunctionName getFunctionName() { | ||
return FunctionName.METRICS_CORRELATION; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeCollection(this.inputData, StreamOutput::writeFloatArray); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(METRICS_FIELD, inputData); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
common/src/main/java/org/opensearch/ml/common/model/MetricsCorrelationModelConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.ml.common.FunctionName; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
|
||
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
@Setter | ||
@Getter | ||
public class MetricsCorrelationModelConfig extends MLModelConfig { | ||
|
||
public static final String PARSE_FIELD_NAME = FunctionName.METRICS_CORRELATION.name(); | ||
|
||
@Builder(toBuilder = true) | ||
public MetricsCorrelationModelConfig(String modelType, String allConfig) { | ||
super(modelType, allConfig); | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return PARSE_FIELD_NAME; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (modelType != null) { | ||
builder.field(MODEL_TYPE_FIELD, modelType); | ||
} | ||
if (allConfig != null) { | ||
builder.field(ALL_CONFIG_FIELD, allConfig); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public static MetricsCorrelationModelConfig parse(XContentParser parser) throws IOException { | ||
String modelType = null; | ||
String allConfig = null; | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
|
||
switch (fieldName) { | ||
case MODEL_TYPE_FIELD: | ||
modelType = parser.text(); | ||
break; | ||
case ALL_CONFIG_FIELD: | ||
allConfig = parser.text(); | ||
break; | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
return new MetricsCorrelationModelConfig(modelType, allConfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ public enum MLOutputType { | |
TRAINING, | ||
PREDICTION, | ||
SAMPLE_ALGO, | ||
MODEL_TENSOR | ||
MODEL_TENSOR, | ||
MCORR_TENSOR | ||
} |
86 changes: 86 additions & 0 deletions
86
...in/java/org/opensearch/ml/common/output/execute/metrics_correlation/MCorrModelTensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.output.execute.metrics_correlation; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.io.stream.Writeable; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
@Data | ||
public class MCorrModelTensor implements Writeable, ToXContentObject { | ||
|
||
public static final String EVENT_WINDOW = "event_window"; | ||
public static final String EVENT_PATTERN = "event_pattern"; | ||
public static final String SUSPECTED_METRICS = "suspected_metrics"; | ||
|
||
private float[] event_window; | ||
private float[] event_pattern; | ||
private long[] suspected_metrics; | ||
|
||
@Builder | ||
public MCorrModelTensor(float[] event_window, float[] event_pattern, long[] suspected_metrics) { | ||
this.event_window = event_window; | ||
this.event_pattern = event_pattern; | ||
this.suspected_metrics = suspected_metrics; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (event_window != null) { | ||
builder.field(EVENT_WINDOW, event_window); | ||
} | ||
if (event_pattern != null) { | ||
builder.field(EVENT_PATTERN, event_pattern); | ||
} | ||
if (suspected_metrics != null) { | ||
builder.field(SUSPECTED_METRICS, suspected_metrics); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public MCorrModelTensor(StreamInput in) throws IOException { | ||
if (in.readBoolean()) { | ||
this.event_window = in.readFloatArray(); | ||
} | ||
if (in.readBoolean()) { | ||
this.event_pattern = in.readFloatArray(); | ||
} | ||
if (in.readBoolean()) { | ||
this.suspected_metrics = in.readLongArray(); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
if (event_window != null) { | ||
out.writeBoolean(true); | ||
out.writeFloatArray(event_window); | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
|
||
if (event_pattern != null) { | ||
out.writeBoolean(true); | ||
out.writeFloatArray(event_pattern); | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
if (suspected_metrics != null) { | ||
out.writeBoolean(true); | ||
out.writeLongArray(suspected_metrics); | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.