Small fixes on metrics

This commit is contained in:
Thomas Georgios Giannos 2024-04-15 11:26:18 +03:00
parent af5a9c269e
commit e2aab3e523
3 changed files with 29 additions and 15 deletions

View File

@ -3,24 +3,19 @@ package eu.eudat.service.metrics;
import eu.eudat.commons.enums.*;
import eu.eudat.commons.metrics.MetricLabels;
import eu.eudat.commons.metrics.MetricNames;
import eu.eudat.data.DmpEntity;
import eu.eudat.data.TenantEntityManager;
import eu.eudat.model.Dmp;
import eu.eudat.query.*;
import eu.eudat.service.keycloak.KeycloakService;
import eu.eudat.service.keycloak.MyKeycloakAdminRestApi;
import gr.cite.commons.web.keycloak.api.configuration.KeycloakClientProperties;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.Gauge;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -39,12 +34,15 @@ public class MetricsServiceImpl implements MetricsService {
private final KeycloakClientProperties _keycloakConfig;
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(MetricsServiceImpl.class));
public MetricsServiceImpl(
PrometheusMeterRegistry registry,
QueryFactory queryFactory,
TenantEntityManager entityManager,
MyKeycloakAdminRestApi keycloakAdminRestApi,
UpdateMetricsTaskProperties config, KeycloakClientProperties keycloakConfig) {
UpdateMetricsTaskProperties config,
KeycloakClientProperties keycloakConfig) {
this.registry = registry;
this.queryFactory = queryFactory;
this.entityManager = entityManager;
@ -58,7 +56,7 @@ public class MetricsServiceImpl implements MetricsService {
try {
this.entityManager.disableTenantFilters();
this.setGaugeValue(gauges, MetricNames.DMP, calculateDraftDmps(false), MetricLabels.DRAFT);
this.setGaugeValue(gauges, MetricNames.DMP, calculateDraftDmps(false), MetricLabels.DRAFT);
this.setGaugeValue(gauges, MetricNames.DMP, calculateFinalizedDmps(false), MetricLabels.FINALIZED);
this.setGaugeValue(gauges, MetricNames.DMP, calculatePublishedDmps(false), MetricLabels.PUBLISHED);
this.setGaugeValue(gauges, MetricNames.DMP, calculateDoiedDmps(false), MetricLabels.DOIED);
@ -106,13 +104,15 @@ public class MetricsServiceImpl implements MetricsService {
this.setGaugeValue(gauges, MetricNames.NEXUS_PREFIX + MetricNames.DATASET_TEMPLATE, calculateFinalizedTemplates(true), MetricLabels.ACTIVE);
this.setGaugeValue(gauges, MetricNames.NEXUS_PREFIX + MetricNames.DATASET_TEMPLATE, calculateUsedTemplates(true), MetricLabels.USED);
this.setGaugeValue(gauges, MetricNames.USERS, calculateActiveUsers(), MetricLabels.LOGGEDIN);
this.setGaugeValue(gauges, MetricNames.USERS, calculateAllUsers(), MetricLabels.TOTAL);
this.setGaugeValue(gauges, MetricNames.LANGUAGES, calculateLanguages(), null);
this.setGaugeValue(gauges, MetricNames.INSTALLATIONS, 1d, null);
this.setGaugeValue(gauges, MetricNames.NEXUS_PREFIX + MetricNames.INSTALLATIONS, 1d, null);
this.setGaugeValue(gauges, MetricNames.USERS, calculateActiveUsers(), MetricLabels.LOGGEDIN);
this.setGaugeValue(gauges, MetricNames.USERS, calculateAllUsers(), MetricLabels.TOTAL);
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
this.entityManager.enableTenantFilters();
}
@ -158,7 +158,7 @@ public class MetricsServiceImpl implements MetricsService {
DmpQuery dmpQuery = this.queryFactory.query(DmpQuery.class).statuses(DmpStatus.Draft).isActive(IsActive.Active);
if (forNexus)
dmpQuery.after(_config.getNexusDate());
return dmpQuery.count();
return dmpQuery.count();
}
private double calculateFinalizedDmps(boolean forNexus) {
@ -304,11 +304,18 @@ public class MetricsServiceImpl implements MetricsService {
}
private double calculateActiveUsers() {
return this.keycloakAdminRestApi.users().getUserSessionsCountByClientId(_keycloakConfig.getClientId());
double result = -1;
try {
result = this.keycloakAdminRestApi.users().getUserSessionsCountByClientId(_config.getUsersLoginClient());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return result;
}
private double calculateAllUsers() {
return this.keycloakAdminRestApi.users().getAllUsersCount();
UserQuery userQuery = this.queryFactory.query(UserQuery.class).isActive(IsActive.Active);
return userQuery.count();
}
private double calculateLanguages() {

View File

@ -15,6 +15,8 @@ public class UpdateMetricsTaskProperties {
private Instant nexusDate;
private String usersLoginClient;
private ReferenceTypesProperties referenceTypes;
public boolean getEnable() {
@ -29,6 +31,10 @@ public class UpdateMetricsTaskProperties {
return nexusDate;
}
public String getUsersLoginClient() {
return usersLoginClient;
}
public ReferenceTypesProperties getReferenceTypes() {
return referenceTypes;
}

View File

@ -3,6 +3,7 @@ metrics:
enable: true
intervalSeconds: 600
nexusDate: "2021-01-01T00:00:00.00Z"
usersLoginClient: ${IDP_APIKEY_CLIENT_ID:}
referenceTypes:
funderIds: []
grantIds: []