use of org.opensearch.ad.constant.CommonName.AGG_NAME_MIN_TIME in project anomaly-detection by opensearch-project.
the class ADBatchTaskRunner method getDateRangeOfSourceData.
private void getDateRangeOfSourceData(ADTask adTask, BiConsumer<Long, Long> consumer, ActionListener<String> internalListener) {
String taskId = adTask.getTaskId();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().aggregation(AggregationBuilders.min(AGG_NAME_MIN_TIME).field(adTask.getDetector().getTimeField())).aggregation(AggregationBuilders.max(AGG_NAME_MAX_TIME).field(adTask.getDetector().getTimeField())).size(0);
if (adTask.getEntity() != null && adTask.getEntity().getAttributes().size() > 0) {
BoolQueryBuilder query = new BoolQueryBuilder();
adTask.getEntity().getAttributes().entrySet().forEach(entity -> query.filter(new TermQueryBuilder(entity.getKey(), entity.getValue())));
searchSourceBuilder.query(query);
}
SearchRequest request = new SearchRequest().indices(adTask.getDetector().getIndices().toArray(new String[0])).source(searchSourceBuilder);
client.search(request, ActionListener.wrap(r -> {
InternalMin minAgg = r.getAggregations().get(AGG_NAME_MIN_TIME);
InternalMax maxAgg = r.getAggregations().get(AGG_NAME_MAX_TIME);
double minValue = minAgg.getValue();
double maxValue = maxAgg.getValue();
// If time field not exist or there is no value, will return infinity value
if (minValue == Double.POSITIVE_INFINITY) {
internalListener.onFailure(new ResourceNotFoundException(adTask.getDetectorId(), "There is no data in the time field"));
return;
}
long interval = ((IntervalTimeConfiguration) adTask.getDetector().getDetectionInterval()).toDuration().toMillis();
DetectionDateRange detectionDateRange = adTask.getDetectionDateRange();
long dataStartTime = detectionDateRange.getStartTime().toEpochMilli();
long dataEndTime = detectionDateRange.getEndTime().toEpochMilli();
long minDate = (long) minValue;
long maxDate = (long) maxValue;
if (minDate >= dataEndTime || maxDate <= dataStartTime) {
internalListener.onFailure(new ResourceNotFoundException(adTask.getDetectorId(), "There is no data in the detection date range"));
return;
}
if (minDate > dataStartTime) {
dataStartTime = minDate;
}
if (maxDate < dataEndTime) {
dataEndTime = maxDate;
}
// normalize start/end time to make it consistent with feature data agg result
dataStartTime = dataStartTime - dataStartTime % interval;
dataEndTime = dataEndTime - dataEndTime % interval;
logger.debug("adjusted date range: start: {}, end: {}, taskId: {}", dataStartTime, dataEndTime, taskId);
if ((dataEndTime - dataStartTime) < NUM_MIN_SAMPLES * interval) {
internalListener.onFailure(new AnomalyDetectionException("There is not enough data to train model").countedInStats(false));
return;
}
consumer.accept(dataStartTime, dataEndTime);
}, e -> {
internalListener.onFailure(e);
}));
}
Aggregations