Minor metrics refactor

This commit is contained in:
George Kalampokis 2021-10-05 13:09:02 +03:00
parent b66bde871a
commit 54c862ecbb
8 changed files with 85 additions and 57 deletions

View File

@ -20,6 +20,7 @@ import eu.eudat.models.data.listingmodels.UserInfoListingModel;
import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import eu.eudat.types.ApiMessageCode;
import eu.eudat.types.MetricNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -70,7 +71,7 @@ public class Admin extends BaseController {
userDatasetProfile.setRole(0);
getApiContext().getOperationsContext().getDatabaseRepository().getUserDatasetProfileDao().createOrUpdate(userDatasetProfile);
datasetProfileManager.storeDatasetProfileUsers(datasetProfile, profile);
MetricsManager.increaseValue("argos_dataset_templates", 1, "total");
MetricsManager.increaseValue(MetricNames.DATASET_TEMPLATE, 1, MetricNames.TOTAL);
return ResponseEntity.status(HttpStatus.OK).body(modelDefinition.getId());
}
@ -89,7 +90,7 @@ public class Admin extends BaseController {
eu.eudat.data.entities.DatasetProfile datasetProfile = this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetProfileDao().createOrUpdate(datasetprofile);
datasetProfileManager.storeDatasetProfileUsers(datasetProfile, profile);
if (datasetProfile.getStatus() == 1 && oldStatus == 0) {
MetricsManager.increaseValue("argos_dataset_templates", 1, "active");
MetricsManager.increaseValue(MetricNames.DATASET_TEMPLATE, 1, MetricNames.ACTIVE);
}
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UUID>().status(ApiMessageCode.NO_MESSAGE));
}

View File

@ -35,6 +35,7 @@ import eu.eudat.models.data.login.LoginInfo;
import eu.eudat.models.data.principal.PrincipalModel;
import eu.eudat.models.data.security.Principal;
import eu.eudat.types.ApiMessageCode;
import eu.eudat.types.MetricNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -95,7 +96,7 @@ public class Login {
public @ResponseBody
ResponseEntity<ResponseItem<PrincipalModel>> externallogin(@RequestBody LoginInfo credentials) throws GeneralSecurityException, NullEmailException {
logger.info("Trying To Login With " + credentials.getProvider());
MetricsManager.increaseValue("argos_users", 1, "loggedin");
MetricsManager.increaseValue(MetricNames.USERS, 1, MetricNames.LOGGEDIN);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<PrincipalModel>().payload(customAuthenticationProvider.authenticate(credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
}
@ -104,7 +105,7 @@ public class Login {
public @ResponseBody
ResponseEntity<ResponseItem<PrincipalModel>> nativelogin(@RequestBody Credentials credentials) throws NullEmailException {
logger.info(credentials.getUsername() + " Trying To Login");
MetricsManager.increaseValue("argos_users", 1, "loggedin");
MetricsManager.increaseValue(MetricNames.USERS, 1, MetricNames.LOGGEDIN);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<PrincipalModel>().payload(userManager.authenticate(this.nonVerifiedUserAuthenticationService, credentials)).status(ApiMessageCode.SUCCESS_MESSAGE));
}
@ -165,7 +166,7 @@ public class Login {
ResponseEntity<ResponseItem<Principal>> logout(Principal principal) {
this.nonVerifiedUserAuthenticationService.Logout(principal.getToken());
logger.info(principal + " Logged Out");
MetricsManager.decreaseValue("argos_users", 1, "loggedin");
MetricsManager.decreaseValue(MetricNames.USERS, 1, MetricNames.LOGGEDIN);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Principal>().status(ApiMessageCode.NO_MESSAGE));
}

View File

@ -6,6 +6,7 @@ import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.userguide.UserGuide;
import eu.eudat.types.ApiMessageCode;
import eu.eudat.types.MetricNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
@ -39,7 +40,7 @@ public class UserGuideController {
@RequestMapping(path = "{lang}", method = RequestMethod.GET )
public ResponseEntity getUserGuide(@PathVariable(name = "lang") String lang) throws IOException {
long files = Files.list(Paths.get(this.environment.getProperty("userguide.path"))).count();
MetricsManager.calculateValue("argos_languages", (int) files, null);
MetricsManager.calculateValue(MetricNames.LANGUAGES, (int) files, null);
Stream<Path> walk = Files.walk(Paths.get(this.environment.getProperty("userguide.path")));
List<String> result = walk.filter(Files::isRegularFile)
.map(Path::toString).collect(Collectors.toList());

View File

@ -59,6 +59,7 @@ import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import eu.eudat.models.data.userinfo.UserListingModel;
import eu.eudat.queryable.QueryableList;
import eu.eudat.types.Authorities;
import eu.eudat.types.MetricNames;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
@ -445,7 +446,7 @@ public class DataManagementPlanManager {
setNotification = true;
} else {
MetricsManager.increaseValue("argos_managed_dmps", 1, "draft");
MetricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT);
}
DMP newDmp = dataManagementPlan.toDataModel();
@ -572,7 +573,7 @@ public class DataManagementPlanManager {
if (dataManagementPlan.getStatus() == (int) DMP.DMPStatus.FINALISED.getValue() && dmp1.getStatus().equals(DMP.DMPStatus.FINALISED.getValue()))
throw new Exception("DMP is finalized, therefore cannot be edited.");
} else {
MetricsManager.increaseValue("argos_managed_dmps", 1, "draft");
MetricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT);
}
List<Dataset> datasets = new ArrayList<>();
DMP tempDMP = dataManagementPlan.toDataModel();
@ -700,7 +701,7 @@ public class DataManagementPlanManager {
newDmp.setDataset(new HashSet<>(databaseRepository.getDatasetDao().getWithCriteria(criteria1).toList()));
this.updateIndex(newDmp);
MetricsManager.increaseValue("argos_managed_dmps", 1, "draft");
MetricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT);
return newDmp.getId();
}
@ -714,16 +715,16 @@ public class DataManagementPlanManager {
DMP oldDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(uuid);
switch (oldDmp.getStatus()) {
case 0:
MetricsManager.decreaseValue("argos_managed_dmps", 1, "draft");
MetricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.DRAFT);
break;
case 1:
if (oldDmp.getDoi() != null) {
MetricsManager.decreaseValue("argos_managed_dmps", 1, "doied");
MetricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.DOIED);
}
if (oldDmp.isPublic()) {
MetricsManager.decreaseValue("argos_managed_dmps", 1, "published");
MetricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.PUBLISHED);
}
MetricsManager.decreaseValue("argos_managed_dmps", 1, "finalized");
MetricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.FINALIZED);
break;
}
oldDmp.setStatus(DMP.DMPStatus.DELETED.getValue());
@ -763,7 +764,7 @@ public class DataManagementPlanManager {
else {
researcher.setCreationUser(user);
researcherRepository.createOrUpdate(researcher);
MetricsManager.increaseValue("argos_researchers", 1, null);
MetricsManager.increaseValue(MetricNames.RESEARCHER, 1, null);
}
}
}
@ -845,14 +846,14 @@ public class DataManagementPlanManager {
project.setType(Project.ProjectType.EXTERNAL.getValue());
if (project.getId() == null) project.setId(UUID.randomUUID());
projectDao.createOrUpdate(project);
MetricsManager.increaseValue("argos_projects", 1, null);
MetricsManager.increaseValue(MetricNames.PROJECT, 1, null);
}
}
else {
project.setType(Project.ProjectType.EXTERNAL.getValue());
if (project.getId() == null) project.setId(UUID.randomUUID());
projectDao.createOrUpdate(project);
MetricsManager.increaseValue("argos_projects", 1, null);
MetricsManager.increaseValue(MetricNames.PROJECT, 1, null);
}
}
}
@ -929,7 +930,7 @@ public class DataManagementPlanManager {
return newDataset;
}).thenApplyAsync(item -> {
futures.add(datasetDao.createOrUpdateAsync(item).whenComplete(((dataset1, throwable) -> {
MetricsManager.increaseValue("argos_managed_dataset_descriptions", 1, "draft");
MetricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.DRAFT);
eu.eudat.elastic.entities.Dataset datasetElastic = new eu.eudat.elastic.entities.Dataset();
datasetElastic.setId(dataset1.getId().toString());
datasetElastic.setLabel(dataset1.getLabel());
@ -988,7 +989,7 @@ public class DataManagementPlanManager {
dmp.setPublic(true);
apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp);
this.updateIndex(dmp);
MetricsManager.increaseValue("argos_managed_dmps", 1, "published");
MetricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.PUBLISHED);
DataManagementPlanCriteria criteria = new DataManagementPlanCriteria();
criteria.setGroupIds(Collections.singletonList(dmp.getGroupId()));
criteria.setAllVersions(true);
@ -1001,7 +1002,7 @@ public class DataManagementPlanManager {
tags = elastic.getTags();
}
this.datasetManager.updateTags(dataset, tags);
MetricsManager.increaseValue("argos_managed_dataset_descriptions", 1, "published");
MetricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.PUBLISHED);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
@ -1087,11 +1088,11 @@ public class DataManagementPlanManager {
this.updateIndex(dmp);
UserInfo user = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
sendNotification(dmp, user, NotificationType.DMP_FINALISED);
MetricsManager.decreaseValue("argos_managed_dmps", 1, "draft");
MetricsManager.increaseValue("argos_managed_dmps", 1, "finalized");
MetricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.DRAFT);
MetricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.FINALIZED);
this.updateDatasetsIndex(indexDatasets);
MetricsManager.decreaseValue("argos_managed_dataset_descriptions", indexDatasets.size(), "draft");
MetricsManager.increaseValue("argos_managed_dataset_descriptions", indexDatasets.size(), "finalized");
MetricsManager.decreaseValue(MetricNames.DATASET, indexDatasets.size(), MetricNames.DRAFT);
MetricsManager.increaseValue(MetricNames.DATASET, indexDatasets.size(), MetricNames.FINALIZED);
}
public void undoFinalize(UUID id, Principal principal) throws Exception {
@ -1103,8 +1104,8 @@ public class DataManagementPlanManager {
dmp.setStatus(DMP.DMPStatus.ACTIVE.getValue());
apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp);
this.updateIndex(dmp);
MetricsManager.decreaseValue("argos_managed_dmps", 1, "finalized");
MetricsManager.increaseValue("argos_managed_dmps", 1, "draft");
MetricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.FINALIZED);
MetricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT);
}
public void updateUsers(UUID id, List<UserInfoListingModel> users, Principal principal) throws Exception {

View File

@ -48,6 +48,7 @@ import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import eu.eudat.queryable.QueryableList;
import eu.eudat.types.Authorities;
import eu.eudat.types.MetricNames;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
@ -619,7 +620,7 @@ public class DatasetManager {
sendNotification = true;
}
} else {
MetricsManager.increaseValue("argos_managed_dataset_descriptions", 1, "draft");
MetricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.DRAFT);
}
if (dmp.getStatus().equals(DMP.DMPStatus.FINALISED.getValue()) && datasetWizardModel.getId() != null)
throw new Exception("DMP is finalized, therefore Dataset cannot be edited.");
@ -889,7 +890,7 @@ public class DatasetManager {
if (dataset.getStatus() != eu.eudat.data.entities.Dataset.Status.FINALISED.getValue())
throw new Exception("You cannot make public a Dataset That Has not Been Finalised");
datasetDao.createOrUpdate(dataset);
MetricsManager.increaseValue("argos_managed_dataset_descriptions", 1, "published");
MetricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.PUBLISHED);
}
public ResponseEntity<byte[]> getDocument(String id, VisibilityRuleService visibilityRuleService, String contentType, Principal principal) throws IllegalAccessException, IOException, InstantiationException {
@ -978,7 +979,7 @@ public class DatasetManager {
createServicesIfTheyDontExist(entity);
createExternalDatasetsIfTheyDontExist(entity);
MetricsManager.increaseValue("argos_managed_dataset_descriptions", 1, "draft");
MetricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.DRAFT);
return apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao().createOrUpdate(entity);
}

View File

@ -27,6 +27,7 @@ import eu.eudat.models.data.listingmodels.UserInfoListingModel;
import eu.eudat.models.data.security.Principal;
import eu.eudat.queryable.QueryableList;
import eu.eudat.types.Authorities;
import eu.eudat.types.MetricNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -92,7 +93,7 @@ public class DatasetProfileManager {
DatasetProfile profile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(id));
apiContext.getOperationsContext().getDatabaseRepository().detachEntity(profile);
profile.setId(null);
MetricsManager.increaseValue("argos_dataset_templates", 1, "total");
MetricsManager.increaseValue(MetricNames.DATASET_TEMPLATE, 1, MetricNames.TOTAL);
return profile;
}
@ -233,7 +234,7 @@ public class DatasetProfileManager {
File localFile = convert(multiPartFile);
eu.eudat.logic.utilities.documents.xml.datasetProfileXml.datasetProfileModel.DatasetProfile profile = xmlBuilder.build(localFile);
Files.deleteIfExists(localFile.toPath());
MetricsManager.increaseValue("argos_dataset_templates", 1, "total");
MetricsManager.increaseValue(MetricNames.DATASET_TEMPLATE, 1, MetricNames.TOTAL);
return profile;
} catch (IOException e) {
logger.error(e.getMessage(), e);

View File

@ -1,5 +1,6 @@
package eu.eudat.logic.managers;
import eu.eudat.types.MetricNames;
import io.prometheus.client.Gauge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -21,20 +22,20 @@ import java.util.stream.Stream;
public class MetricsManager {
private final static Logger logger = LoggerFactory.getLogger(MetricsManager.class);
private static Map<String, Gauge> gauges = Stream.of( new Object[][]{
{"argos_managed_dmps", Gauge.build().name("argos_managed_dmps").help("Number of managed DMPs").labelNames("status").register()},
{MetricNames.DMP, Gauge.build().name(MetricNames.DMP).help("Number of managed DMPs").labelNames("status").register()},
{"argos_funders", Gauge.build().name("argos_funders").help("Number of registered Funders").register()},
{"argos_grants", Gauge.build().name("argos_grants").help("Number of registered Grants").register()},
{"argos_projects", Gauge.build().name("argos_projects").help("Number of registered Projects").register()},
{"argos_researchers", Gauge.build().name("argos_researchers").help("Number of Colaborators/Researchers").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()},
{"argos_managed_dataset_descriptions", Gauge.build().name("argos_managed_dataset_descriptions").help("Number of managed Dataset Descriptions").labelNames("status").register()},
{MetricNames.DATASET, Gauge.build().name(MetricNames.DATASET).help("Number of managed Dataset Descriptions").labelNames("status").register()},
{"argos_dataset_templates", Gauge.build().name("argos_dataset_templates").help("Number of dataset Templates").labelNames("status").register()},
{MetricNames.DATASET_TEMPLATE, Gauge.build().name(MetricNames.DATASET_TEMPLATE).help("Number of dataset Templates").labelNames("status").register()},
{"argos_users", Gauge.build().name("argos_users").help("Number of users").labelNames("type").register()},
{MetricNames.USERS, Gauge.build().name(MetricNames.USERS).help("Number of users").labelNames("type").register()},
{"argos_languages", Gauge.build().name("argos_languages").help("Number of Languages").register()},
{MetricNames.LANGUAGES, Gauge.build().name(MetricNames.LANGUAGES).help("Number of Languages").register()},
}).collect(Collectors.toMap(data -> (String)data[0], data -> (Gauge) data[1]));
@ -95,30 +96,30 @@ public class MetricsManager {
@Scheduled(initialDelay = 1000 * 60 * 60, fixedDelay = 1000 * 60 * 60)
public void init() throws IOException {
logger.info("Start calculating Metrics");
calculateValue("argos_managed_dmps", (int) dataManagementPlanManager.countAllDrafts(), "draft");
calculateValue("argos_managed_dmps", (int) dataManagementPlanManager.countAllFinalized(), "finalized");
calculateValue("argos_managed_dmps", (int) dataManagementPlanManager.countAllPublished(), "published");
calculateValue("argos_managed_dmps", (int) dataManagementPlanManager.countAllDoied(), "doied");
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("argos_funders", (int) dataManagementPlanManager.countAllFunders(), null);
calculateValue("argos_grants", (int) dataManagementPlanManager.countAllGrants(), null);
calculateValue("argos_projects", (int) dataManagementPlanManager.countAllProjects(), null);
calculateValue("argos_researchers", (int) dataManagementPlanManager.countAllResearchers(), null);
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("argos_managed_dataset_descriptions", (int) datasetManager.countAllDraft(), "draft");
calculateValue("argos_managed_dataset_descriptions", (int) datasetManager.countAllFinalized(), "finalized");
calculateValue("argos_managed_dataset_descriptions", (int) datasetManager.countAllPublic(), "published");
calculateValue("argos_managed_dataset_descriptions", (int) datasetManager.countAllWithDoi(), "doied");
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("argos_dataset_templates", (int) datasetProfileManager.countAlldraft(), "draft");
calculateValue("argos_dataset_templates", (int) datasetProfileManager.countAllFinalized(), "active");
calculateValue("argos_dataset_templates", (int) datasetProfileManager.countAllUsed(), "used");
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("argos_users", (int) userManager.countActiveUsers().intValue(), "loggedin");
calculateValue("argos_users", (int) userManager.countAllUsers().intValue(), "total");
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("argos_languages", (int) files, null);
calculateValue(MetricNames.LANGUAGES, (int) files, null);
logger.info("Metrics calculation Completed");
}

View File

@ -0,0 +1,21 @@
package eu.eudat.types;
public class MetricNames {
public static final String DATASET_TEMPLATE = "argos_dataset_templates";
public static final String USERS = "argos_users";
public static final String DMP = "argos_managed_dmps";
public static final String DATASET = "argos_managed_dataset_descriptions";
public static final String RESEARCHER = "argos_researchers";
public static final String PROJECT = "argos_projects";
public static final String FUNDERS = "argos_funders";
public static final String GRANTS = "argos_grants";
public static final String LANGUAGES = "argos_languages";
public static final String DRAFT = "draft";
public static final String FINALIZED = "finalized";
public static final String PUBLISHED = "published";
public static final String DOIED = "doied";
public static final String ACTIVE = "active";
public static final String USED = "used";
public static final String LOGGEDIN = "loggedin";
public static final String TOTAL = "total";
}