use of org.opensearch.ad.model.Feature in project anomaly-detection by opensearch-project.
the class ParseUtilsTests method testGenerateInternalFeatureQuery.
public void testGenerateInternalFeatureQuery() throws IOException {
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(null, Instant.now());
long startTime = randomLong();
long endTime = randomLong();
SearchSourceBuilder builder = ParseUtils.generateInternalFeatureQuery(detector, startTime, endTime, TestHelpers.xContentRegistry());
for (Feature feature : detector.getFeatureAttributes()) {
assertTrue(builder.toString().contains(feature.getId()));
}
}
use of org.opensearch.ad.model.Feature in project anomaly-detection by opensearch-project.
the class ParseUtilsTests method testGenerateInternalFeatureQueryTemplate.
public void testGenerateInternalFeatureQueryTemplate() throws IOException {
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(null, Instant.now());
String builder = ParseUtils.generateInternalFeatureQueryTemplate(detector, TestHelpers.xContentRegistry());
for (Feature feature : detector.getFeatureAttributes()) {
assertTrue(builder.contains(feature.getId()));
}
}
use of org.opensearch.ad.model.Feature in project anomaly-detection by opensearch-project.
the class ParseUtilsTests method testBatchFeatureQuery.
public void testBatchFeatureQuery() throws IOException {
String index = randomAlphaOfLength(5);
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
Feature feature1 = TestHelpers.randomFeature(true);
Feature feature2 = TestHelpers.randomFeature(false);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableList.of(index), ImmutableList.of(feature1, feature2), null, now, 1, false, null);
long startTime = now.minus(10, ChronoUnit.DAYS).toEpochMilli();
long endTime = now.plus(10, ChronoUnit.DAYS).toEpochMilli();
SearchSourceBuilder searchSourceBuilder = ParseUtils.batchFeatureQuery(detector, null, startTime, endTime, TestHelpers.xContentRegistry());
assertEquals("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"range\":{\"" + detector.getTimeField() + "\":{\"from\":" + startTime + ",\"to\":" + endTime + ",\"include_lower\":true,\"include_upper\":false,\"format\":\"epoch_millis\",\"boost\"" + ":1.0}}},{\"bool\":{\"must\":[{\"term\":{\"user\":{\"value\":\"kimchy\",\"boost\":1.0}}}],\"filter\":" + "[{\"term\":{\"tag\":{\"value\":\"tech\",\"boost\":1.0}}}],\"must_not\":[{\"range\":{\"age\":{\"from\":10," + "\"to\":20,\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}}],\"should\":[{\"term\":{\"tag\":" + "{\"value\":\"wow\",\"boost\":1.0}}},{\"term\":{\"tag\":{\"value\":\"elasticsearch\",\"boost\":1.0}}}]," + "\"adjust_pure_negative\":true,\"minimum_should_match\":\"1\",\"boost\":1.0}}],\"adjust_pure_negative" + "\":true,\"boost\":1.0}},\"aggregations\":{\"feature_aggs\":{\"composite\":{\"size\":10000,\"sources\":" + "[{\"date_histogram\":{\"date_histogram\":{\"field\":\"" + detector.getTimeField() + "\",\"missing_bucket\":false,\"order\":\"asc\"," + "\"fixed_interval\":\"60s\"}}}]},\"aggregations\":{\"" + feature1.getId() + "\":{\"value_count\":{\"field\":\"ok\"}}}}}}", searchSourceBuilder.toString());
}
use of org.opensearch.ad.model.Feature in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorTransportActionTests method testValidateAnomalyDetectorWithDuplicateFeatureNamesAndDuplicateAggregationNames.
@Test
public void testValidateAnomalyDetectorWithDuplicateFeatureNamesAndDuplicateAggregationNames() throws IOException {
Feature maxFeature = maxValueFeature(nameField, categoryField, nameField);
Feature maxFeatureTwo = maxValueFeature(nameField, categoryField, nameField);
AnomalyDetector anomalyDetector = TestHelpers.randomAnomalyDetector(timeField, "test-index", ImmutableList.of(maxFeature, maxFeatureTwo));
ingestTestDataValidate(anomalyDetector.getIndices().get(0), Instant.now().minus(1, ChronoUnit.DAYS), 1, "error");
ValidateAnomalyDetectorRequest request = new ValidateAnomalyDetectorRequest(anomalyDetector, ValidationAspect.DETECTOR.getName(), 5, 5, 5, new TimeValue(5_000L));
ValidateAnomalyDetectorResponse response = client().execute(ValidateAnomalyDetectorAction.INSTANCE, request).actionGet(5_000);
assertNotNull(response.getIssue());
assertTrue(response.getIssue().getMessage().contains("Detector has duplicate feature aggregation query names:"));
assertTrue(response.getIssue().getMessage().contains("Detector has duplicate feature names:"));
assertEquals(DetectorValidationIssueType.FEATURE_ATTRIBUTES, response.getIssue().getType());
assertEquals(ValidationAspect.DETECTOR, response.getIssue().getAspect());
}
use of org.opensearch.ad.model.Feature in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorTransportActionTests method testValidateAnomalyDetectorWithNonExistingFeatureField.
@Test
public void testValidateAnomalyDetectorWithNonExistingFeatureField() throws IOException {
Feature maxFeature = maxValueFeature(nameField, "non_existing_field", nameField);
AnomalyDetector anomalyDetector = TestHelpers.randomAnomalyDetector(timeField, "test-index", ImmutableList.of(maxFeature));
ingestTestDataValidate(anomalyDetector.getIndices().get(0), Instant.now().minus(1, ChronoUnit.DAYS), 1, "error");
ValidateAnomalyDetectorRequest request = new ValidateAnomalyDetectorRequest(anomalyDetector, ValidationAspect.DETECTOR.getName(), 5, 5, 5, new TimeValue(5_000L));
ValidateAnomalyDetectorResponse response = client().execute(ValidateAnomalyDetectorAction.INSTANCE, request).actionGet(5_000);
assertNotNull(response.getIssue());
assertEquals(DetectorValidationIssueType.FEATURE_ATTRIBUTES, response.getIssue().getType());
assertEquals(ValidationAspect.DETECTOR, response.getIssue().getAspect());
assertTrue(response.getIssue().getMessage().contains(CommonErrorMessages.FEATURE_WITH_EMPTY_DATA_MSG));
assertTrue(response.getIssue().getSubIssues().containsKey(maxFeature.getName()));
assertTrue(CommonErrorMessages.FEATURE_WITH_EMPTY_DATA_MSG.contains(response.getIssue().getSubIssues().get(maxFeature.getName())));
}
Aggregations