From d16e130dc63994df2515fc7c3fc9f3e20eabed09 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Fri, 18 Feb 2022 17:51:57 +0200 Subject: [PATCH] Optimize general metric counter --- .../eudat/logic/managers/MetricsManager.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/MetricsManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/MetricsManager.java index 589e58538..63bac9c04 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/MetricsManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/MetricsManager.java @@ -3,7 +3,6 @@ package eu.eudat.logic.managers; import eu.eudat.data.dao.criteria.*; import eu.eudat.data.entities.DatasetProfile; import eu.eudat.logic.services.ApiContext; -import eu.eudat.logic.utilities.helpers.StreamDistinctBy; import eu.eudat.types.MetricNames; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.Gauge; @@ -23,7 +22,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; -import java.util.*; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -402,17 +404,10 @@ public class MetricsManager { criteria.setStatus(1); criteria.setAllVersions(false); if (countNexus) criteria.setPeriodStart(getNexusDate()); - List datasetProfiles = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().getWithCriteria(criteria).toList(); - List filteredProfiles = new ArrayList<>(); - for (DatasetProfile datasetProfile : datasetProfiles) { - DatasetCriteria datasetCriteria = new DatasetCriteria(); - datasetCriteria.setDatasetTemplates(Collections.singletonList(datasetProfile.getId())); - Long datasetCount = apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao().getWithCriteria(datasetCriteria).count(); - if (datasetCount > 0) { - filteredProfiles.add(datasetProfile); - } - } - return filteredProfiles.stream().filter(StreamDistinctBy.distinctByKey(DatasetProfile::getId)).count(); + List datasetProfiles = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().getWithCriteria(criteria).withFields(Collections.singletonList("id")).toList(); + DatasetCriteria datasetCriteria = new DatasetCriteria(); + datasetCriteria.setDatasetTemplates(datasetProfiles.stream().map(DatasetProfile::getId).collect(Collectors.toList())); + return apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao().getWithCriteria(datasetCriteria).select(root -> root.getProfile().getId()).stream().distinct().count(); } }