Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine CommonMessageAttributes and CommonName #77

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/org/opensearch/ad/constant/CommonName.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,17 @@ public class CommonName {
public static final String MODELS = "models";
public static final String MODEL = "model";
public static final String INIT_PROGRESS = "init_progress";
public static final String MODEL_SIZE_IN_BYTES = "model_size_in_bytes";
public static final String CATEGORICAL_FIELD = "category_field";

public static final String TOTAL_ENTITIES = "total_entities";
public static final String ACTIVE_ENTITIES = "active_entities";
public static final String ENTITY_INFO = "entity_info";
public static final String TOTAL_UPDATES = "total_updates";

// ======================================
// Historical detectors
// ======================================
public static final String AD_TASK = "ad_task";
public static final String AD_TASK_REMOTE = "ad_task_remote";
public static final String CANCEL_TASK = "cancel_task";
Expand Down Expand Up @@ -107,4 +113,25 @@ public class CommonName {
public static final String DATE_HISTOGRAM = "date_histogram";
// feature aggregation name
public static final String FEATURE_AGGS = "feature_aggs";

// ======================================
// Used in almost all components
// ======================================
public static final String MODEL_ID_KEY = "model_id";
public static final String DETECTOR_ID_KEY = "detector_id";
public static final String ENTITY_KEY = "entity";

// ======================================
// Used in toXContent
// ======================================
public static final String RCF_SCORE_JSON_KEY = "rCFScore";
public static final String ID_JSON_KEY = "adID";
public static final String FEATURE_JSON_KEY = "features";
public static final String CONFIDENCE_JSON_KEY = "confidence";
public static final String ANOMALY_GRADE_JSON_KEY = "anomalyGrade";
public static final String QUEUE_JSON_KEY = "queue";
public static final String START_JSON_KEY = "start";
public static final String END_JSON_KEY = "end";
public static final String VALUE_JSON_KEY = "value";
public static final String ENTITIES_JSON_KEY = "entities";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.InputStreamStreamInput;
import org.opensearch.common.io.stream.OutputStreamStreamOutput;
Expand Down Expand Up @@ -103,9 +103,9 @@ public ActionRequestValidationException validate() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ID_JSON_KEY, adID);
builder.field(CommonMessageAttributes.START_JSON_KEY, start);
builder.field(CommonMessageAttributes.END_JSON_KEY, end);
builder.field(CommonName.ID_JSON_KEY, adID);
builder.field(CommonName.START_JSON_KEY, start);
builder.field(CommonName.END_JSON_KEY, end);
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.nodes.BaseNodesRequest;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
Expand Down Expand Up @@ -84,7 +84,7 @@ public ActionRequestValidationException validate() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ID_JSON_KEY, adID);
builder.field(CommonName.ID_JSON_KEY, adID);
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -75,7 +75,7 @@ public ActionRequestValidationException validate() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ID_JSON_KEY, adID);
builder.field(CommonName.ID_JSON_KEY, adID);
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -107,9 +107,9 @@ public ActionRequestValidationException validate() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ID_JSON_KEY, adID);
builder.field(CommonMessageAttributes.MODEL_ID_JSON_KEY, modelID);
builder.startArray(CommonMessageAttributes.FEATURE_JSON_KEY);
builder.field(CommonName.ID_JSON_KEY, adID);
builder.field(CommonName.MODEL_ID_KEY, modelID);
builder.startArray(CommonName.FEATURE_JSON_KEY);
for (double feature : features) {
builder.value(feature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.InputStreamStreamInput;
import org.opensearch.common.io.stream.OutputStreamStreamOutput;
Expand Down Expand Up @@ -87,7 +87,7 @@ public ActionRequestValidationException validate() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ID_JSON_KEY, adID);
builder.field(CommonName.ID_JSON_KEY, adID);
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -99,9 +99,9 @@ public ActionRequestValidationException validate() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ID_JSON_KEY, adID);
builder.field(CommonMessageAttributes.MODEL_ID_JSON_KEY, modelID);
builder.field(CommonMessageAttributes.RCF_SCORE_JSON_KEY, rcfScore);
builder.field(CommonName.ID_JSON_KEY, adID);
builder.field(CommonName.MODEL_ID_KEY, modelID);
builder.field(CommonName.RCF_SCORE_JSON_KEY, rcfScore);
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.io.IOException;

import org.opensearch.action.ActionResponse;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.ToXContentObject;
Expand Down Expand Up @@ -67,8 +67,8 @@ public void writeTo(StreamOutput out) throws IOException {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonMessageAttributes.ANOMALY_GRADE_JSON_KEY, anomalyGrade);
builder.field(CommonMessageAttributes.CONFIDENCE_JSON_KEY, confidence);
builder.field(CommonName.ANOMALY_GRADE_JSON_KEY, anomalyGrade);
builder.field(CommonName.CONFIDENCE_JSON_KEY, confidence);
builder.endObject();
return builder;
}
Expand Down
32 changes: 26 additions & 6 deletions src/test/java/org/opensearch/ad/AbstractADTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
Expand All @@ -57,9 +59,11 @@
import org.opensearch.ad.model.AnomalyDetectorJob;
import org.opensearch.ad.model.AnomalyResult;
import org.opensearch.ad.model.DetectorInternalState;
import org.opensearch.ad.model.Entity;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.http.HttpRequest;
Expand Down Expand Up @@ -212,7 +216,7 @@ protected static void setUpThreadPool(String name) {
AnomalyDetectorPlugin.AD_THREAD_POOL_NAME,
1,
1000,
"opendistro.ad." + AnomalyDetectorPlugin.AD_THREAD_POOL_NAME
"opensearch.ad." + AnomalyDetectorPlugin.AD_THREAD_POOL_NAME
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
)
);
}
Expand All @@ -223,17 +227,33 @@ protected static void tearDownThreadPool() {
threadPool = null;
}

public void setupTestNodes(Settings settings, TransportInterceptor transportInterceptor) {
/**
*
* @param transportInterceptor Interceptor to for transport requests. Used
* to mock transport layer.
* @param nodeSettings node override of setting
* @param setting the supported setting set.
*/
public void setupTestNodes(TransportInterceptor transportInterceptor, final Settings nodeSettings, Setting<?>... setting) {
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
nodesCount = randomIntBetween(2, 10);
testNodes = new FakeNode[nodesCount];
Set<Setting<?>> settingSet = new HashSet<>(Arrays.asList(setting));
for (int i = 0; i < testNodes.length; i++) {
testNodes[i] = new FakeNode("node" + i, threadPool, settings, transportInterceptor);
testNodes[i] = new FakeNode("node" + i, threadPool, nodeSettings, settingSet, transportInterceptor);
}
FakeNode.connectNodes(testNodes);
}

public void setupTestNodes(Settings settings) {
setupTestNodes(settings, TransportService.NOOP_TRANSPORT_INTERCEPTOR);
public void setupTestNodes(Setting<?>... setting) {
setupTestNodes(TransportService.NOOP_TRANSPORT_INTERCEPTOR, Settings.EMPTY, setting);
}

public void setupTestNodes(Settings nodeSettings) {
setupTestNodes(TransportService.NOOP_TRANSPORT_INTERCEPTOR, nodeSettings);
}

public void setupTestNodes(TransportInterceptor transportInterceptor) {
setupTestNodes(transportInterceptor, Settings.EMPTY);
}

public void tearDownTestNodes() {
Expand Down Expand Up @@ -340,7 +360,7 @@ public HttpRequest releaseAndCopy() {
}, null);
}

protected boolean areEqualWithArrayValue(Map<String, double[]> first, Map<String, double[]> second) {
protected boolean areEqualWithArrayValue(Map<Entity, double[]> first, Map<Entity, double[]> second) {
if (first.size() != second.size()) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/opensearch/ad/transport/DeleteTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import org.opensearch.ad.AbstractADTest;
import org.opensearch.ad.common.exception.JsonPathNotFoundException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.ad.util.DiscoveryNodeFilterer;
import org.opensearch.client.Client;
import org.opensearch.cluster.ClusterName;
Expand Down Expand Up @@ -141,7 +141,7 @@ public void setUp() throws Exception {
transportService = mock(TransportService.class);
threadPool = mock(ThreadPool.class);
actionFilters = mock(ActionFilters.class);
Settings settings = Settings.builder().put("opendistro.anomaly_detection.request_timeout", TimeValue.timeValueSeconds(10)).build();
Settings settings = Settings.builder().put("plugins.anomaly_detection.request_timeout", TimeValue.timeValueSeconds(10)).build();
task = mock(Task.class);
when(task.getId()).thenReturn(1000L);
client = mock(Client.class);
Expand Down Expand Up @@ -201,7 +201,7 @@ public <R extends ToXContent> void testJsonRequestTemplate(R request, Supplier<S
request.toXContent(builder, ToXContent.EMPTY_PARAMS);

String json = Strings.toString(builder);
assertEquals(JsonDeserializer.getTextValue(json, CommonMessageAttributes.ID_JSON_KEY), requestSupplier.get());
assertEquals(JsonDeserializer.getTextValue(json, CommonName.ID_JSON_KEY), requestSupplier.get());
}

public void testJsonRequestStopDetector() throws IOException, JsonPathNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import org.opensearch.ad.cluster.HashRing;
import org.opensearch.ad.common.exception.AnomalyDetectionException;
import org.opensearch.ad.common.exception.JsonPathNotFoundException;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.ad.ml.ModelManager;
import org.opensearch.ad.ml.ModelPartitioner;
import org.opensearch.cluster.node.DiscoveryNode;
Expand Down Expand Up @@ -313,7 +313,7 @@ public String executor() {
}

public void testGetRemoteNormalResponse() {
setupTestNodes(Settings.EMPTY, normalTransportInterceptor);
setupTestNodes(normalTransportInterceptor, Settings.EMPTY);
try {
TransportService realTransportService = testNodes[0].transportService;
clusterService = testNodes[0].clusterService;
Expand Down Expand Up @@ -341,7 +341,7 @@ public void testGetRemoteNormalResponse() {
}

public void testGetRemoteFailureResponse() {
setupTestNodes(Settings.EMPTY, failureTransportInterceptor);
setupTestNodes(failureTransportInterceptor, Settings.EMPTY);
try {
TransportService realTransportService = testNodes[0].transportService;
clusterService = testNodes[0].clusterService;
Expand Down Expand Up @@ -376,7 +376,7 @@ public void testResponseToXContent() throws IOException, JsonPathNotFoundExcepti
public void testRequestToXContent() throws IOException, JsonPathNotFoundException {
RCFPollingRequest response = new RCFPollingRequest(detectorId);
String json = TestHelpers.xContentBuilderToString(response.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
assertEquals(detectorId, JsonDeserializer.getTextValue(json, CommonMessageAttributes.ID_JSON_KEY));
assertEquals(detectorId, JsonDeserializer.getTextValue(json, CommonName.ID_JSON_KEY));
}

public void testNullDetectorId() {
Expand Down
10 changes: 3 additions & 7 deletions src/test/java/org/opensearch/ad/transport/RCFResultTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.opensearch.ad.common.exception.JsonPathNotFoundException;
import org.opensearch.ad.common.exception.LimitExceededException;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonMessageAttributes;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.ad.ml.ModelManager;
import org.opensearch.ad.ml.RcfResult;
import org.opensearch.common.Strings;
Expand Down Expand Up @@ -194,12 +194,8 @@ public void testJsonRequest() throws IOException, JsonPathNotFoundException {
request.toXContent(builder, ToXContent.EMPTY_PARAMS);

String json = Strings.toString(builder);
assertEquals(JsonDeserializer.getTextValue(json, CommonMessageAttributes.ID_JSON_KEY), request.getAdID());
assertArrayEquals(
JsonDeserializer.getDoubleArrayValue(json, CommonMessageAttributes.FEATURE_JSON_KEY),
request.getFeatures(),
0.001
);
assertEquals(JsonDeserializer.getTextValue(json, CommonName.ID_JSON_KEY), request.getAdID());
assertArrayEquals(JsonDeserializer.getDoubleArrayValue(json, CommonName.FEATURE_JSON_KEY), request.getFeatures(), 0.001);
}

@SuppressWarnings("unchecked")
Expand Down
Loading