metrics cache

This commit is contained in:
Michele Artini 2021-07-30 16:17:20 +02:00
parent 53750ff818
commit 91fccbae7f
12 changed files with 120 additions and 46 deletions

View File

@ -2,6 +2,7 @@ package eu.dnetlib.broker;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.ClientConfiguration;
@ -18,6 +19,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration @Configuration
@EnableSwagger2 @EnableSwagger2
@EnableCaching
@EnableScheduling @EnableScheduling
@EnableTransactionManagement @EnableTransactionManagement
@EnableElasticsearchRepositories(basePackageClasses = { @EnableElasticsearchRepositories(basePackageClasses = {

View File

@ -2,6 +2,7 @@ package eu.dnetlib.broker;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.ClientConfiguration;
@ -18,6 +19,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration @Configuration
@EnableSwagger2 @EnableSwagger2
@EnableCaching
@EnableScheduling @EnableScheduling
@EnableTransactionManagement @EnableTransactionManagement
@EnableElasticsearchRepositories(basePackageClasses = { @EnableElasticsearchRepositories(basePackageClasses = {

View File

@ -31,6 +31,7 @@ public class UserController extends AbstractDnetController {
@Autowired @Autowired
private UserViewRepository userViewRepository; private UserViewRepository userViewRepository;
@Autowired @Autowired
private DatabaseUtils dbUtils; private DatabaseUtils dbUtils;

View File

@ -14,7 +14,7 @@ public class ValidOrganizationsMetric extends MetricInfo {
private OrganizationRepository organizationRepository; private OrganizationRepository organizationRepository;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return organizationRepository.countByStatus(OrganizationStatus.approved.toString()); return organizationRepository.countByStatus(OrganizationStatus.approved.toString());
} }

View File

@ -14,21 +14,21 @@ public abstract class MetricInfo implements BeanNameAware {
private String beanName; private String beanName;
abstract public double obtainValue(MetricInfo info); abstract public double obtainValue();
public String getBeanName() { public String getBeanName() {
return beanName; return beanName;
} }
@Override @Override
public final void setBeanName(final String beanName) { public void setBeanName(final String beanName) {
this.beanName = beanName; this.beanName = beanName;
} }
@PostConstruct @PostConstruct
public final void register() { public void register() {
log.info("Prometheus - new metric registered: " + getBeanName()); log.info("Prometheus - new metric registered: " + getBeanName());
Metrics.gauge(getBeanName(), this, this::obtainValue); Metrics.gauge(getBeanName(), this, o -> obtainValue());
} }
} }

View File

@ -0,0 +1,92 @@
package eu.dnetlib.broker.common.metrics;
import java.util.List;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.elasticsearch.NotificationRepository;
import eu.dnetlib.broker.common.stats.OpenaireDsStatRepository;
import eu.dnetlib.broker.common.subscriptions.ConditionOperator;
import eu.dnetlib.broker.common.subscriptions.MapCondition;
import eu.dnetlib.broker.common.subscriptions.Subscription;
import eu.dnetlib.broker.common.subscriptions.SubscriptionRepository;
@Component
public class MetricsCacheUtils {
public static final String metrics_cache = "broker_metrics_cache";
private static final long interval = 12 * 60 * 60 * 1000; // 12 hours
private static final Log log = LogFactory.getLog(MetricsCacheUtils.class);
@Autowired
private SubscriptionRepository subscriptionRepository;
@Autowired
private OpenaireDsStatRepository openaireDsStatRepository;
@Autowired
private NotificationRepository notificationRepository;
@Scheduled(fixedDelay = interval, initialDelay = 500)
@CacheEvict(allEntries = true, value = metrics_cache)
public void reportCacheEvict() {
log.debug("Flush Cache: " + metrics_cache);
}
@Cacheable(value = MetricsCacheUtils.metrics_cache, key = "#root.methodName")
public double totalEvents() {
log.debug("Call to cached method: totalEvents()");
return openaireDsStatRepository.totalEvents();
}
@Cacheable(value = MetricsCacheUtils.metrics_cache, key = "#root.methodName")
public double countDatasourcesWithEvents() {
log.debug("Call to cached method: countDatasourcesWithEvents()");
return openaireDsStatRepository.countDatasourcesWithEvents();
}
@Cacheable(value = MetricsCacheUtils.metrics_cache, key = "#root.methodName")
public double countNotifications() {
log.debug("Call to cached method: countNotifications()");
return notificationRepository.count();
}
@Cacheable(value = MetricsCacheUtils.metrics_cache, key = "#root.methodName")
public double countSubscriptions() {
log.debug("Call to cached method: countSubscriptions()");
return subscriptionRepository.count();
}
@Cacheable(value = MetricsCacheUtils.metrics_cache, key = "#root.methodName")
public double countSubscribers() {
log.debug("Call to cached method: countSubscribers()");
return subscriptionRepository.countSubscribers();
}
@Cacheable(value = MetricsCacheUtils.metrics_cache, key = "#root.methodName")
public double countDatasourcesWithSubscriptions() {
log.debug("Call to cached method: countDatasourcesWithSubscriptions()");
return StreamSupport.stream(subscriptionRepository.findAll().spliterator(), false)
.map(Subscription::getConditionsAsList)
.flatMap(List::stream)
.filter(c -> c.getField().equals("targetDatasourceName"))
.filter(c -> c.getOperator() == ConditionOperator.EXACT)
.map(MapCondition::getListParams)
.filter(l -> !l.isEmpty())
.map(l -> l.get(0).getValue())
.filter(StringUtils::isNotBlank)
.distinct()
.count();
}
}

View File

@ -3,18 +3,17 @@ package eu.dnetlib.broker.common.metrics;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.stats.OpenaireDsStatRepository;
import eu.dnetlib.common.metrics.MetricInfo; import eu.dnetlib.common.metrics.MetricInfo;
@Component("provide_broker_datasources_with_events") @Component("provide_broker_datasources_with_events")
public class TotalDatasourcesWithEventsMetric extends MetricInfo { public class TotalDatasourcesWithEventsMetric extends MetricInfo {
@Autowired @Autowired
private OpenaireDsStatRepository openaireDsStatRepository; private MetricsCacheUtils metricsCacheUtils;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return openaireDsStatRepository.countDatasourcesWithEvents(); return metricsCacheUtils.countDatasourcesWithEvents();
} }
} }

View File

@ -1,37 +1,19 @@
package eu.dnetlib.broker.common.metrics; package eu.dnetlib.broker.common.metrics;
import java.util.List;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.subscriptions.ConditionOperator;
import eu.dnetlib.broker.common.subscriptions.MapCondition;
import eu.dnetlib.broker.common.subscriptions.Subscription;
import eu.dnetlib.broker.common.subscriptions.SubscriptionRepository;
import eu.dnetlib.common.metrics.MetricInfo; import eu.dnetlib.common.metrics.MetricInfo;
@Component("provide_broker_datasources_with_subscriptions") @Component("provide_broker_datasources_with_subscriptions")
public class TotalDatasourcesWithSubscriptionsMetric extends MetricInfo { public class TotalDatasourcesWithSubscriptionsMetric extends MetricInfo {
@Autowired @Autowired
private SubscriptionRepository subscriptionRepository; private MetricsCacheUtils metricsCacheUtils;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return StreamSupport.stream(subscriptionRepository.findAll().spliterator(), false) return metricsCacheUtils.countDatasourcesWithSubscriptions();
.map(Subscription::getConditionsAsList)
.flatMap(List::stream)
.filter(c -> c.getField().equals("targetDatasourceName"))
.filter(c -> c.getOperator() == ConditionOperator.EXACT)
.map(MapCondition::getListParams)
.filter(l -> !l.isEmpty())
.map(l -> l.get(0).getValue())
.filter(StringUtils::isNotBlank)
.distinct()
.count();
} }
} }

View File

@ -3,18 +3,17 @@ package eu.dnetlib.broker.common.metrics;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.stats.OpenaireDsStatRepository;
import eu.dnetlib.common.metrics.MetricInfo; import eu.dnetlib.common.metrics.MetricInfo;
@Component("provide_broker_events") @Component("provide_broker_events")
public class TotalEventsMetric extends MetricInfo { public class TotalEventsMetric extends MetricInfo {
@Autowired @Autowired
private OpenaireDsStatRepository openaireDsStatRepository; private MetricsCacheUtils metricsCacheUtils;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return openaireDsStatRepository.totalEvents(); return metricsCacheUtils.totalEvents();
} }
} }

View File

@ -3,18 +3,17 @@ package eu.dnetlib.broker.common.metrics;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.elasticsearch.NotificationRepository;
import eu.dnetlib.common.metrics.MetricInfo; import eu.dnetlib.common.metrics.MetricInfo;
@Component("provide_broker_notifications") @Component("provide_broker_notifications")
public class TotalNotificationsMetric extends MetricInfo { public class TotalNotificationsMetric extends MetricInfo {
@Autowired @Autowired
private NotificationRepository notificationRepository; private MetricsCacheUtils metricsCacheUtils;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return notificationRepository.count(); return metricsCacheUtils.countNotifications();
} }
} }

View File

@ -3,18 +3,17 @@ package eu.dnetlib.broker.common.metrics;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.subscriptions.SubscriptionRepository;
import eu.dnetlib.common.metrics.MetricInfo; import eu.dnetlib.common.metrics.MetricInfo;
@Component("provide_broker_subscribers") @Component("provide_broker_subscribers")
public class TotalSubscribersMetric extends MetricInfo { public class TotalSubscribersMetric extends MetricInfo {
@Autowired @Autowired
private SubscriptionRepository subscriptionRepository; private MetricsCacheUtils metricsCacheUtils;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return subscriptionRepository.countSubscribers(); return metricsCacheUtils.countSubscribers();
} }
} }

View File

@ -3,18 +3,17 @@ package eu.dnetlib.broker.common.metrics;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import eu.dnetlib.broker.common.subscriptions.SubscriptionRepository;
import eu.dnetlib.common.metrics.MetricInfo; import eu.dnetlib.common.metrics.MetricInfo;
@Component("provide_broker_subscriptions") @Component("provide_broker_subscriptions")
public class TotalSubscriptionsMetric extends MetricInfo { public class TotalSubscriptionsMetric extends MetricInfo {
@Autowired @Autowired
private SubscriptionRepository subscriptionRepository; private MetricsCacheUtils metricsCacheUtils;
@Override @Override
public double obtainValue(final MetricInfo info) { public double obtainValue() {
return subscriptionRepository.count(); return metricsCacheUtils.countSubscriptions();
} }
} }