private void cleanup(Observable<Execution> observable, String application, String type) {
List<Map> executions = observable.filter(filter).map(mapper).toList().toBlocking().single();
executions.sort(comparing(a -> (Long) Optional.ofNullable(a.get("startTime")).orElse(0L)));
if (executions.size() > threshold) {
executions.subList(0, (executions.size() - threshold)).forEach(it -> {
Long startTime = Optional.ofNullable((Long) it.get("startTime")).orElseGet(() -> (Long) it.get("buildTime"));
log.info("Deleting {} execution {} (startTime: {}, application: {}, pipelineConfigId: {}, status: {})", type, it.get("id"), startTime != null ? Instant.ofEpochMilli(startTime) : null, application, it.get("pipelineConfigId"), it.get("status"));
if (type.equals("orchestration")) {
executionRepository.delete(ORCHESTRATION, (String) it.get("id"));
registry.counter(deleteCountId.withTag("application", application)).increment();
} else {
throw new IllegalArgumentException(format("Unsupported type '%s'", type));
}
});
}
}
}