use of io.kestra.core.models.executions.statistics.ExecutionCount in project kestra by kestra-io.
the class Counts method run.
@Override
public Output run(RunContext runContext) throws Exception {
Logger logger = runContext.logger();
ExecutionRepositoryInterface executionRepository = runContext.getApplicationContext().getBean(ExecutionRepositoryInterface.class);
String query = null;
if (this.states != null) {
query = "state.current:(" + this.states.stream().map(Enum::name).collect(Collectors.joining(" OR ")) + ")";
}
List<ExecutionCount> executionCounts = executionRepository.executionCounts(flows, query, startDate != null ? ZonedDateTime.parse(runContext.render(startDate)) : null, endDate != null ? ZonedDateTime.parse(runContext.render(endDate)) : null);
logger.trace("{} flows matching filters", executionCounts.size());
List<Result> count = executionCounts.stream().filter(throwPredicate(item -> runContext.render(this.expression, ImmutableMap.of("count", item.getCount().intValue())).equals("true"))).map(item -> Result.builder().namespace(item.getNamespace()).flowId(item.getFlowId()).count(item.getCount()).build()).collect(Collectors.toList());
logger.debug("{} flows matching the expression", count.size());
return Output.builder().results(count).build();
}
use of io.kestra.core.models.executions.statistics.ExecutionCount 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