Project

General

Profile

1
package eu.dnetlib.repo.manager.service.controllers;
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.shared.Constants;
7
import eu.dnetlib.repo.manager.shared.JobsOfUser;
8
import gr.uoa.di.driver.util.ServiceLocator;
9
import io.swagger.annotations.ApiParam;
10
import org.apache.log4j.Logger;
11
import org.json.JSONException;
12
import org.springframework.security.access.prepost.PreAuthorize;
13
import org.springframework.stereotype.Component;
14
import org.springframework.web.bind.annotation.RequestParam;
15

    
16
import javax.annotation.Resource;
17

    
18
@Component
19
public class MonitorApiImpl implements MonitorApi {
20

    
21
    @Resource(name = "validatorServiceLocator")
22
    private ServiceLocator<ValidatorService> validatorServiceLocator;
23

    
24
    private ValidatorService getValidationService() {
25
        return this.validatorServiceLocator.getService();
26
    }
27

    
28
    public ServiceLocator<ValidatorService> getValidatorServiceLocator() {
29
        return validatorServiceLocator;
30
    }
31

    
32
    public void setValidatorServiceLocator(ServiceLocator<ValidatorService> validatorServiceLocator) {
33
        this.validatorServiceLocator = validatorServiceLocator;
34
    }
35

    
36

    
37
    private static final Logger LOGGER = Logger
38
            .getLogger(MonitorApiImpl.class);
39

    
40
    @Override
41
    @PreAuthorize("hasRole('ROLE_USER')")
42
    public JobsOfUser getJobsOfUser(@RequestParam("user") @ApiParam(value = "User email", required = true) String user,
43
                                    @RequestParam(value = "jobType", required = false)
44
                                    @ApiParam(value = "Equals to filter job type on validation history page") String jobType,
45
                                    @RequestParam("offset") @ApiParam(value = "Page number", required = true) String offset,
46
                                    @RequestParam(value = "limit", required = false,defaultValue = "10") @ApiParam(value = "Null value") String limit,
47
                                    @RequestParam(value = "dateFrom", required = false) @ApiParam(value = "Null value") String dateFrom,
48
                                    @RequestParam(value = "dateTo", required = false) @ApiParam(value = "Null value") String dateTo,
49
                                    @RequestParam("validationStatus") @ApiParam(value = "Equals to filter validation jobs", required = false) String validationStatus,
50
                                    @RequestParam("includeJobsTotal") @ApiParam(value = "Always true", required = true) String includeJobsTotal) throws JSONException, ValidatorServiceException {
51

    
52
        LOGGER.debug("Getting jobs of user : " + user);
53
        LOGGER.debug(user + "/" + jobType + "/" + offset + "/" + dateFrom + "/" + dateTo + "/" + validationStatus + "/" + includeJobsTotal);
54
        JobsOfUser retJobs = new JobsOfUser();
55
        retJobs.setJobs(getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset),
56
                Integer.parseInt(limit), dateFrom, dateTo, validationStatus));
57
        if (Boolean.parseBoolean(includeJobsTotal)) {
58
            retJobs.setTotalJobs(this.getJobsTotalNumberOfUser(user, jobType, null));
59
            retJobs.setTotalJobsSuccessful(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_SUCCESSFUL));
60
            retJobs.setTotalJobsFailed(this.getJobsTotalNumberOfUser(user, jobType, Constants.VALIDATION_JOB_STATUS_FAILED));
61
            retJobs.setTotalJobsOngoing(this.getJobsTotalNumberOfUser(user, jobType,Constants.VALIDATION_JOB_STATUS_ONGOING));
62
        }
63

    
64
        //TODO fix status with new validator version
65
        if(retJobs.getJobs() != null){
66
            for(StoredJob job :retJobs.getJobs()){
67
                if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) {
68
                    job.setValidationStatus("ongoing");
69
                } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50)
70
                        || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50)
71
                        || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) {
72
                    job.setValidationStatus("successful");
73
                } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50))
74
                        || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50)
75
                        || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50) ) {
76
                    job.setValidationStatus("failed");
77
                }
78

    
79
            }
80
        }
81

    
82

    
83
        return retJobs;
84

    
85
    }
86

    
87
    private int getJobsTotalNumberOfUser(String user, String jobType, String validationStatus) throws ValidatorServiceException {
88
        return  getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
89
    }
90

    
91
    @Override
92
    @PreAuthorize("hasRole('ROLE_USER')")
93
    public int getJobsOfUserPerValidationStatus(String user,
94
                                                String jobType,
95
                                                String validationStatus) throws JSONException {
96
        LOGGER.debug("Getting job with validation status : " + validationStatus);
97
        try {
98
            return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus);
99
        } catch (ValidatorServiceException e) {
100
            e.printStackTrace();
101
        }
102
        return 0;
103
    }
104

    
105
    @Override
106
    public StoredJob getJobSummary(String jobId,
107
                                   String groupBy) throws JSONException {
108
        LOGGER.debug("Getting job summary with id : " + jobId);
109
        try {
110
            return getValidationService().getStoredJob(Integer.parseInt(jobId), groupBy);
111
        } catch (ValidatorServiceException e) {
112
            e.printStackTrace();
113
        }
114
        return null;
115
    }
116

    
117
}
(7-7/19)