208 lines
9.6 KiB
Java
208 lines
9.6 KiB
Java
package eu.dnetlib.repo.manager.service;
|
|
|
|
import eu.dnetlib.api.functionality.ValidatorService;
|
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
|
import eu.dnetlib.domain.functionality.validator.JobForValidation;
|
|
import eu.dnetlib.domain.functionality.validator.StoredJob;
|
|
import eu.dnetlib.repo.manager.shared.Constants;
|
|
import eu.dnetlib.repo.manager.shared.JobsOfUser;
|
|
import gr.uoa.di.driver.util.ServiceLocator;
|
|
import org.apache.log4j.Logger;
|
|
import org.eurocris.openaire.cris.validator.model.Job;
|
|
import org.eurocris.openaire.cris.validator.service.MapJobDao;
|
|
import org.json.JSONException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.mail.Store;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service("monitorService")
|
|
public class MonitorServiceImpl implements MonitorService {
|
|
|
|
@Autowired
|
|
private MapJobDao crisJobs;
|
|
|
|
@Resource(name = "validatorServiceLocator")
|
|
private ServiceLocator<ValidatorService> validatorServiceLocator;
|
|
|
|
private ValidatorService getValidationService() {
|
|
return this.validatorServiceLocator.getService();
|
|
}
|
|
|
|
public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
|
|
return validatorServiceLocator;
|
|
}
|
|
|
|
public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
|
|
this.validatorServiceLocator = validatorServiceLocator;
|
|
}
|
|
|
|
|
|
private static final Logger LOGGER = Logger
|
|
.getLogger(MonitorServiceImpl.class);
|
|
|
|
@Override
|
|
public JobsOfUser getJobsOfUser(String user,
|
|
String jobType,
|
|
String offset,
|
|
String limit,
|
|
String dateFrom,
|
|
String dateTo,
|
|
String validationStatus,
|
|
String includeJobsTotal) throws JSONException, ValidatorServiceException {
|
|
|
|
LOGGER.debug("Getting jobs of user : " + user);
|
|
LOGGER.debug(user + "/" + jobType + "/" + offset + "/" + dateFrom + "/" + dateTo + "/" + validationStatus + "/" + includeJobsTotal);
|
|
JobsOfUser retJobs = new JobsOfUser();
|
|
retJobs.setJobs(getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset),
|
|
Integer.parseInt(limit), dateFrom, dateTo, validationStatus));
|
|
|
|
if (Boolean.parseBoolean(includeJobsTotal)) {
|
|
retJobs.setTotalJobs(this.getJobsTotalNumberOfUser(user, jobType, null));
|
|
retJobs.setTotalJobsSuccessful(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_SUCCESSFUL));
|
|
retJobs.setTotalJobsFailed(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_FAILED));
|
|
retJobs.setTotalJobsOngoing(this.getJobsTotalNumberOfUser(user, jobType,Constants.VALIDATION_JOB_STATUS_ONGOING));
|
|
}
|
|
|
|
//TODO fix status with new validator version
|
|
if(retJobs.getJobs() != null){
|
|
for(StoredJob job :retJobs.getJobs()){
|
|
if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) {
|
|
job.setValidationStatus("ongoing");
|
|
} else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50)
|
|
|| (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50)
|
|
|| (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) {
|
|
job.setValidationStatus("successful");
|
|
} else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50))
|
|
|| (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50)
|
|
|| (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50) ) {
|
|
job.setValidationStatus("failed");
|
|
}
|
|
|
|
}
|
|
}
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
List<StoredJob> jobs = new ArrayList<>();
|
|
|
|
List<Job> cj = crisJobs.getJobs(user);
|
|
for (Job job : cj) {
|
|
|
|
StoredJob sj = converJobToStoredJob(job);
|
|
|
|
|
|
// filter out entries based on 'validationStatus'
|
|
if ("all".equals(validationStatus)) {
|
|
jobs.add(sj);
|
|
} else {
|
|
if (sj.getValidationStatus().equals(validationStatus)) {
|
|
jobs.add(sj);
|
|
}
|
|
}
|
|
}
|
|
|
|
// add to CRIS Jan jobs all other jobs
|
|
if (retJobs.getJobs() != null) {
|
|
jobs.addAll(retJobs.getJobs());
|
|
}
|
|
// set all jobs back to retJobs
|
|
retJobs.setJobs(jobs);
|
|
|
|
// fix number of jobs
|
|
if (Boolean.parseBoolean(includeJobsTotal)) {
|
|
retJobs.setTotalJobs(retJobs.getTotalJobs() + crisJobs.getJobs(user).size());
|
|
retJobs.setTotalJobsSuccessful(retJobs.getTotalJobsSuccessful() + crisJobs.getJobs(user, Job.Status.SUCCESSFUL.getKey()).size());
|
|
retJobs.setTotalJobsFailed(retJobs.getTotalJobsFailed() + crisJobs.getJobs(user, Job.Status.FAILED.getKey()).size());
|
|
retJobs.setTotalJobsOngoing(retJobs.getTotalJobsOngoing() + crisJobs.getJobs(user, Job.Status.ONGOING.getKey()).size());
|
|
}
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
return retJobs;
|
|
|
|
}
|
|
|
|
private int getJobsTotalNumberOfUser(String user, String jobType, String validationStatus) throws ValidatorServiceException {
|
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
|
}
|
|
|
|
@Override
|
|
public int getJobsOfUserPerValidationStatus(String user,
|
|
String jobType,
|
|
String validationStatus) throws JSONException {
|
|
LOGGER.debug("Getting job with validation status : " + validationStatus);
|
|
try {
|
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
|
} catch (ValidatorServiceException e) {
|
|
LOGGER.error(e);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public StoredJob getJobSummary(String jobId,
|
|
String groupBy) throws JSONException {
|
|
LOGGER.debug("Getting job summary with id : " + jobId);
|
|
StoredJob job = null;
|
|
try {
|
|
job = getValidationService().getStoredJob(Integer.parseInt(jobId), groupBy);
|
|
} catch (ValidatorServiceException e) {
|
|
LOGGER.error(e);
|
|
}
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
if (job == null) {
|
|
// not a good way to do it but Job id field is string
|
|
List<Job> cJobs = crisJobs.getAll().stream().filter(j -> j.getId().hashCode() == Integer.parseInt(jobId)).collect(Collectors.toList());
|
|
if (!cJobs.isEmpty()) {
|
|
job = converJobToStoredJob(cJobs.get(0));
|
|
}
|
|
}
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
return job;
|
|
}
|
|
|
|
private StoredJob converJobToStoredJob(Job job) {
|
|
StoredJob sj = new StoredJob();
|
|
sj.setId(job.getId().hashCode());
|
|
sj.setValidationStatus(job.getStatus());
|
|
if (job.getDateFinished() != null) {
|
|
sj.setEnded(job.getDateFinished().toString());
|
|
sj.setDuration(new Date(job.getDateFinished().getTime() - job.getDateStarted().getTime()).toString());
|
|
} else {
|
|
sj.setEnded("-");
|
|
sj.setDuration("-");
|
|
}
|
|
sj.setStarted(job.getDateStarted().toString());
|
|
|
|
sj.setUserEmail(job.getUser());
|
|
sj.setCris(true);
|
|
sj.setBaseUrl(job.getUrl());
|
|
sj.setJobType("CRIS Validation");
|
|
|
|
// TODO: status
|
|
sj.setValidationStatus(job.getStatus());
|
|
sj.setUsageJobStatus(job.getStatus());
|
|
sj.setContentJobStatus(job.getStatus());
|
|
|
|
// TODO: scores
|
|
// sj.setFilteredScores();
|
|
sj.setContentJobScore(job.getScore());
|
|
sj.setUsageJobScore(job.getScore());
|
|
|
|
sj.setValidationType("CRIS Validation");
|
|
|
|
return sj;
|
|
}
|
|
|
|
}
|