Search in sources :

Example 1 with Flow

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

the class CountsTest method run.

@Test
void run() throws Exception {
    for (int i = 0; i < 28; i++) {
        executionRepository.save(ElasticSearchExecutionRepositoryTest.builder(i < 5 ? State.Type.RUNNING : (i < 8 ? State.Type.FAILED : State.Type.SUCCESS), i < 4 ? "first" : (i < 10 ? "second" : "third")).build());
    }
    RunContext runContext = runContextFactory.of(ImmutableMap.of("namespace", "io.kestra.unittest"));
    // matching one
    Counts.Output run = Counts.builder().flows(List.of(new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "first"), new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "second"), new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "third"))).expression("{{ count >= 5 }}").startDate("{{ now() | dateAdd (-30, 'DAYS') }}").endDate("{{ now() }}").build().run(runContext);
    assertThat(run.getResults().size(), is(2));
    assertThat(run.getResults().stream().filter(f -> f.getFlowId().equals("second")).count(), is(1L));
    assertThat(run.getResults().stream().filter(f -> f.getFlowId().equals("third")).count(), is(1L));
    // add state filter no result
    run = Counts.builder().flows(List.of(new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "first"), new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "second"), new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "third"))).states(List.of(State.Type.RUNNING)).expression("{{ count >= 5 }}").build().run(runContext);
    assertThat(run.getResults().size(), is(0));
    // non matching entry
    run = Counts.builder().flows(List.of(new Flow("io.kestra.test", "missing"), new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "second"), new Flow(ElasticSearchExecutionRepositoryTest.NAMESPACE, "third"))).expression("{{ count == 0 }}").build().run(runContext);
    assertThat(run.getResults().size(), is(1));
    assertThat(run.getResults().stream().filter(f -> f.getFlowId().equals("missing")).count(), is(1L));
}
Also used : RunContext(io.kestra.core.runners.RunContext) Flow(io.kestra.core.models.executions.statistics.Flow) ElasticSearchExecutionRepositoryTest(io.kestra.repository.elasticsearch.ElasticSearchExecutionRepositoryTest) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 2 with Flow

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

the class ElasticSearchExecutionRepository method executionCounts.

@Override
public List<ExecutionCount> executionCounts(List<Flow> flows, String query, ZonedDateTime startDate, ZonedDateTime endDate) {
    if (startDate == null) {
        startDate = ZonedDateTime.now().minusDays(30);
    }
    if (endDate == null) {
        endDate = ZonedDateTime.now();
    }
    SearchSourceBuilder sourceBuilder = this.searchSource(this.dateFilters(query, startDate, endDate), Optional.of(Collections.singletonList(AggregationBuilders.filters("FILTERS", flows.stream().map(flow -> new FiltersAggregator.KeyedFilter(flow.getNamespace() + "/" + flow.getFlowId(), QueryBuilders.boolQuery().must(QueryBuilders.termQuery("namespace", flow.getNamespace())).must(QueryBuilders.termQuery("flowId", flow.getFlowId())))).toArray(FiltersAggregator.KeyedFilter[]::new)))), null);
    try {
        SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        List<ExecutionCount> result = new ArrayList<>();
        ((ParsedFilters) searchResponse.getAggregations().get("FILTERS")).getBuckets().forEach(filtersBuckets -> {
            final String key = filtersBuckets.getKeyAsString();
            result.add(new ExecutionCount(key.split("/")[0], key.split("/")[1], filtersBuckets.getDocCount()));
        });
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) SearchRequest(org.opensearch.action.search.SearchRequest) ExecutionCount(io.kestra.core.models.executions.statistics.ExecutionCount) FiltersAggregator(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

Flow (io.kestra.core.models.executions.statistics.Flow)2 Execution (io.kestra.core.models.executions.Execution)1 TaskRun (io.kestra.core.models.executions.TaskRun)1 DailyExecutionStatistics (io.kestra.core.models.executions.statistics.DailyExecutionStatistics)1 ExecutionCount (io.kestra.core.models.executions.statistics.ExecutionCount)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 RunContext (io.kestra.core.runners.RunContext)1 ExecutorsUtils (io.kestra.core.utils.ExecutorsUtils)1 ElasticSearchExecutionRepositoryTest (io.kestra.repository.elasticsearch.ElasticSearchExecutionRepositoryTest)1 Nullable (io.micronaut.core.annotation.Nullable)1 Pageable (io.micronaut.data.model.Pageable)1 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)1 Inject (jakarta.inject.Inject)1 Singleton (jakarta.inject.Singleton)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 LocalDate (java.time.LocalDate)1