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));
}
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);
}
}
Aggregations