use of org.wso2.carbon.bpmn.rest.model.stats.ProcessInstanceAverageInfo 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;
}
Aggregations