forked from opensearch-project/anomaly-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the discrepancy between Profile API and real time tasks API
This PR fixes the discrepancy by querying the result index when the total updates is less than 32. We have done similar things in profile API so I refactored reusable code to ProfileUtil. We also cached whether we have queried the result index and won't repeatedly issue the extra query. Testing done: 1. repeated repro steps in opensearch-project#502 and verified the issue has been resolved. Signed-off-by: Kaituo Li <[email protected]>
- Loading branch information
Showing
16 changed files
with
646 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.ad; | ||
|
||
import org.opensearch.action.ActionListener; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.ad.constant.CommonName; | ||
import org.opensearch.ad.model.AnomalyDetector; | ||
import org.opensearch.ad.model.AnomalyResult; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.index.query.BoolQueryBuilder; | ||
import org.opensearch.index.query.ExistsQueryBuilder; | ||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
|
||
public class ProfileUtil { | ||
/** | ||
* Create search request to check if we have at least 1 anomaly score larger than 0 after AD job enabled time. | ||
* Note this function is only meant to check for status of real time analysis. | ||
* | ||
* @param detectorId detector id | ||
* @param enabledTime the time when AD job is enabled in milliseconds | ||
* @return the search request | ||
*/ | ||
private static SearchRequest createRealtimeInittedEverRequest(String detectorId, long enabledTime, String resultIndex) { | ||
BoolQueryBuilder filterQuery = new BoolQueryBuilder(); | ||
filterQuery.filter(QueryBuilders.termQuery(AnomalyResult.DETECTOR_ID_FIELD, detectorId)); | ||
filterQuery.filter(QueryBuilders.rangeQuery(AnomalyResult.EXECUTION_END_TIME_FIELD).gte(enabledTime)); | ||
filterQuery.filter(QueryBuilders.rangeQuery(AnomalyResult.ANOMALY_SCORE_FIELD).gt(0)); | ||
// Historical analysis result also stored in result index, which has non-null task_id. | ||
// For realtime detection result, we should filter task_id == null | ||
ExistsQueryBuilder taskIdExistsFilter = QueryBuilders.existsQuery(AnomalyResult.TASK_ID_FIELD); | ||
filterQuery.mustNot(taskIdExistsFilter); | ||
|
||
SearchSourceBuilder source = new SearchSourceBuilder().query(filterQuery).size(1); | ||
|
||
SearchRequest request = new SearchRequest(CommonName.ANOMALY_RESULT_INDEX_ALIAS); | ||
request.source(source); | ||
if (resultIndex != null) { | ||
request.indices(resultIndex); | ||
} | ||
return request; | ||
} | ||
|
||
public static void confirmDetectorRealtimeInitStatus( | ||
AnomalyDetector detector, | ||
long enabledTime, | ||
Client client, | ||
ActionListener<SearchResponse> listener | ||
) { | ||
SearchRequest searchLatestResult = createRealtimeInittedEverRequest( | ||
detector.getDetectorId(), | ||
enabledTime, | ||
detector.getResultIndex() | ||
); | ||
client.search(searchLatestResult, listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.