Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Validation API Backend for AD everywhere #221

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f9a097d
starting validate API structure
Jul 10, 2020
ef8195b
starting validate method
Jul 14, 2020
faf0e73
pre validation steps begining
Jul 14, 2020
f8d6e88
working on feature aggregation
Jul 22, 2020
ffa20dd
works right now but random sampling is incosistent
Jul 27, 2020
8dcf2eb
this solved the asynch issues with search request and this works with…
Jul 28, 2020
2bbcb58
changed random sampling to nonrandom and no aggregation, filter query…
Jul 31, 2020
7dd7851
commit before switching to updated code
Aug 3, 2020
a0a5e82
merge conflict
Aug 3, 2020
6e46041
recomendation fully works now
Aug 5, 2020
890fa5b
fixed feature query validation bug
Aug 5, 2020
7afabb4
ready for PR
Aug 19, 2020
8ade35d
fixed style issue for build
Aug 19, 2020
e66d6a2
Merge branch 'master' into newbranch-validate-1.9
Aug 20, 2020
2f79363
fixed all changes from CR and added check for field type
Aug 31, 2020
2370d5e
Merge branch 'master' into newbranch-validate-1.9
Aug 31, 2020
4235849
style fixes
Aug 31, 2020
a05c272
added a datetimerange class plus some other fixes
Sep 1, 2020
4a523f8
creating new series of timerange with the DateTimeRange class
Sep 2, 2020
a2e56da
creating new series of timerange with the DateTimeRange class
Sep 2, 2020
143e5d8
style fix
Sep 2, 2020
3487c95
Merge pull request #1 from amitgalitz/newbranch-validate-1.9
amitgalitz Sep 2, 2020
ef0bddf
more unit tests added for models
Sep 4, 2020
e9fc63a
style fix
Sep 4, 2020
80c3a3e
Merge pull request #2 from amitgalitz/newbranch-validate-1.9
amitgalitz Sep 4, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import com.amazon.opendistroforelasticsearch.ad.rest.RestSearchAnomalyDetectorAction;
import com.amazon.opendistroforelasticsearch.ad.rest.RestSearchAnomalyResultAction;
import com.amazon.opendistroforelasticsearch.ad.rest.RestStatsAnomalyDetectorAction;
import com.amazon.opendistroforelasticsearch.ad.rest.RestValidateAnomalyDetectorAction;
import com.amazon.opendistroforelasticsearch.ad.settings.AnomalyDetectorSettings;
import com.amazon.opendistroforelasticsearch.ad.settings.EnabledSetting;
import com.amazon.opendistroforelasticsearch.ad.stats.ADStat;
Expand Down Expand Up @@ -210,6 +211,11 @@ public List<RestHandler> getRestHandlers(
anomalyDetectionIndices
);
RestSearchAnomalyDetectorAction searchAnomalyDetectorAction = new RestSearchAnomalyDetectorAction();
RestValidateAnomalyDetectorAction validateAnomalyDetectorAction = new RestValidateAnomalyDetectorAction(
settings,
anomalyDetectionIndices,
xContentRegistry
);
RestSearchAnomalyResultAction searchAnomalyResultAction = new RestSearchAnomalyResultAction();
RestDeleteAnomalyDetectorAction deleteAnomalyDetectorAction = new RestDeleteAnomalyDetectorAction(clusterService);
RestExecuteAnomalyDetectorAction executeAnomalyDetectorAction = new RestExecuteAnomalyDetectorAction(
Expand All @@ -232,6 +238,7 @@ public List<RestHandler> getRestHandlers(
.of(
restGetAnomalyDetectorAction,
restIndexAnomalyDetectorAction,
validateAnomalyDetectorAction,
searchAnomalyDetectorAction,
searchAnomalyResultAction,
deleteAnomalyDetectorAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class AnomalyDetector implements ToXContentObject {
private final Map<String, Object> uiMetadata;
private final Integer schemaVersion;
private final Instant lastUpdateTime;
private final Boolean validation;

/**
* Constructor function.
Expand Down Expand Up @@ -122,20 +123,62 @@ public AnomalyDetector(
Integer schemaVersion,
Instant lastUpdateTime
) {
if (Strings.isBlank(name)) {
throw new IllegalArgumentException("Detector name should be set");
}
if (timeField == null) {
throw new IllegalArgumentException("Time field should be set");
}
if (indices == null || indices.isEmpty()) {
throw new IllegalArgumentException("Indices should be set");
}
if (detectionInterval == null) {
throw new IllegalArgumentException("Detection interval should be set");
}
if (shingleSize != null && shingleSize < 1) {
throw new IllegalArgumentException("Shingle size must be a positive integer");
this(
detectorId,
version,
name,
description,
timeField,
indices,
features,
filterQuery,
detectionInterval,
windowDelay,
shingleSize,
uiMetadata,
schemaVersion,
lastUpdateTime,
false
);
}

public AnomalyDetector(
String detectorId,
Long version,
String name,
String description,
String timeField,
List<String> indices,
List<Feature> features,
QueryBuilder filterQuery,
TimeConfiguration detectionInterval,
TimeConfiguration windowDelay,
Integer shingleSize,
Map<String, Object> uiMetadata,
Integer schemaVersion,
Instant lastUpdateTime,
Boolean validation
) {
if (validation) {
if (indices == null || indices.isEmpty()) {
indices = null;
}
} else {
if (Strings.isBlank(name)) {
throw new IllegalArgumentException("Detector name should be set");
}
if (timeField == null) {
throw new IllegalArgumentException("Time field should be set");
}
if (indices == null || indices.isEmpty()) {
throw new IllegalArgumentException("Indices should be set");
}
if (detectionInterval == null) {
throw new IllegalArgumentException("Detection interval should be set");
}
if (shingleSize != null && shingleSize < 1) {
throw new IllegalArgumentException("Shingle size must be a positive integer");
}
}
this.detectorId = detectorId;
this.version = version;
Expand All @@ -151,6 +194,7 @@ public AnomalyDetector(
this.uiMetadata = uiMetadata;
this.schemaVersion = schemaVersion;
this.lastUpdateTime = lastUpdateTime;
this.validation = validation;
}

public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
Expand Down Expand Up @@ -253,7 +297,6 @@ public static AnomalyDetector parse(
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();

switch (fieldName) {
case NAME_FIELD:
name = parser.text();
Expand Down Expand Up @@ -327,6 +370,100 @@ public static AnomalyDetector parse(
);
}

public static AnomalyDetector parseValidation(XContentParser parser, String detectorId, Long version, Integer defaultShingleSize)
throws IOException {
String name = null;
String description = null;
String timeField = null;
List<String> indices = new ArrayList<String>();
QueryBuilder filterQuery = QueryBuilders.matchAllQuery();
TimeConfiguration detectionInterval = null;
TimeConfiguration windowDelay = null;
List<Feature> features = new ArrayList<>();
int schemaVersion = 0;
Map<String, Object> uiMetadata = null;
Instant lastUpdateTime = null;
Integer shingleSize = defaultShingleSize;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();

switch (fieldName) {
case NAME_FIELD:
name = parser.text();
break;
case DESCRIPTION_FIELD:
description = parser.text();
break;
case TIMEFIELD_FIELD:
timeField = parser.text();
break;
case INDICES_FIELD:
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser::getTokenLocation);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
indices.add(parser.text());
}
break;
case UI_METADATA_FIELD:
uiMetadata = parser.map();
break;
case SCHEMA_VERSION_FIELD:
schemaVersion = parser.intValue();
break;
case FILTER_QUERY_FIELD:
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
try {
filterQuery = parseInnerQueryBuilder(parser);
} catch (IllegalArgumentException e) {
if (!e.getMessage().contains("empty clause")) {
throw e;
}
}
break;
case DETECTION_INTERVAL_FIELD:
detectionInterval = TimeConfiguration.parse(parser);
break;
case FEATURE_ATTRIBUTES_FIELD:
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser::getTokenLocation);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
features.add(Feature.parse(parser));
}
break;
case WINDOW_DELAY_FIELD:
windowDelay = TimeConfiguration.parse(parser);
break;
case SHINGLE_SIZE_FIELD:
shingleSize = parser.intValue();
break;
case LAST_UPDATE_TIME_FIELD:
lastUpdateTime = ParseUtils.toInstant(parser);
break;
default:
parser.skipChildren();
break;
}
}
return new AnomalyDetector(
detectorId,
version,
name,
description,
timeField,
indices,
features,
filterQuery,
detectionInterval,
windowDelay,
shingleSize,
uiMetadata,
schemaVersion,
lastUpdateTime,
true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only place that the method is different from the parse method?

);
}

public SearchSourceBuilder generateFeatureQuery() {
SearchSourceBuilder generatedFeatureQuery = new SearchSourceBuilder().query(filterQuery);
if (this.getFeatureAttributes() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazon.opendistroforelasticsearch.ad.model;

import java.time.Instant;

/**
* A DateTimeRange is used to represent start and end time for a timeRange
*/
public class DateTimeRange {

private long start;
private long end;

public DateTimeRange(long start, long end) {
this.start = start;
this.end = end;
}

public static DateTimeRange rangeBasedOfInterval(long windowDelay, long intervalLength, int numOfIntervals) {
long dataEndTime = Instant.now().toEpochMilli() - windowDelay;
long dataStartTime = dataEndTime - ((long) (numOfIntervals) * intervalLength);
return new DateTimeRange(dataStartTime, dataEndTime);

}

public long getStart() {
return start;
}

public long getEnd() {
return end;
}

public void setStart(long start) {
this.start = start;
}

public void setEnd(long end) {
this.end = end;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazon.opendistroforelasticsearch.ad.model;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

public class ValidateResponse implements ToXContentObject {
private Map<String, List<String>> failures;
private Map<String, List<String>> suggestedChanges;

public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
return toXContent(builder, ToXContent.EMPTY_PARAMS);
}

public ValidateResponse() {
failures = null;
suggestedChanges = null;
}

public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
XContentBuilder xContentBuilder = builder.startObject();

xContentBuilder.startObject("failures");
for (String key : failures.keySet()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if failures or suggestedChanges are null?

xContentBuilder.field(key, failures.get(key));
}
xContentBuilder.endObject();

xContentBuilder.startObject("suggestedChanges");
for (String key : suggestedChanges.keySet()) {
xContentBuilder.field(key, suggestedChanges.get(key));
}
xContentBuilder.endObject();
return xContentBuilder.endObject();
}

public Map<String, List<String>> getFailures() {
return failures;
}

public Map<String, List<String>> getSuggestedChanges() {
return suggestedChanges;
}

public void setFailures(Map<String, List<String>> failures) {
this.failures = failures;
}

public void setSuggestedChanges(Map<String, List<String>> suggestedChanges) {
this.suggestedChanges = suggestedChanges;
}

}
Loading