Search in sources :

Example 1 with JobsListQuery

use of org.opencastproject.index.service.resources.list.query.JobsListQuery in project opencast by opencast.

the class JobsListProviderTest method testWorkflowListName.

@Test
public void testWorkflowListName() throws ListProviderException, WorkflowDatabaseException {
    ResourceListQuery query = new JobsListQuery();
    assertEquals(workflowDefinitions.size(), jobsListProvider.getList(JobsListProvider.LIST_WORKFLOW, query, null).size());
    for (Entry<String, String> entry : jobsListProvider.getList(JobsListProvider.LIST_WORKFLOW, query, null).entrySet()) {
        boolean match = false;
        for (WorkflowDefinition wfD : workflowDefinitions) {
            if (StringUtils.equals(wfD.getId(), entry.getKey()) && StringUtils.equals(wfD.getTitle(), entry.getValue())) {
                match = true;
                break;
            }
        }
        assertTrue(match);
    }
}
Also used : WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) ResourceListQuery(org.opencastproject.index.service.resources.list.api.ResourceListQuery) JobsListQuery(org.opencastproject.index.service.resources.list.query.JobsListQuery) Test(org.junit.Test)

Example 2 with JobsListQuery

use of org.opencastproject.index.service.resources.list.query.JobsListQuery in project opencast by opencast.

the class JobsListProviderTest method testStatusListName.

@Test
public void testStatusListName() throws ListProviderException, WorkflowDatabaseException {
    ResourceListQuery query = new JobsListQuery();
    assertEquals(4, jobsListProvider.getList(JobsListProvider.LIST_STATUS, query, null).size());
    for (Entry<String, String> entry : jobsListProvider.getList(JobsListProvider.LIST_STATUS, query, null).entrySet()) {
        try {
            Job.Status.valueOf(entry.getKey());
        } catch (IllegalArgumentException ex) {
            fail("Can not parse job state");
        }
        assertTrue(StringUtils.startsWith(entry.getValue(), JobsListProvider.JOB_STATUS_FILTER_PREFIX));
    }
}
Also used : ResourceListQuery(org.opencastproject.index.service.resources.list.api.ResourceListQuery) JobsListQuery(org.opencastproject.index.service.resources.list.query.JobsListQuery) Test(org.junit.Test)

Example 3 with JobsListQuery

use of org.opencastproject.index.service.resources.list.query.JobsListQuery in project opencast by opencast.

the class JobEndpoint method getJobs.

@GET
@Path("jobs.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(description = "Returns the list of active jobs", name = "jobs", restParameters = { @RestParameter(name = "limit", description = "The maximum number of items to return per page", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "offset", description = "The offset", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "filter", description = "Filter results by hostname, status or free text query", isRequired = false, type = RestParameter.Type.STRING), @RestParameter(name = "sort", description = "The sort order. May include any of the following: CREATOR, OPERATION, PROCESSINGHOST, STATUS, STARTED, SUBMITTED or TYPE. " + "The suffix must be :ASC for ascending or :DESC for descending sort order (e.g. OPERATION:DESC)", isRequired = false, type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "Returns the list of active jobs from Opencast", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The list of jobs as JSON")
public Response getJobs(@QueryParam("limit") final int limit, @QueryParam("offset") final int offset, @QueryParam("filter") final String filter, @QueryParam("sort") final String sort) {
    JobsListQuery query = new JobsListQuery();
    EndpointUtil.addRequestFiltersToQuery(filter, query);
    query.setLimit(limit);
    query.setOffset(offset);
    String fHostname = null;
    if (query.getHostname().isSome())
        fHostname = StringUtils.trimToNull(query.getHostname().get());
    String fStatus = null;
    if (query.getStatus().isSome())
        fStatus = StringUtils.trimToNull(query.getStatus().get());
    String fFreeText = null;
    if (query.getFreeText().isSome())
        fFreeText = StringUtils.trimToNull(query.getFreeText().get());
    List<Job> jobs = new ArrayList<>();
    try {
        for (Job job : serviceRegistry.getActiveJobs()) {
            // filter workflow jobs
            if (StringUtils.equals(WorkflowService.JOB_TYPE, job.getJobType()) && StringUtils.equals("START_WORKFLOW", job.getOperation()))
                continue;
            // filter by hostname
            if (fHostname != null && !StringUtils.equalsIgnoreCase(job.getProcessingHost(), fHostname))
                continue;
            // filter by status
            if (fStatus != null && !StringUtils.equalsIgnoreCase(job.getStatus().toString(), fStatus))
                continue;
            // fitler by user free text
            if (fFreeText != null && !StringUtils.equalsIgnoreCase(job.getProcessingHost(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getJobType(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getOperation(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getCreator(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getStatus().toString(), fFreeText) && !StringUtils.equalsIgnoreCase(Long.toString(job.getId()), fFreeText) && (job.getRootJobId() != null && !StringUtils.equalsIgnoreCase(Long.toString(job.getRootJobId()), fFreeText)))
                continue;
            jobs.add(job);
        }
    } catch (ServiceRegistryException ex) {
        logger.error("Failed to retrieve jobs list from service registry.", ex);
        return RestUtil.R.serverError();
    }
    JobSort sortKey = JobSort.SUBMITTED;
    boolean ascending = true;
    if (StringUtils.isNotBlank(sort)) {
        try {
            SortCriterion sortCriterion = RestUtils.parseSortQueryParameter(sort).iterator().next();
            sortKey = JobSort.valueOf(sortCriterion.getFieldName().toUpperCase());
            ascending = SearchQuery.Order.Ascending == sortCriterion.getOrder() || SearchQuery.Order.None == sortCriterion.getOrder();
        } catch (WebApplicationException ex) {
            logger.warn("Failed to parse sort criterion \"{}\", invalid format.", sort);
        } catch (IllegalArgumentException ex) {
            logger.warn("Can not apply sort criterion \"{}\", no field with this name.", sort);
        }
    }
    JobComparator comparator = new JobComparator(sortKey, ascending);
    Collections.sort(jobs, comparator);
    List<JValue> json = getJobsAsJSON(new SmartIterator(query.getLimit().getOrElse(0), query.getOffset().getOrElse(0)).applyLimitAndOffset(jobs));
    return RestUtils.okJsonList(json, offset, limit, jobs.size());
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SmartIterator(org.opencastproject.util.SmartIterator) ArrayList(java.util.ArrayList) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) SortCriterion(org.opencastproject.matterhorn.search.SortCriterion) JValue(com.entwinemedia.fn.data.json.JValue) JobsListQuery(org.opencastproject.index.service.resources.list.query.JobsListQuery) Job(org.opencastproject.job.api.Job) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

JobsListQuery (org.opencastproject.index.service.resources.list.query.JobsListQuery)3 Test (org.junit.Test)2 ResourceListQuery (org.opencastproject.index.service.resources.list.api.ResourceListQuery)2 JValue (com.entwinemedia.fn.data.json.JValue)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Job (org.opencastproject.job.api.Job)1 SortCriterion (org.opencastproject.matterhorn.search.SortCriterion)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 SmartIterator (org.opencastproject.util.SmartIterator)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)1