use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.
the class JsonDeserializer method getListValue.
public static <T> List<T> getListValue(String jsonString, Function<JsonElement, T> function, String... paths) throws JsonPathNotFoundException, IOException {
JsonElement jsonNode = getChildNode(jsonString, paths);
if (jsonNode != null && jsonNode.isJsonArray()) {
JsonArray array = jsonNode.getAsJsonArray();
List<T> values = new ArrayList<>(array.size());
for (int i = 0; i < array.size(); i++) {
values.add(function.apply(array.get(i)));
}
return values;
}
throw new JsonPathNotFoundException();
}
use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.
the class CronTransportActionTests method testNormal.
public void testNormal() throws IOException, JsonPathNotFoundException {
CronRequest request = new CronRequest();
CronNodeRequest nodeRequest = new CronNodeRequest();
BytesStreamOutput nodeRequestOut = new BytesStreamOutput();
nodeRequestOut.setVersion(Version.V_1_0_0);
nodeRequest.writeTo(nodeRequestOut);
StreamInput siNode = nodeRequestOut.bytes().streamInput();
siNode.setVersion(Version.V_1_0_0);
CronNodeRequest nodeResponseRead = new CronNodeRequest(siNode);
CronNodeResponse nodeResponse1 = action.nodeOperation(nodeResponseRead);
CronNodeResponse nodeResponse2 = action.nodeOperation(new CronNodeRequest());
CronResponse response = action.newResponse(request, Arrays.asList(nodeResponse1, nodeResponse2), Collections.emptyList());
assertEquals(2, response.getNodes().size());
assertTrue(!response.hasFailures());
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
String json = Strings.toString(builder);
Function<JsonElement, String> function = (s) -> {
try {
return JsonDeserializer.getTextValue(s, CronNodeResponse.NODE_ID);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
return null;
};
assertArrayEquals(JsonDeserializer.getArrayValue(json, function, CronResponse.NODES_JSON_KEY), new String[] { localNodeID, localNodeID });
}
use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.
the class JsonDeserializer method getDoubleArrayValue.
/**
* Search an int number inside a JSON string matching the input path expression
*
* @param jsonString an encoded JSON string
* @param paths path fragments
* @return list of double
* @throws JsonPathNotFoundException if json path is invalid
* @throws IOException if the underlying input source has problems
* during parsing
*/
public static double[] getDoubleArrayValue(String jsonString, String... paths) throws JsonPathNotFoundException, IOException {
JsonElement jsonNode = getChildNode(jsonString, paths);
if (jsonNode != null && jsonNode.isJsonArray()) {
JsonArray array = jsonNode.getAsJsonArray();
List<Double> values = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
values.add(array.get(i).getAsDouble());
}
return values.stream().mapToDouble(i -> i).toArray();
}
throw new JsonPathNotFoundException();
}
use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.
the class JsonDeserializer method getArrayValue.
/**
* Search an array inside a JSON string matching the input path expression and convert each element using a function
*
* @param jsonString an encoded JSON string
* @param function function to parse each element
* @param paths path fragments
* @return an array of values
* @throws JsonPathNotFoundException if json path is invalid
* @throws IOException if the underlying input source has problems
* during parsing
*/
@SuppressWarnings("unchecked")
public static <T> T[] getArrayValue(String jsonString, Function<JsonElement, T> function, String... paths) throws JsonPathNotFoundException, IOException {
JsonElement jsonNode = getChildNode(jsonString, paths);
if (jsonNode != null && jsonNode.isJsonArray()) {
JsonArray array = jsonNode.getAsJsonArray();
Object[] values = new Object[array.size()];
for (int i = 0; i < array.size(); i++) {
values[i] = function.apply(array.get(i));
}
return (T[]) values;
}
throw new JsonPathNotFoundException();
}
use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.
the class AnomalyResultTests method testJsonResponse.
public void testJsonResponse() throws IOException, JsonPathNotFoundException {
AnomalyResultResponse response = new AnomalyResultResponse(4d, 0.993, 1.01, Collections.singletonList(new FeatureData(featureId, featureName, 0d)), randomAlphaOfLength(4), randomLong(), randomLong(), randomBoolean(), randomInt(), new double[] { randomDoubleBetween(0, 1.0, true), randomDoubleBetween(0, 1.0, true) }, new double[] { randomDouble(), randomDouble() }, new double[][] { new double[] { randomDouble(), randomDouble() } }, new double[] { randomDouble() }, randomDoubleBetween(1.1, 10.0, true));
XContentBuilder builder = jsonBuilder();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
String json = Strings.toString(builder);
Function<JsonElement, FeatureData> function = (s) -> {
try {
String featureId = JsonDeserializer.getTextValue(s, FeatureData.FEATURE_ID_FIELD);
String featureName = JsonDeserializer.getTextValue(s, FeatureData.FEATURE_NAME_FIELD);
double featureValue = JsonDeserializer.getDoubleValue(s, FeatureData.DATA_FIELD);
return new FeatureData(featureId, featureName, featureValue);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
return null;
};
AnomalyResultResponse readResponse = new AnomalyResultResponse(JsonDeserializer.getDoubleValue(json, AnomalyResultResponse.ANOMALY_GRADE_JSON_KEY), JsonDeserializer.getDoubleValue(json, AnomalyResultResponse.CONFIDENCE_JSON_KEY), JsonDeserializer.getDoubleValue(json, AnomalyResultResponse.ANOMALY_SCORE_JSON_KEY), JsonDeserializer.getListValue(json, function, AnomalyResultResponse.FEATURES_JSON_KEY), randomAlphaOfLength(4), randomLong(), randomLong(), randomBoolean(), randomInt(), new double[] { randomDoubleBetween(0, 1.0, true), randomDoubleBetween(0, 1.0, true) }, new double[] { randomDouble(), randomDouble() }, new double[][] { new double[] { randomDouble(), randomDouble() } }, new double[] { randomDouble() }, randomDoubleBetween(1.1, 10.0, true));
assertAnomalyResultResponse(readResponse, readResponse.getAnomalyGrade(), readResponse.getConfidence(), 0d);
}
Aggregations