diff --git a/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java b/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java index b49ca83fb5a72a5fed39f3caa17cb8f9ad292da6..93229a561e24e00c063b13ad5ce0c3c6fb0bcb91 100644 --- a/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java +++ b/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java @@ -23,6 +23,8 @@ public interface IIocInstanceRepository { List<IocInstanceEntity> findLatestInstances( long templateId, String query, int page, int limit, Boolean sortByVersion); + long countAllLatestInstances(); + long countLatestInstancesForPaging(long templateId, String query); long countInstancesForOperation(final long operationId); @@ -43,8 +45,6 @@ public interface IIocInstanceRepository { boolean isOperationFinished(final Long operationId); - Long countAll(); - boolean hasInstanceRepositoryNewlyCreated(IocInstanceEntity instance); boolean instanceExists(long typeId, long gitRepoId, String configName); diff --git a/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java b/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java index b23e13028b7c44716a6b9d6fe4df90eac9c66704..71fc67a570ca9ebcdabc28a1599bccb4d461639f 100644 --- a/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java +++ b/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java @@ -38,6 +38,10 @@ public class IocInstanceRepository implements IIocInstanceRepository { em.merge(iocInstance); } + private static final String COUNT_OF_ALL_CURRENT_INSTANCES = + "SELECT COUNT(DISTINCT ie.gitProjectId) FROM IocInstanceEntity ie " + + " WHERE ie.iocTypeName NOT IN (SELECT i.name FROM IocTypeEntity i WHERE i.deleted = true)"; + private static final String LATEST_INSTANCES = "FROM IocInstanceEntity ie " + " WHERE (ie.gitProjectId, ie.createdAt) IN (SELECT f.gitProjectId, MAX(f.createdAt) " @@ -103,6 +107,11 @@ public class IocInstanceRepository implements IIocInstanceRepository { return dbQuery.setMaxResults(limit).setFirstResult(page * limit).getResultList(); } + @Override + public long countAllLatestInstances() { + return em.createQuery(COUNT_OF_ALL_CURRENT_INSTANCES, Long.class).getSingleResult(); + } + @Override public long countLatestInstancesForPaging(long templateId, String query) { String queryString = "SELECT COUNT(*) " + LATEST_INSTANCES; @@ -274,11 +283,6 @@ public class IocInstanceRepository implements IIocInstanceRepository { .orElse(null); } - @Override - public Long countAll() { - return em.createQuery("SELECT COUNT(*) FROM IocInstanceEntity", Long.class).getSingleResult(); - } - private <T, R> TypedQuery<T> pagingQuery( CriteriaBuilder cb, CriteriaQuery<T> select, diff --git a/src/main/java/eu/ess/ics/ce/template/service/internal/metrics/MetricsService.java b/src/main/java/eu/ess/ics/ce/template/service/internal/metrics/MetricsService.java index 3484c438c2c86787d0f5287d8ba208b5e247e87e..ab3cc3f677478354087962b2a1e09cc734937734 100644 --- a/src/main/java/eu/ess/ics/ce/template/service/internal/metrics/MetricsService.java +++ b/src/main/java/eu/ess/ics/ce/template/service/internal/metrics/MetricsService.java @@ -38,7 +38,7 @@ public class MetricsService { .description(METRIC_DESCRIPTION_IOC_TYPES) .register(meterRegistry); - Gauge.builder(METRIC_NAME_IOC_INSTANCES, instanceRepository::countAll) + Gauge.builder(METRIC_NAME_IOC_INSTANCES, instanceRepository::countAllLatestInstances) .description(METRIC_DESCRIPTION_IOC_INSTANCES) .register(meterRegistry); }