Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Jackie Han <[email protected]>
  • Loading branch information
jackiehanyang committed Jan 23, 2025
1 parent b131f9a commit 9fced72
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 52 deletions.
50 changes: 28 additions & 22 deletions src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,23 @@ private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<
}

private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<Feature> features, boolean useDateNanos)
throws IOException {
throws IOException {
return createIndexAndGetAnomalyDetector(indexName, features, useDateNanos, false);
}

private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<Feature> features, boolean useDateNanos,
boolean useFlattenResultIndex) throws IOException {
private AnomalyDetector createIndexAndGetAnomalyDetector(
String indexName,
List<Feature> features,
boolean useDateNanos,
boolean useFlattenResultIndex
) throws IOException {
TestHelpers.createIndexWithTimeField(client(), indexName, TIME_FIELD, useDateNanos);
String testIndexData = "{\"keyword-field\": \"field-1\", \"ip-field\": \"1.2.3.4\", \"timestamp\": 1}";
TestHelpers.ingestDataToIndex(client(), indexName, TestHelpers.toHttpEntity(testIndexData));

AnomalyDetector detector = useFlattenResultIndex
? TestHelpers.randomAnomalyDetectorWithFlattenResultIndex(TIME_FIELD, indexName, features)
: TestHelpers.randomAnomalyDetector(TIME_FIELD, indexName, features);
? TestHelpers.randomAnomalyDetectorWithFlattenResultIndex(TIME_FIELD, indexName, features)
: TestHelpers.randomAnomalyDetector(TIME_FIELD, indexName, features);

return detector;
}
Expand Down Expand Up @@ -190,40 +194,42 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception {
}

public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception {
AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME,
ImmutableList.of(TestHelpers.randomFeature(true)), false, true);
AnomalyDetector detector = createIndexAndGetAnomalyDetector(
INDEX_NAME,
ImmutableList.of(TestHelpers.randomFeature(true)),
false,
true
);

// test behavior when AD is disabled
updateClusterSettings(ADEnabledSetting.AD_ENABLED, false);
Exception ex = expectThrows(
ResponseException.class,
() -> TestHelpers
.makeRequest(
client(),
"POST",
TestHelpers.AD_BASE_DETECTORS_URI,
ImmutableMap.of(),
TestHelpers.toHttpEntity(detector),
null
)
ResponseException.class,
() -> TestHelpers
.makeRequest(
client(),
"POST",
TestHelpers.AD_BASE_DETECTORS_URI,
ImmutableMap.of(),
TestHelpers.toHttpEntity(detector),
null
)
);
assertThat(ex.getMessage(), containsString(ADCommonMessages.DISABLED_ERR_MSG));

// test behavior when AD is enabled
updateClusterSettings(ADEnabledSetting.AD_ENABLED, true);
Response response = TestHelpers
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
Map<String, Object> responseMap = entityAsMap(response);
String id = (String) responseMap.get("_id");
int version = (int) responseMap.get("_version");
assertNotEquals("response is missing Id", AnomalyDetector.NO_ID, id);
assertTrue("incorrect version", version > 0);
// ensure the flattened result index was created
String expectedFlattenedIndex = String.format(
"opensearch-ad-plugin-result-test_flattened_%s",
id.toLowerCase(Locale.ROOT)
);
String expectedFlattenedIndex = String
.format(Locale.ROOT, "opensearch-ad-plugin-result-test_flattened_%s", id.toLowerCase(Locale.ROOT));
boolean indexExists = indexExists(expectedFlattenedIndex);
assertTrue(indexExists);
}
Expand Down
61 changes: 31 additions & 30 deletions src/test/java/org/opensearch/timeseries/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,37 +511,38 @@ public static AnomalyDetector randomAnomalyDetector(String timefield, String ind
);
}

public static AnomalyDetector randomAnomalyDetectorWithFlattenResultIndex(String timefield, String indexName, List<Feature> features) throws IOException {
public static AnomalyDetector randomAnomalyDetectorWithFlattenResultIndex(String timefield, String indexName, List<Feature> features)
throws IOException {
return new AnomalyDetector(
randomAlphaOfLength(10),
randomLong(),
randomAlphaOfLength(20),
randomAlphaOfLength(30),
timefield,
ImmutableList.of(indexName.toLowerCase(Locale.ROOT)),
features,
randomQuery(),
randomIntervalTimeConfiguration(),
randomIntervalTimeConfiguration(),
randomIntBetween(1, TimeSeriesSettings.MAX_SHINGLE_SIZE),
null,
randomInt(),
Instant.now(),
null,
randomUser(),
ADCommonName.CUSTOM_RESULT_INDEX_PREFIX + "test",
TestHelpers.randomImputationOption(features),
// timeDecay (reverse of recencyEmphasis) should be less than 1.
// so we start with 2.
randomIntBetween(2, 10000),
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
randomIntBetween(1, 1000),
null,
null,
null,
null,
true,
Instant.now()
randomAlphaOfLength(10),
randomLong(),
randomAlphaOfLength(20),
randomAlphaOfLength(30),
timefield,
ImmutableList.of(indexName.toLowerCase(Locale.ROOT)),
features,
randomQuery(),
randomIntervalTimeConfiguration(),
randomIntervalTimeConfiguration(),
randomIntBetween(1, TimeSeriesSettings.MAX_SHINGLE_SIZE),
null,
randomInt(),
Instant.now(),
null,
randomUser(),
ADCommonName.CUSTOM_RESULT_INDEX_PREFIX + "test",
TestHelpers.randomImputationOption(features),
// timeDecay (reverse of recencyEmphasis) should be less than 1.
// so we start with 2.
randomIntBetween(2, 10000),
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
randomIntBetween(1, 1000),
null,
null,
null,
null,
true,
Instant.now()
);
}

Expand Down

0 comments on commit 9fced72

Please sign in to comment.