128 lines
6.2 KiB
Java
128 lines
6.2 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.types.MetricNames;
|
|
import io.prometheus.client.Gauge;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.transaction.Transactional;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
@Component
|
|
public class MetricsManager {
|
|
private final static Logger logger = LoggerFactory.getLogger(MetricsManager.class);
|
|
private static Map<String, Gauge> gauges = Stream.of( new Object[][]{
|
|
{MetricNames.DMP, Gauge.build().name(MetricNames.DMP).help("Number of managed DMPs").labelNames("status").register()},
|
|
|
|
{MetricNames.FUNDERS, Gauge.build().name(MetricNames.FUNDERS).help("Number of registered Funders").register()},
|
|
{MetricNames.GRANTS, Gauge.build().name(MetricNames.GRANTS).help("Number of registered Grants").register()},
|
|
{MetricNames.PROJECT, Gauge.build().name(MetricNames.PROJECT).help("Number of registered Projects").register()},
|
|
{MetricNames.RESEARCHER, Gauge.build().name(MetricNames.RESEARCHER).help("Number of Colaborators/Researchers").register()},
|
|
|
|
{MetricNames.DATASET, Gauge.build().name(MetricNames.DATASET).help("Number of managed Dataset Descriptions").labelNames("status").register()},
|
|
|
|
{MetricNames.DATASET_TEMPLATE, Gauge.build().name(MetricNames.DATASET_TEMPLATE).help("Number of dataset Templates").labelNames("status").register()},
|
|
|
|
{MetricNames.USERS, Gauge.build().name(MetricNames.USERS).help("Number of users").labelNames("type").register()},
|
|
|
|
{MetricNames.LANGUAGES, Gauge.build().name(MetricNames.LANGUAGES).help("Number of Languages").register()},
|
|
|
|
}).collect(Collectors.toMap(data -> (String)data[0], data -> (Gauge) data[1]));
|
|
|
|
public static void increaseValue(String name, int amount, String label) {
|
|
|
|
if(label != null) {
|
|
gauges.get(name).labels(label).inc(amount);
|
|
} else {
|
|
gauges.get(name).inc(amount);
|
|
}
|
|
}
|
|
|
|
public static void decreaseValue(String name, int amount, String label) {
|
|
if(label != null) {
|
|
gauges.get(name).labels(label).dec(amount);
|
|
} else {
|
|
gauges.get(name).dec(amount);
|
|
}
|
|
}
|
|
|
|
public static Integer getValue(String name, String label) {
|
|
if(label != null) {
|
|
return Double.valueOf(gauges.get(name).labels(label).get()).intValue();
|
|
} else {
|
|
return Double.valueOf(gauges.get(name).get()).intValue();
|
|
}
|
|
}
|
|
|
|
public static void calculateValue(String name, int amount, String label) {
|
|
Integer orig = getValue(name, label);
|
|
int diff = orig - amount;
|
|
if (diff != 0) {
|
|
if (diff > 0) {
|
|
decreaseValue(name, diff, label);
|
|
} else {
|
|
increaseValue(name, Math.abs(diff), label);
|
|
}
|
|
}
|
|
}
|
|
|
|
private final DatasetManager datasetManager;
|
|
private final DataManagementPlanManager dataManagementPlanManager;
|
|
private final DatasetProfileManager datasetProfileManager;
|
|
private final UserManager userManager;
|
|
private final Environment environment;
|
|
|
|
@Autowired
|
|
public MetricsManager(DatasetManager datasetManager, DataManagementPlanManager dataManagementPlanManager, DatasetProfileManager datasetProfileManager, UserManager userManager, Environment environment) {
|
|
this.datasetManager = datasetManager;
|
|
this.dataManagementPlanManager = dataManagementPlanManager;
|
|
this.datasetProfileManager = datasetProfileManager;
|
|
this.userManager = userManager;
|
|
this.environment = environment;
|
|
}
|
|
|
|
@PostConstruct
|
|
@Transactional
|
|
@Scheduled(initialDelay = 1000 * 60 * 60, fixedDelay = 1000 * 60 * 60)
|
|
public void init() throws IOException {
|
|
logger.info("Start calculating Metrics");
|
|
calculateValue(MetricNames.DMP, (int) dataManagementPlanManager.countAllDrafts(), MetricNames.DRAFT);
|
|
calculateValue(MetricNames.DMP, (int) dataManagementPlanManager.countAllFinalized(), MetricNames.FINALIZED);
|
|
calculateValue(MetricNames.DMP, (int) dataManagementPlanManager.countAllPublished(), MetricNames.PUBLISHED);
|
|
calculateValue(MetricNames.DMP, (int) dataManagementPlanManager.countAllDoied(), MetricNames.DOIED);
|
|
|
|
calculateValue(MetricNames.FUNDERS, (int) dataManagementPlanManager.countAllFunders(), null);
|
|
calculateValue(MetricNames.GRANTS, (int) dataManagementPlanManager.countAllGrants(), null);
|
|
calculateValue(MetricNames.PROJECT, (int) dataManagementPlanManager.countAllProjects(), null);
|
|
calculateValue(MetricNames.RESEARCHER, (int) dataManagementPlanManager.countAllResearchers(), null);
|
|
|
|
calculateValue(MetricNames.DATASET, (int) datasetManager.countAllDraft(), MetricNames.DRAFT);
|
|
calculateValue(MetricNames.DATASET, (int) datasetManager.countAllFinalized(), MetricNames.FINALIZED);
|
|
calculateValue(MetricNames.DATASET, (int) datasetManager.countAllPublic(), MetricNames.PUBLISHED);
|
|
calculateValue(MetricNames.DATASET, (int) datasetManager.countAllWithDoi(), MetricNames.DOIED);
|
|
|
|
calculateValue(MetricNames.DATASET_TEMPLATE, (int) datasetProfileManager.countAlldraft(), MetricNames.DRAFT);
|
|
calculateValue(MetricNames.DATASET_TEMPLATE, (int) datasetProfileManager.countAllFinalized(), MetricNames.ACTIVE);
|
|
calculateValue(MetricNames.DATASET_TEMPLATE, (int) datasetProfileManager.countAllUsed(), MetricNames.USED);
|
|
|
|
calculateValue(MetricNames.USERS, (int) userManager.countActiveUsers().intValue(), MetricNames.LOGGEDIN);
|
|
calculateValue(MetricNames.USERS, (int) userManager.countAllUsers().intValue(), MetricNames.TOTAL);
|
|
|
|
long files = Files.list(Paths.get(this.environment.getProperty("userguide.path"))).count();
|
|
calculateValue(MetricNames.LANGUAGES, (int) files, null);
|
|
|
|
logger.info("Metrics calculation Completed");
|
|
}
|
|
|
|
}
|