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