use of org.opensearch.search.aggregations.metrics.ParsedStats in project kestra by kestra-io.
the class ElasticSearchExecutionRepository method dailyStatistics.
@Override
public List<DailyExecutionStatistics> dailyStatistics(@Nullable String query, @Nullable ZonedDateTime startDate, @Nullable ZonedDateTime endDate, boolean isTaskRun) {
if (startDate == null) {
startDate = ZonedDateTime.now().minusDays(30);
}
if (endDate == null) {
endDate = ZonedDateTime.now();
}
AggregationBuilder agg = dailyExecutionStatisticsFinalAgg(startDate, endDate, isTaskRun);
if (isTaskRun) {
agg = AggregationBuilders.nested(NESTED_AGG, "taskRunList").subAggregation(agg);
}
SearchSourceBuilder sourceBuilder = this.searchSource(this.dateFilters(query, startDate, endDate), Optional.of(Collections.singletonList(agg)), null);
try {
SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedDateHistogram groupAgg = isTaskRun ? ((ParsedNested) searchResponse.getAggregations().get(NESTED_AGG)).getAggregations().get(DATE_AGG) : searchResponse.getAggregations().get(DATE_AGG);
List<DailyExecutionStatistics> result = new ArrayList<>();
groupAgg.getBuckets().forEach(bucket -> {
ParsedStringTerms countAgg = bucket.getAggregations().get(COUNT_AGG);
ParsedStats durationAgg = bucket.getAggregations().get(DURATION_AGG);
final LocalDate currentStartDate = LocalDate.parse(bucket.getKeyAsString(), DateTimeFormatter.ISO_LOCAL_DATE);
result.add(dailyExecutionStatisticsMap(countAgg, durationAgg, currentStartDate));
});
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.opensearch.search.aggregations.metrics.ParsedStats in project kestra by kestra-io.
the class ElasticSearchExecutionRepository method parseDateAgg.
private <T extends Terms.Bucket> void parseDateAgg(T bucket, Map<String, Map<String, List<DailyExecutionStatistics>>> result, String namespace, String flowId) {
((ParsedDateHistogram) bucket.getAggregations().get(DATE_AGG)).getBuckets().forEach(dateBucket -> {
final LocalDate currentStartDate = LocalDate.parse(dateBucket.getKeyAsString(), DateTimeFormatter.ISO_LOCAL_DATE);
ParsedStringTerms countAgg = dateBucket.getAggregations().get(COUNT_AGG);
ParsedStats durationAgg = dateBucket.getAggregations().get(DURATION_AGG);
result.compute(namespace, (namespaceKey, namespaceMap) -> {
if (namespaceMap == null) {
namespaceMap = new HashMap<>();
}
namespaceMap.compute(flowId, (flowKey, flowList) -> {
if (flowList == null) {
flowList = new ArrayList<>();
}
flowList.add(dailyExecutionStatisticsMap(countAgg, durationAgg, currentStartDate));
return flowList;
});
return namespaceMap;
});
});
}
use of org.opensearch.search.aggregations.metrics.ParsedStats in project kestra by kestra-io.
the class ElasticSearchExecutionRepository method dailyExecutionStatisticsMap.
private static DailyExecutionStatistics dailyExecutionStatisticsMap(ParsedStringTerms countAgg, ParsedStats durationAgg, LocalDate startDate) {
DailyExecutionStatistics build = DailyExecutionStatistics.builder().startDate(startDate).duration(DailyExecutionStatistics.Duration.builder().avg(durationFromDouble(durationAgg.getAvg())).min(durationFromDouble(durationAgg.getMin())).max(durationFromDouble(durationAgg.getMax())).sum(durationFromDouble(durationAgg.getSum())).count(durationAgg.getCount()).build()).build();
countAgg.getBuckets().forEach(item -> build.getExecutionCounts().compute(State.Type.valueOf(item.getKeyAsString()), (type, current) -> item.getDocCount()));
return build;
}
Aggregations