Search in sources :

Example 1 with DailyExecutionStatistics

use of io.kestra.core.models.executions.statistics.DailyExecutionStatistics 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)

Example 2 with DailyExecutionStatistics

use of io.kestra.core.models.executions.statistics.DailyExecutionStatistics in project kestra by kestra-io.

the class ElasticSearchExecutionRepositoryTest method dailyGroupByFlowStatistics.

@Test
void dailyGroupByFlowStatistics() {
    for (int i = 0; i < 28; i++) {
        executionRepository.save(builder(i < 5 ? State.Type.RUNNING : (i < 8 ? State.Type.FAILED : State.Type.SUCCESS), i < 15 ? null : "second").build());
    }
    Map<String, Map<String, List<DailyExecutionStatistics>>> result = executionRepository.dailyGroupByFlowStatistics("*", ZonedDateTime.now().minusDays(10), ZonedDateTime.now(), false);
    assertThat(result.size(), is(1));
    assertThat(result.get("io.kestra.unittest").size(), is(2));
    DailyExecutionStatistics full = result.get("io.kestra.unittest").get(FLOW).get(10);
    DailyExecutionStatistics second = result.get("io.kestra.unittest").get("second").get(10);
    assertThat(full.getDuration().getAvg().getSeconds(), greaterThan(0L));
    assertThat(full.getExecutionCounts().size(), is(9));
    assertThat(full.getExecutionCounts().get(State.Type.FAILED), is(3L));
    assertThat(full.getExecutionCounts().get(State.Type.RUNNING), is(5L));
    assertThat(full.getExecutionCounts().get(State.Type.SUCCESS), is(7L));
    assertThat(full.getExecutionCounts().get(State.Type.CREATED), is(0L));
    assertThat(second.getDuration().getAvg().getSeconds(), greaterThan(0L));
    assertThat(second.getExecutionCounts().size(), is(9));
    assertThat(second.getExecutionCounts().get(State.Type.SUCCESS), is(13L));
    assertThat(second.getExecutionCounts().get(State.Type.CREATED), is(0L));
    result = executionRepository.dailyGroupByFlowStatistics("*", ZonedDateTime.now().minusDays(10), ZonedDateTime.now(), true);
    assertThat(result.size(), is(1));
    assertThat(result.get("io.kestra.unittest").size(), is(1));
    full = result.get("io.kestra.unittest").get("*").get(10);
    assertThat(full.getDuration().getAvg().getSeconds(), greaterThan(0L));
    assertThat(full.getExecutionCounts().size(), is(9));
    assertThat(full.getExecutionCounts().get(State.Type.FAILED), is(3L));
    assertThat(full.getExecutionCounts().get(State.Type.RUNNING), is(5L));
    assertThat(full.getExecutionCounts().get(State.Type.SUCCESS), is(20L));
    assertThat(full.getExecutionCounts().get(State.Type.CREATED), is(0L));
}
Also used : DailyExecutionStatistics(io.kestra.core.models.executions.statistics.DailyExecutionStatistics) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 3 with DailyExecutionStatistics

use of io.kestra.core.models.executions.statistics.DailyExecutionStatistics in project kestra by kestra-io.

the class ElasticSearchExecutionRepository method dailyGroupByFlowStatistics.

@Override
public Map<String, Map<String, List<DailyExecutionStatistics>>> dailyGroupByFlowStatistics(@Nullable String query, @Nullable ZonedDateTime startDate, @Nullable ZonedDateTime endDate, boolean groupByNamespaceOnly) {
    if (startDate == null) {
        startDate = ZonedDateTime.now().minusDays(30);
    }
    if (endDate == null) {
        endDate = ZonedDateTime.now();
    }
    TermsAggregationBuilder agg = AggregationBuilders.terms(NAMESPACE_AGG).size(groupByNamespaceOnly ? 25 : 10000).field("namespace").subAggregation(groupByNamespaceOnly ? dailyExecutionStatisticsFinalAgg(startDate, endDate, false) : AggregationBuilders.terms(FLOW_AGG).size(10000).field("flowId").subAggregation(dailyExecutionStatisticsFinalAgg(startDate, endDate, false)));
    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);
        Map<String, Map<String, List<DailyExecutionStatistics>>> result = new HashMap<>();
        ((ParsedStringTerms) searchResponse.getAggregations().get(NAMESPACE_AGG)).getBuckets().forEach(namespaceBucket -> {
            final String namespace = namespaceBucket.getKeyAsString();
            if (groupByNamespaceOnly) {
                this.parseDateAgg(namespaceBucket, result, namespace, "*");
            } else {
                ((ParsedStringTerms) namespaceBucket.getAggregations().get(FLOW_AGG)).getBuckets().forEach(flowBucket -> {
                    final String flowId = flowBucket.getKeyAsString();
                    this.parseDateAgg(flowBucket, result, namespace, flowId);
                });
            }
        });
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IOException(java.io.IOException) DailyExecutionStatistics(io.kestra.core.models.executions.statistics.DailyExecutionStatistics) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with DailyExecutionStatistics

use of io.kestra.core.models.executions.statistics.DailyExecutionStatistics 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;
}
Also used : TaskRun(io.kestra.core.models.executions.TaskRun) RequestOptions(org.opensearch.client.RequestOptions) java.util(java.util) ExecutionRepositoryInterface(io.kestra.core.repositories.ExecutionRepositoryInterface) Flow(io.kestra.core.models.executions.statistics.Flow) ParsedStringTerms(org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms) ZonedDateTime(java.time.ZonedDateTime) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) DateHistogramInterval(org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval) ExecutionCount(io.kestra.core.models.executions.statistics.ExecutionCount) ParsedDateHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram) State(io.kestra.core.models.flows.State) LongBounds(org.opensearch.search.aggregations.bucket.histogram.LongBounds) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) FiltersAggregator(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator) Nullable(io.micronaut.core.annotation.Nullable) Duration(java.time.Duration) Pageable(io.micronaut.data.model.Pageable) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) ParsedFilters(org.opensearch.search.aggregations.bucket.filter.ParsedFilters) QueryBuilders(org.opensearch.index.query.QueryBuilders) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) TopHits(org.opensearch.search.aggregations.metrics.TopHits) Singleton(jakarta.inject.Singleton) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) ScoreMode(org.apache.lucene.search.join.ScoreMode) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) DailyExecutionStatistics(io.kestra.core.models.executions.statistics.DailyExecutionStatistics) ZoneId(java.time.ZoneId) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) ParsedStats(org.opensearch.search.aggregations.metrics.ParsedStats) ArrayListTotal(io.kestra.core.repositories.ArrayListTotal) ExecutorsUtils(io.kestra.core.utils.ExecutorsUtils) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) IndexSettings(org.opensearch.index.IndexSettings) ModelValidator(io.kestra.core.models.validations.ModelValidator) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) Inject(jakarta.inject.Inject) ParsedNested(org.opensearch.search.aggregations.bucket.nested.ParsedNested) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) DailyExecutionStatistics(io.kestra.core.models.executions.statistics.DailyExecutionStatistics)

Aggregations

DailyExecutionStatistics (io.kestra.core.models.executions.statistics.DailyExecutionStatistics)4 IOException (java.io.IOException)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)3 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)3 LocalDate (java.time.LocalDate)2 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)2 DateHistogramAggregationBuilder (org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)2 Execution (io.kestra.core.models.executions.Execution)1 TaskRun (io.kestra.core.models.executions.TaskRun)1 ExecutionCount (io.kestra.core.models.executions.statistics.ExecutionCount)1 Flow (io.kestra.core.models.executions.statistics.Flow)1 State (io.kestra.core.models.flows.State)1 ModelValidator (io.kestra.core.models.validations.ModelValidator)1 ArrayListTotal (io.kestra.core.repositories.ArrayListTotal)1 ExecutionRepositoryInterface (io.kestra.core.repositories.ExecutionRepositoryInterface)1 ExecutorsUtils (io.kestra.core.utils.ExecutorsUtils)1 Nullable (io.micronaut.core.annotation.Nullable)1 Pageable (io.micronaut.data.model.Pageable)1