Search in sources :

Example 1 with WorkflowStatistics

use of org.opencastproject.workflow.api.WorkflowStatistics in project opencast by opencast.

the class WorkflowServiceSolrIndex method getStatistics.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.impl.WorkflowServiceIndex#getStatistics()
 */
@Override
public WorkflowStatistics getStatistics() throws WorkflowDatabaseException {
    long total = 0;
    long paused = 0;
    long failed = 0;
    long failing = 0;
    long instantiated = 0;
    long running = 0;
    long stopped = 0;
    long succeeded = 0;
    WorkflowStatistics stats = new WorkflowStatistics();
    // Get all definitions and then query for the numbers and the current operation per definition
    try {
        String orgId = securityService.getOrganization().getId();
        StringBuilder queryString = new StringBuilder().append(ORG_KEY).append(":").append(escapeQueryChars(orgId));
        appendSolrAuthFragment(queryString, Permissions.Action.WRITE.toString());
        SolrQuery solrQuery = new SolrQuery(queryString.toString());
        solrQuery.addFacetField(WORKFLOW_DEFINITION_KEY);
        solrQuery.addFacetField(OPERATION_KEY);
        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        QueryResponse response = solrServer.query(solrQuery);
        FacetField templateFacet = response.getFacetField(WORKFLOW_DEFINITION_KEY);
        FacetField operationFacet = response.getFacetField(OPERATION_KEY);
        // For every template and every operation
        if (templateFacet != null && templateFacet.getValues() != null) {
            for (Count template : templateFacet.getValues()) {
                WorkflowDefinitionReport templateReport = new WorkflowDefinitionReport();
                templateReport.setId(template.getName());
                long templateTotal = 0;
                long templatePaused = 0;
                long templateFailed = 0;
                long templateFailing = 0;
                long templateInstantiated = 0;
                long templateRunning = 0;
                long templateStopped = 0;
                long templateSucceeded = 0;
                if (operationFacet != null && operationFacet.getValues() != null) {
                    for (Count operation : operationFacet.getValues()) {
                        OperationReport operationReport = new OperationReport();
                        operationReport.setId(operation.getName());
                        StringBuilder baseSolrQuery = new StringBuilder().append(ORG_KEY).append(":").append(escapeQueryChars(orgId));
                        appendSolrAuthFragment(baseSolrQuery, Permissions.Action.WRITE.toString());
                        solrQuery = new SolrQuery(baseSolrQuery.toString());
                        solrQuery.addFacetField(STATE_KEY);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.INSTANTIATED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.PAUSED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.RUNNING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.STOPPED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.SUCCEEDED);
                        solrQuery.addFilterQuery(WORKFLOW_DEFINITION_KEY + ":" + template.getName());
                        solrQuery.addFilterQuery(OPERATION_KEY + ":" + operation.getName());
                        solrQuery.setFacetMinCount(0);
                        solrQuery.setFacet(true);
                        response = solrServer.query(solrQuery);
                        // Add the states
                        FacetField stateFacet = response.getFacetField(STATE_KEY);
                        for (Count stateValue : stateFacet.getValues()) {
                            WorkflowState state = WorkflowState.valueOf(stateValue.getName().toUpperCase());
                            templateTotal += stateValue.getCount();
                            total += stateValue.getCount();
                            switch(state) {
                                case FAILED:
                                    operationReport.setFailed(stateValue.getCount());
                                    templateFailed += stateValue.getCount();
                                    failed += stateValue.getCount();
                                    break;
                                case FAILING:
                                    operationReport.setFailing(stateValue.getCount());
                                    templateFailing += stateValue.getCount();
                                    failing += stateValue.getCount();
                                    break;
                                case INSTANTIATED:
                                    operationReport.setInstantiated(stateValue.getCount());
                                    templateInstantiated += stateValue.getCount();
                                    instantiated += stateValue.getCount();
                                    break;
                                case PAUSED:
                                    operationReport.setPaused(stateValue.getCount());
                                    templatePaused += stateValue.getCount();
                                    paused += stateValue.getCount();
                                    break;
                                case RUNNING:
                                    operationReport.setRunning(stateValue.getCount());
                                    templateRunning += stateValue.getCount();
                                    running += stateValue.getCount();
                                    break;
                                case STOPPED:
                                    operationReport.setStopped(stateValue.getCount());
                                    templateStopped += stateValue.getCount();
                                    stopped += stateValue.getCount();
                                    break;
                                case SUCCEEDED:
                                    operationReport.setFinished(stateValue.getCount());
                                    templateSucceeded += stateValue.getCount();
                                    succeeded += stateValue.getCount();
                                    break;
                                default:
                                    throw new IllegalStateException("State '" + state + "' is not handled");
                            }
                        }
                        templateReport.getOperations().add(operationReport);
                    }
                }
                // Update the template statistics
                templateReport.setTotal(templateTotal);
                templateReport.setFailed(templateFailed);
                templateReport.setFailing(templateFailing);
                templateReport.setInstantiated(templateInstantiated);
                templateReport.setPaused(templatePaused);
                templateReport.setRunning(templateRunning);
                templateReport.setStopped(templateStopped);
                templateReport.setFinished(templateSucceeded);
                // Add the definition report to the statistics
                stats.getDefinitions().add(templateReport);
            }
        }
    } catch (SolrServerException e) {
        throw new WorkflowDatabaseException(e);
    }
    stats.setTotal(total);
    stats.setFailed(failed);
    stats.setFailing(failing);
    stats.setInstantiated(instantiated);
    stats.setPaused(paused);
    stats.setRunning(running);
    stats.setStopped(stopped);
    stats.setFinished(succeeded);
    return stats;
}
Also used : WorkflowDefinitionReport(org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport) SolrServerException(org.apache.solr.client.solrj.SolrServerException) FacetField(org.apache.solr.client.solrj.response.FacetField) Count(org.apache.solr.client.solrj.response.FacetField.Count) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) SolrQuery(org.apache.solr.client.solrj.SolrQuery) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) OperationReport(org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport.OperationReport)

Example 2 with WorkflowStatistics

use of org.opencastproject.workflow.api.WorkflowStatistics in project opencast by opencast.

the class WorkflowStatisticsTest method testEmptyStatistics.

/**
 * Tests whether the workflow service statistics are gathered correctly while there are no workflows active in the
 * system. Since no workflows are known, not even empty definition reports are to be expected.
 */
@Test
public void testEmptyStatistics() throws Exception {
    WorkflowStatistics stats = service.getStatistics();
    assertEquals(0, stats.getDefinitions().size());
    assertEquals(0, stats.getFailed());
    assertEquals(0, stats.getFailing());
    assertEquals(0, stats.getFinished());
    assertEquals(0, stats.getInstantiated());
    assertEquals(0, stats.getPaused());
    assertEquals(0, stats.getRunning());
    assertEquals(0, stats.getStopped());
    assertEquals(0, stats.getTotal());
}
Also used : WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) Test(org.junit.Test)

Example 3 with WorkflowStatistics

use of org.opencastproject.workflow.api.WorkflowStatistics in project opencast by opencast.

the class WorkflowStatisticsTest method testStatistics.

/**
 * Tests whether the workflow service statistics are gathered correctly.
 */
@Test
public void testStatistics() throws Exception {
    // Start the workflows and advance them in "random" order. With every definition, an instance is started for every
    // operation that is part of the definition. So we end up with an instance per definition and operation, and there
    // are no two workflows that are in the same operation.
    int total = 0;
    int paused = 0;
    int failed = 0;
    int failing = 0;
    int instantiated = 0;
    int running = 0;
    int stopped = 0;
    int succeeded = 0;
    WorkflowStateListener listener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(listener);
    List<WorkflowInstance> instances = new ArrayList<WorkflowInstance>();
    for (WorkflowDefinition def : workflowDefinitions) {
        for (int j = 0; j < def.getOperations().size(); j++) {
            mediaPackage.setIdentifier(new UUIDIdBuilderImpl().createNew());
            instances.add(service.start(def, mediaPackage));
            total++;
            paused++;
        }
    }
    // Wait for all the workflows to go into "paused" state
    synchronized (listener) {
        while (listener.countStateChanges() < WORKFLOW_DEFINITION_COUNT * OPERATION_COUNT) {
            listener.wait();
        }
    }
    service.removeWorkflowListener(listener);
    // Resume all of them, so some will be finished, some won't
    int j = 0;
    for (WorkflowInstance instance : instances) {
        WorkflowListener instanceListener = new IndividualWorkflowListener(instance.getId());
        service.addWorkflowListener(instanceListener);
        for (int k = 0; k <= (j % OPERATION_COUNT - 1); k++) {
            synchronized (instanceListener) {
                service.resume(instance.getId(), null);
                instanceListener.wait();
            }
        }
        j++;
    }
    // TODO: Add failed, failing, stopped etc. workflows as well
    // Get the statistics
    WorkflowStatistics stats = service.getStatistics();
    assertEquals(failed, stats.getFailed());
    assertEquals(failing, stats.getFailing());
    assertEquals(instantiated, stats.getInstantiated());
    assertEquals(succeeded, stats.getFinished());
    assertEquals(paused, stats.getPaused());
    assertEquals(running, stats.getRunning());
    assertEquals(stopped, stats.getStopped());
    assertEquals(total, stats.getTotal());
// TODO: Test the operations
// Make sure they are as expected
// for (WorkflowDefinitionReport report : stats.getDefinitions()) {
// 
// }
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) ArrayList(java.util.ArrayList) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowListener(org.opencastproject.workflow.api.WorkflowListener) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Test(org.junit.Test)

Example 4 with WorkflowStatistics

use of org.opencastproject.workflow.api.WorkflowStatistics in project opencast by opencast.

the class WorkflowStatisticsTest method testStatisticsMarshalling.

/**
 * @throws Exception
 */
@Test
public void testStatisticsMarshalling() throws Exception {
    WorkflowStatistics stats = new WorkflowStatistics();
    stats.setFailed(100);
    stats.setInstantiated(20);
    OperationReport op1 = new OperationReport();
    op1.setId("compose");
    op1.setInstantiated(10);
    op1.setFailing(1);
    List<OperationReport> ops1 = new ArrayList<WorkflowStatistics.WorkflowDefinitionReport.OperationReport>();
    ops1.add(op1);
    WorkflowDefinitionReport def1 = new WorkflowDefinitionReport();
    def1.setFailed(40);
    def1.setInstantiated(10);
    def1.setOperations(ops1);
    def1.setId("def1");
    def1.setOperations(ops1);
    WorkflowDefinitionReport def2 = new WorkflowDefinitionReport();
    def1.setFailed(60);
    def1.setInstantiated(10);
    List<WorkflowDefinitionReport> reports = new ArrayList<WorkflowDefinitionReport>();
    reports.add(def1);
    reports.add(def2);
    stats.setDefinitions(reports);
}
Also used : WorkflowDefinitionReport(org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport) OperationReport(org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport.OperationReport) ArrayList(java.util.ArrayList) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) Test(org.junit.Test)

Example 5 with WorkflowStatistics

use of org.opencastproject.workflow.api.WorkflowStatistics in project opencast by opencast.

the class WorkflowServiceImpl method getBeanStatistics.

private WorkflowStatistics getBeanStatistics() throws WorkflowDatabaseException {
    WorkflowStatistics stats = new WorkflowStatistics();
    long total = 0L;
    long failed = 0L;
    long failing = 0L;
    long instantiated = 0L;
    long paused = 0L;
    long running = 0L;
    long stopped = 0L;
    long finished = 0L;
    Organization organization = securityService.getOrganization();
    try {
        for (Organization org : organizationDirectoryService.getOrganizations()) {
            securityService.setOrganization(org);
            WorkflowStatistics statistics = getStatistics();
            total += statistics.getTotal();
            failed += statistics.getFailed();
            failing += statistics.getFailing();
            instantiated += statistics.getInstantiated();
            paused += statistics.getPaused();
            running += statistics.getRunning();
            stopped += statistics.getStopped();
            finished += statistics.getFinished();
        }
    } finally {
        securityService.setOrganization(organization);
    }
    stats.setTotal(total);
    stats.setFailed(failed);
    stats.setFailing(failing);
    stats.setInstantiated(instantiated);
    stats.setPaused(paused);
    stats.setRunning(running);
    stats.setStopped(stopped);
    stats.setFinished(finished);
    return stats;
}
Also used : Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics)

Aggregations

WorkflowStatistics (org.opencastproject.workflow.api.WorkflowStatistics)5 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 WorkflowDefinitionReport (org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport)2 OperationReport (org.opencastproject.workflow.api.WorkflowStatistics.WorkflowDefinitionReport.OperationReport)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 FacetField (org.apache.solr.client.solrj.response.FacetField)1 Count (org.apache.solr.client.solrj.response.FacetField.Count)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 UUIDIdBuilderImpl (org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)1 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)1 Organization (org.opencastproject.security.api.Organization)1 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)1 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)1 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)1 WorkflowState (org.opencastproject.workflow.api.WorkflowInstance.WorkflowState)1 WorkflowListener (org.opencastproject.workflow.api.WorkflowListener)1 WorkflowStateListener (org.opencastproject.workflow.api.WorkflowStateListener)1