/**
   * 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("/avgDurationToCompleteProcess/")
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public ResponseHolder getAvgTimeDurationForCompletedProcesses() {
    List<ProcessDefinition> deployements =
        BPMNOSGIService.getRepositoryService()
            .createProcessDefinitionQuery()
            .processDefinitionTenantId(str)
            .list();

    ResponseHolder response = new ResponseHolder();
    List list = new ArrayList<>();

    for (ProcessDefinition instance : deployements) {
      CompletedProcesses bpmnProcessInstance = new CompletedProcesses();
      bpmnProcessInstance.setProcessDefinitionId(instance.getId());

      double totalTime = 0;
      double averageTime = 0;
      String processDefinitionID = instance.getId();

      HistoricProcessInstanceQuery historicProcessInstanceQuery =
          BPMNOSGIService.getHistoryService()
              .createHistoricProcessInstanceQuery()
              .processInstanceTenantId(str)
              .processDefinitionId(processDefinitionID)
              .finished();

      long noOfHistoricInstances = historicProcessInstanceQuery.count();

      if (noOfHistoricInstances == 0) {
      } else {
        List<HistoricProcessInstance> instanceList = historicProcessInstanceQuery.list();

        for (HistoricProcessInstance completedProcess : instanceList) {
          double timeDurationOfTask = completedProcess.getDurationInMillis();
          double timeInMins = timeDurationOfTask / (1000 * 60);
          totalTime += timeInMins;
        }
        averageTime = totalTime / noOfHistoricInstances;
        bpmnProcessInstance.setAverageTimeForCompletion(averageTime);
        list.add(bpmnProcessInstance);
      }
    }
    response.setData(list);
    return response;
  }
 /**
  * Convert historic process instances to BPMN process instances
  *
  * @param historicProcessInstanceList List of historic process instances
  * @return BPMNProcessInstance array
  */
 private BPMNProcessInstance[] getBPMNProcessInstances(
     List<HistoricProcessInstance> historicProcessInstanceList) {
   BPMNProcessInstance bpmnProcessInstance;
   List<BPMNProcessInstance> bpmnProcessInstances = new ArrayList<>();
   for (HistoricProcessInstance instance : historicProcessInstanceList) {
     bpmnProcessInstance = new BPMNProcessInstance();
     bpmnProcessInstance.setProcessDefinitionId(instance.getProcessDefinitionId());
     bpmnProcessInstance.setTenantId(instance.getTenantId());
     bpmnProcessInstance.setName(instance.getName());
     bpmnProcessInstance.setInstanceId(instance.getId());
     bpmnProcessInstance.setBusinessKey(instance.getBusinessKey());
     bpmnProcessInstance.setStartTime(instance.getStartTime());
     bpmnProcessInstance.setEndTime(instance.getEndTime());
     bpmnProcessInstance.setDuration(instance.getDurationInMillis());
     bpmnProcessInstance.setStartUserId(instance.getStartUserId());
     bpmnProcessInstance.setStartActivityId(instance.getStartActivityId());
     bpmnProcessInstance.setVariables(formatVariables(instance.getProcessVariables()));
     bpmnProcessInstances.add(bpmnProcessInstance);
   }
   return bpmnProcessInstances.toArray(new BPMNProcessInstance[bpmnProcessInstances.size()]);
 }
 @Test
 @Deployment(resources = {"chapter4/bookorder.bpmn20.xml"})
 public void queryHistoricInstances() {
   String processInstanceID = startAndComplete();
   HistoryService historyService = activitiRule.getHistoryService();
   HistoricProcessInstance historicProcessInstance =
       historyService
           .createHistoricProcessInstanceQuery()
           .processInstanceId(processInstanceID)
           .singleResult();
   assertNotNull(historicProcessInstance);
   assertEquals(processInstanceID, historicProcessInstance.getId());
   System.out.println(
       "history process with definition id "
           + historicProcessInstance.getProcessDefinitionId()
           + ", started at "
           + historicProcessInstance.getStartTime()
           + ", ended at "
           + historicProcessInstance.getEndTime()
           + ", duration was "
           + historicProcessInstance.getDurationInMillis());
 }