Project

General

Profile

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