Search in sources :

Example 1 with ParsedDateHistogram

use of org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram 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);
    }
}
Also used : ParsedStats(org.opensearch.search.aggregations.metrics.ParsedStats) SearchRequest(org.opensearch.action.search.SearchRequest) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) ParsedDateHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram) ParsedNested(org.opensearch.search.aggregations.bucket.nested.ParsedNested) IOException(java.io.IOException) DailyExecutionStatistics(io.kestra.core.models.executions.statistics.DailyExecutionStatistics) ParsedStringTerms(org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms) LocalDate(java.time.LocalDate) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

DailyExecutionStatistics (io.kestra.core.models.executions.statistics.DailyExecutionStatistics)1 IOException (java.io.IOException)1 LocalDate (java.time.LocalDate)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)1 DateHistogramAggregationBuilder (org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)1 ParsedDateHistogram (org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram)1 NestedAggregationBuilder (org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)1 ParsedNested (org.opensearch.search.aggregations.bucket.nested.ParsedNested)1 ParsedStringTerms (org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms)1 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 ParsedStats (org.opensearch.search.aggregations.metrics.ParsedStats)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1