Skip to content

Commit

Permalink
Create Detector Workflow (#692)
Browse files Browse the repository at this point in the history
* Removed opensearchplugin and ran extension independently

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Initial create detector

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Initial draft for create detector workflow

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Removed empty constructor

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Removed comments

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Integrated create index with mapping

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Changed access specifier

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Parser error

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Added namedXContent for create detector

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Implemented create detector

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Formatted the code

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Updated build.gradle

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Migrate client transports to Apache HttpClient / Core 5.x

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Removed jackson dependency

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

* Changed dynamic mapping to string

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>
  • Loading branch information
owaiskazi19 authored Nov 7, 2022
1 parent a14b2f8 commit 5fdb72a
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 149 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ configurations.all {
resolutionStrategy {
force "joda-time:joda-time:2.10.13"
force "commons-logging:commons-logging:1.2"
force "org.apache.httpcomponents:httpcore:4.4.15"
force "org.apache.httpcomponents:httpcore5:5.1.4"
force "commons-codec:commons-codec:1.15"

force "org.mockito:mockito-core:2.25.0"
Expand Down Expand Up @@ -757,6 +757,8 @@ dependencies {
// implementation "org.opensearch:common-utils:${common_utils_version}"
implementation "org.opensearch:opensearch-sdk-java:1.0.0-SNAPSHOT"
implementation "org.opensearch.client:opensearch-rest-client:${opensearch_version}"
implementation "org.opensearch.client:opensearch-java:${opensearch_version}"
implementation "org.opensearch.client:opensearch-rest-client:${opensearch_version}"
implementation group: 'com.google.guava', name: 'guava', version:'31.0.1-jre'
implementation group: 'com.google.guava', name: 'failureaccess', version:'1.0.1'
implementation group: 'org.javassist', name: 'javassist', version:'3.28.0-GA'
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
import static java.util.Collections.unmodifiableList;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.opensearch.ad.rest.RestCreateDetectorAction;
import org.opensearch.ad.settings.AnomalyDetectorSettings;
import org.opensearch.ad.settings.EnabledSetting;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Setting;
import org.opensearch.sdk.Extension;
import org.opensearch.sdk.ExtensionRestHandler;
import org.opensearch.sdk.ExtensionSettings;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.SDKClient;
import org.opensearch.threadpool.ThreadPool;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -83,6 +88,11 @@ public List<Setting<?>> getSettings() {
);
}

@Override
public Collection<Object> createComponents(SDKClient sdkClient, ClusterService clusterService, ThreadPool threadPool) {
return null;
}

private static ExtensionSettings initializeSettings() throws IOException {
ExtensionSettings settings = Extension.readSettingsFromYaml(EXTENSION_SETTINGS_PATH);
if (settings == null || settings.getHostAddress() == null || settings.getHostPort() == null) {
Expand All @@ -91,6 +101,12 @@ private static ExtensionSettings initializeSettings() throws IOException {
return settings;
}

public OpenSearchClient getClient() {
SDKClient sdkClient = new SDKClient();
OpenSearchClient client = sdkClient.initializeClient("localhost", 9200);
return client;
}

public static void main(String[] args) throws IOException {
// Execute this extension by instantiating it and passing to ExtensionsRunner
ExtensionsRunner.run(new AnomalyDetectorExtension());
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/opensearch/ad/auth/UserIdentity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import java.util.Map;
import java.util.Objects;

import org.apache.http.util.EntityUtils;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.opensearch.client.Response;
import org.opensearch.common.Nullable;
import org.opensearch.common.Strings;
Expand Down Expand Up @@ -110,7 +111,7 @@ public UserIdentity(
* @param response The security plugin response.
* @throws IOException If there was an error receiving the response.
*/
public UserIdentity(final Response response) throws IOException {
public UserIdentity(final Response response) throws IOException, ParseException {
this(EntityUtils.toString(response.getEntity()));
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/opensearch/ad/model/AnomalyDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@
* in code rather than config it in anomaly-detection-state.json file.
*/
public class AnomalyDetector implements Writeable, ToXContentObject {

public static final String PARSE_FIELD_NAME = "AnomalyDetector";
public static final NamedXContentRegistry.Entry XCONTENT_REGISTRY = new NamedXContentRegistry.Entry(
AnomalyDetector.class,
new ParseField(PARSE_FIELD_NAME),
it -> parse(it)
);
public static final String NO_ID = "";
public static final String ANOMALY_DETECTORS_INDEX = ".opendistro-anomaly-detectors";
public static final String ANOMALY_DETECTORS_INDEX = "opendistro-anomaly-detectors";
public static final String TYPE = "_doc";
public static final String QUERY_PARAM_PERIOD_START = "period_start";
public static final String QUERY_PARAM_PERIOD_END = "period_end";
Expand Down
Loading

0 comments on commit 5fdb72a

Please sign in to comment.