1
|
package eu.dnetlib.repo.manager.service;
|
2
|
|
3
|
import eu.dnetlib.api.functionality.ValidatorService;
|
4
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
5
|
import eu.dnetlib.domain.functionality.validator.StoredJob;
|
6
|
import eu.dnetlib.repo.manager.domain.Constants;
|
7
|
import eu.dnetlib.repo.manager.domain.JobsOfUser;
|
8
|
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
|
9
|
import gr.uoa.di.driver.util.ServiceLocator;
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.eurocris.openaire.cris.validator.model.Job;
|
12
|
import org.eurocris.openaire.cris.validator.service.MapJobDao;
|
13
|
import org.json.JSONException;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.stereotype.Service;
|
16
|
|
17
|
import javax.annotation.Resource;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.List;
|
20
|
import java.util.stream.Collectors;
|
21
|
|
22
|
@Service("monitorService")
|
23
|
public class MonitorServiceImpl implements MonitorService {
|
24
|
|
25
|
@Autowired
|
26
|
private MapJobDao crisJobs;
|
27
|
|
28
|
@Resource(name = "validatorServiceLocator")
|
29
|
private ServiceLocator<ValidatorService> validatorServiceLocator;
|
30
|
|
31
|
private ValidatorService getValidationService() {
|
32
|
return this.validatorServiceLocator.getService();
|
33
|
}
|
34
|
|
35
|
public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
|
36
|
return validatorServiceLocator;
|
37
|
}
|
38
|
|
39
|
public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
|
40
|
this.validatorServiceLocator = validatorServiceLocator;
|
41
|
}
|
42
|
|
43
|
|
44
|
private static final Logger LOGGER = Logger
|
45
|
.getLogger(MonitorServiceImpl.class);
|
46
|
|
47
|
@Override
|
48
|
public JobsOfUser getJobsOfUser(String user,
|
49
|
String jobType,
|
50
|
String offset,
|
51
|
String limit,
|
52
|
String dateFrom,
|
53
|
String dateTo,
|
54
|
String validationStatus,
|
55
|
String includeJobsTotal) throws JSONException, ValidatorServiceException {
|
56
|
|
57
|
LOGGER.debug("Getting jobs of user : " + user);
|
58
|
LOGGER.debug(user + "/" + jobType + "/" + offset + "/" + dateFrom + "/" + dateTo + "/" + validationStatus + "/" + includeJobsTotal);
|
59
|
|
60
|
/////////////////////////////////////////////////////////////////////////////////////////
|
61
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
62
|
/////////////////////////////////////////////////////////////////////////////////////////
|
63
|
int crisOffset = 0;
|
64
|
int limitWithCris = 0;
|
65
|
if (offset.equals("0")) {
|
66
|
limitWithCris = crisJobs.getJobs(user, validationStatus).size();
|
67
|
} else {
|
68
|
crisOffset = crisJobs.getJobs(user, validationStatus).size();
|
69
|
}
|
70
|
|
71
|
offset = String.valueOf(Math.max(Integer.parseInt(offset) - crisOffset, 0));
|
72
|
limit = String.valueOf(Math.max(Integer.parseInt(limit) - limitWithCris, 0));
|
73
|
/////////////////////////////////////////////////////////////////////////////////////////
|
74
|
/////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
JobsOfUser retJobs = new JobsOfUser();
|
77
|
retJobs.setJobs(getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset),
|
78
|
Integer.parseInt(limit), dateFrom, dateTo, validationStatus));
|
79
|
|
80
|
if (Boolean.parseBoolean(includeJobsTotal)) {
|
81
|
retJobs.setTotalJobs(this.getJobsTotalNumberOfUser(user, jobType, null));
|
82
|
retJobs.setTotalJobsSuccessful(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_SUCCESSFUL));
|
83
|
retJobs.setTotalJobsFailed(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_FAILED));
|
84
|
retJobs.setTotalJobsOngoing(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_ONGOING));
|
85
|
}
|
86
|
|
87
|
//TODO fix status with new validator version
|
88
|
if (retJobs.getJobs() != null) {
|
89
|
for (StoredJob job : retJobs.getJobs()) {
|
90
|
if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) {
|
91
|
job.setValidationStatus("ongoing");
|
92
|
} else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50)
|
93
|
|| (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50)
|
94
|
|| (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) {
|
95
|
job.setValidationStatus("successful");
|
96
|
} else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50))
|
97
|
|| (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50)
|
98
|
|| (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50)) {
|
99
|
job.setValidationStatus("failed");
|
100
|
}
|
101
|
|
102
|
}
|
103
|
}
|
104
|
|
105
|
|
106
|
/////////////////////////////////////////////////////////////////////////////////////////
|
107
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
108
|
/////////////////////////////////////////////////////////////////////////////////////////
|
109
|
if (crisOffset == 0) {
|
110
|
List<StoredJob> jobs = new ArrayList<>();
|
111
|
|
112
|
List<Job> cj = crisJobs.getJobs(user, validationStatus);
|
113
|
for (Job job : cj) {
|
114
|
StoredJob sj = CrisValidatorUtils.convertJobToStoredJob(job);
|
115
|
jobs.add(sj);
|
116
|
|
117
|
// // filter out entries based on 'validationStatus'
|
118
|
// if ("all".equals(validationStatus)) {
|
119
|
// jobs.add(sj);
|
120
|
// } else {
|
121
|
// if (sj.getValidationStatus().equals(validationStatus)) {
|
122
|
// jobs.add(sj);
|
123
|
// }
|
124
|
// }
|
125
|
}
|
126
|
|
127
|
// add to CRIS Jan jobs all other jobs
|
128
|
if (retJobs.getJobs() != null) {
|
129
|
jobs.addAll(retJobs.getJobs());
|
130
|
}
|
131
|
// set all jobs back to retJobs
|
132
|
retJobs.setJobs(jobs);
|
133
|
}
|
134
|
|
135
|
// fix number of jobs
|
136
|
if (Boolean.parseBoolean(includeJobsTotal)) {
|
137
|
retJobs.setTotalJobs(retJobs.getTotalJobs() + crisJobs.getJobs(user).size());
|
138
|
retJobs.setTotalJobsSuccessful(retJobs.getTotalJobsSuccessful() + crisJobs.getJobs(user, Job.Status.SUCCESSFUL.getKey()).size());
|
139
|
retJobs.setTotalJobsFailed(retJobs.getTotalJobsFailed() + crisJobs.getJobs(user, Job.Status.FAILED.getKey()).size());
|
140
|
retJobs.setTotalJobsOngoing(retJobs.getTotalJobsOngoing() + crisJobs.getJobs(user, Job.Status.ONGOING.getKey()).size());
|
141
|
}
|
142
|
/////////////////////////////////////////////////////////////////////////////////////////
|
143
|
/////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
return retJobs;
|
146
|
|
147
|
}
|
148
|
|
149
|
private int getJobsTotalNumberOfUser(String user, String jobType, String validationStatus) throws ValidatorServiceException {
|
150
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public int getJobsOfUserPerValidationStatus(String user,
|
155
|
String jobType,
|
156
|
String validationStatus) throws JSONException {
|
157
|
LOGGER.debug("Getting job with validation status : " + validationStatus);
|
158
|
|
159
|
if (jobType.equalsIgnoreCase("cris")) {
|
160
|
return crisJobs.getJobs(user, validationStatus).size();
|
161
|
}
|
162
|
|
163
|
try {
|
164
|
return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
|
165
|
} catch (ValidatorServiceException e) {
|
166
|
LOGGER.error(e);
|
167
|
}
|
168
|
return 0;
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public StoredJob getJobSummary(String jobId,
|
173
|
String groupBy) throws JSONException {
|
174
|
LOGGER.debug("Getting job summary with id : " + jobId);
|
175
|
StoredJob job = null;
|
176
|
try {
|
177
|
job = getValidationService().getStoredJob(Integer.parseInt(jobId), groupBy);
|
178
|
} catch (ValidatorServiceException e) {
|
179
|
LOGGER.error(e);
|
180
|
}
|
181
|
/////////////////////////////////////////////////////////////////////////////////////////
|
182
|
// FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly //
|
183
|
/////////////////////////////////////////////////////////////////////////////////////////
|
184
|
if (job == null) {
|
185
|
// not a good way to do it but Job id field is string
|
186
|
List<Job> cJobs = crisJobs.getAll().stream().filter(j -> j.getId().hashCode() == Integer.parseInt(jobId)).collect(Collectors.toList());
|
187
|
if (!cJobs.isEmpty()) {
|
188
|
job = CrisValidatorUtils.convertJobToStoredJob(cJobs.get(0));
|
189
|
}
|
190
|
}
|
191
|
/////////////////////////////////////////////////////////////////////////////////////////
|
192
|
/////////////////////////////////////////////////////////////////////////////////////////
|
193
|
return job;
|
194
|
}
|
195
|
}
|