Search in sources :

Example 1 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project herd by FINRAOS.

the class ActivitiServiceTest method getHistoricProcessInstancesCountByStatusAndProcessDefinitionKeys.

@Test
public void getHistoricProcessInstancesCountByStatusAndProcessDefinitionKeys() {
    JobStatusEnum jobStatus = JobStatusEnum.RUNNING;
    Collection<String> processDefinitionKeys = new ArrayList<>();
    DateTime startTime = new DateTime();
    DateTime endTime = new DateTime();
    HistoricProcessInstanceQuery historicProcessInstanceQuery = mock(HistoricProcessInstanceQuery.class);
    when(activitiHistoryService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys))).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.unfinished()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.startedAfter(startTime.toDate())).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.finishedBefore(endTime.toDate())).thenReturn(historicProcessInstanceQuery);
    long expectedResult = 1234l;
    when(historicProcessInstanceQuery.count()).thenReturn(expectedResult);
    long actualResult = activitiService.getHistoricProcessInstancesCountByStatusAndProcessDefinitionKeys(jobStatus, processDefinitionKeys, startTime, endTime);
    assertEquals(expectedResult, actualResult);
    InOrder inOrder = inOrder(historicProcessInstanceQuery);
    inOrder.verify(historicProcessInstanceQuery).processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys));
    inOrder.verify(historicProcessInstanceQuery).unfinished();
    inOrder.verify(historicProcessInstanceQuery).startedAfter(startTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).finishedBefore(endTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).count();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) JobStatusEnum(org.finra.herd.model.api.xml.JobStatusEnum) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project herd by FINRAOS.

the class ActivitiServiceTest method testGetHistoricProcessInstancesByStatusAndProcessDefinitionKeysWhenStatusNotSpecified.

@Test
public void testGetHistoricProcessInstancesByStatusAndProcessDefinitionKeysWhenStatusNotSpecified() {
    JobStatusEnum jobStatus = null;
    Collection<String> processDefinitionKeys = new ArrayList<>();
    DateTime startTime = new DateTime();
    DateTime endTime = new DateTime();
    HistoricProcessInstanceQuery historicProcessInstanceQuery = mock(HistoricProcessInstanceQuery.class);
    when(activitiHistoryService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys))).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.unfinished()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.startedAfter(startTime.toDate())).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.finishedBefore(endTime.toDate())).thenReturn(historicProcessInstanceQuery);
    List<HistoricProcessInstance> expectedHistoricProcessInstances = new ArrayList<>();
    when(historicProcessInstanceQuery.list()).thenReturn(expectedHistoricProcessInstances);
    List<HistoricProcessInstance> actualHistoricProcessInstance = activitiService.getHistoricProcessInstancesByStatusAndProcessDefinitionKeys(jobStatus, processDefinitionKeys, startTime, endTime);
    assertSame(expectedHistoricProcessInstances, actualHistoricProcessInstance);
    InOrder inOrder = inOrder(historicProcessInstanceQuery);
    inOrder.verify(historicProcessInstanceQuery).processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys));
    inOrder.verify(historicProcessInstanceQuery).startedAfter(startTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).finishedBefore(endTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).list();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) JobStatusEnum(org.finra.herd.model.api.xml.JobStatusEnum) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class ProcessStatisticsService method getAvgTimeDurationForCompletedProcesses.

/**
 * Get the average time duration of completed processes
 *
 * @return list with the completed processes and the average time duration taken for each process
 */
@GET
@Path("/process-instances/duration/average")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public ResponseHolder getAvgTimeDurationForCompletedProcesses() {
    ResponseHolder response = new ResponseHolder();
    List<Object> list = new ArrayList<>();
    HistoryService historyService = BPMNOSGIService.getHistoryService();
    RepositoryService repositoryService = BPMNOSGIService.getRepositoryService();
    List<ProcessDefinition> deployedProcessList = repositoryService.createProcessDefinitionQuery().processDefinitionTenantId(getTenantIdStr()).list();
    for (ProcessDefinition instance : deployedProcessList) {
        ProcessInstanceAverageInfo bpmnProcessInstance = new ProcessInstanceAverageInfo();
        bpmnProcessInstance.setProcessDefinitionId(instance.getId());
        double totalTime = 0;
        double averageTime;
        String processDefinitionID = instance.getId();
        HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery().processInstanceTenantId(getTenantIdStr()).processDefinitionId(processDefinitionID).finished();
        long instanceCount = historicProcessInstanceQuery.count();
        if (instanceCount != 0) {
            List<HistoricProcessInstance> instanceList = historicProcessInstanceQuery.list();
            for (HistoricProcessInstance completedProcess : instanceList) {
                double timeDurationOfTask = completedProcess.getDurationInMillis();
                double timeInMins = timeDurationOfTask / (1000 * 60);
                totalTime += timeInMins;
            }
            averageTime = totalTime / instanceCount;
            bpmnProcessInstance.setAverageTimeForCompletion(averageTime);
            list.add(bpmnProcessInstance);
        }
    }
    response.setData(list);
    return response;
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) HistoryService(org.activiti.engine.HistoryService) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ResponseHolder(org.wso2.carbon.bpmn.rest.model.stats.ResponseHolder) ProcessInstanceAverageInfo(org.wso2.carbon.bpmn.rest.model.stats.ProcessInstanceAverageInfo) RepositoryService(org.activiti.engine.RepositoryService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class BPMNDeploymentService method deleteInstances.

private void deleteInstances(BPMNDeletableInstances bpmnDeletableInstances, ProcessEngine processEngine) {
    List<HistoricProcessInstance> activeHistoricProcessInstance = bpmnDeletableInstances.getActiveHistoricProcessInstance();
    for (HistoricProcessInstance instance : activeHistoricProcessInstance) {
        String instanceId = instance.getId();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processInstanceTenantId(bpmnDeletableInstances.getTenantId().toString()).processInstanceId(instanceId).list();
        if (!processInstances.isEmpty()) {
            runtimeService.deleteProcessInstance(instance.getId(), "Deleted by user: " + bpmnDeletableInstances.getTenantId());
        }
    }
    List<String> completedProcessDefinitionIds = bpmnDeletableInstances.getCompletedProcessDefinitionIds();
    for (String processId : completedProcessDefinitionIds) {
        HistoricProcessInstanceQuery runtimeQuery = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(bpmnDeletableInstances.getTenantId().toString()).includeProcessVariables().finished().processDefinitionId(processId);
        int completedProcessInstanceCount = (int) runtimeQuery.count();
        if (completedProcessInstanceCount > 0) {
            List<HistoricProcessInstance> instances = runtimeQuery.listPage(0, completedProcessInstanceCount + 1);
            HistoryService historyService = processEngine.getHistoryService();
            for (HistoricProcessInstance instance : instances) {
                String instanceId = instance.getId();
                historyService.deleteHistoricProcessInstance(instanceId);
            }
        }
    }
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) RuntimeService(org.activiti.engine.RuntimeService) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) HistoryService(org.activiti.engine.HistoryService) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance)

Example 5 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class BPMNDeploymentService method constructBPMNInstancesByProcessID.

private boolean constructBPMNInstancesByProcessID(BPMNDeletableInstances bpmnDeletableInstances, String processId, Integer tenantId, ProcessEngine processEngine) {
    // first going to get the instances list of unfinished instances
    HistoricProcessInstanceQuery runtimeQuery = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(tenantId.toString()).includeProcessVariables().unfinished().processDefinitionId(processId);
    int processInstanceCount = (int) runtimeQuery.count();
    bpmnDeletableInstances.setActiveInstanceCount(processInstanceCount);
    if (bpmnDeletableInstances.getActiveInstanceCount() > maximumDeleteCount) {
        return false;
    }
    if (log.isDebugEnabled()) {
        log.debug("Process ID has un completed instances count : " + processInstanceCount);
    }
    if (processInstanceCount > 0) {
        List<HistoricProcessInstance> instances = runtimeQuery.listPage(0, processInstanceCount + 1);
        bpmnDeletableInstances.setActiveProcessInstance(instances);
    }
    // next get the count of finished instance for the same process id
    runtimeQuery = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(tenantId.toString()).includeProcessVariables().finished().processDefinitionId(processId);
    int completedProcessInstanceCount = (int) runtimeQuery.count();
    if ((completedProcessInstanceCount + bpmnDeletableInstances.getActiveInstanceCount()) > maximumDeleteCount) {
        return false;
    }
    bpmnDeletableInstances.setCompletedInstanceCount(completedProcessInstanceCount);
    bpmnDeletableInstances.addCompletedProcessDefinitionIds(processId);
    if (log.isDebugEnabled()) {
        log.debug("Process ID has completed instances count : " + completedProcessInstanceCount);
    }
    return true;
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance)

Aggregations

HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)36 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)29 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)8 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)7 InOrder (org.mockito.InOrder)7 HistoryService (org.activiti.engine.HistoryService)6 RuntimeService (org.activiti.engine.RuntimeService)6 Deployment (org.activiti.engine.test.Deployment)6 JobStatusEnum (org.finra.herd.model.api.xml.JobStatusEnum)6 DateTime (org.joda.time.DateTime)6 BPMNInstance (org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance)6 HashSet (java.util.HashSet)3 ProcessEngine (org.activiti.engine.ProcessEngine)3 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)3 ProcessInstanceQuery (org.activiti.engine.runtime.ProcessInstanceQuery)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2