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();
}
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();
}
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;
}
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);
}
}
}
}
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;
}
Aggregations