StatisticsDao { public Map<String, Map<String, WorkflowDefinitionStatistics>> getWorkflowDefinitionStatistics(String type, DateTime createdAfter, DateTime createdBefore, DateTime modifiedAfter, DateTime modifiedBefore) { String executorGroup = executorInfo.getExecutorGroup(); List<Object> argsList = new ArrayList<>(); argsList.addAll(asList(executorGroup, type)); StringBuilder whereBuilder = new StringBuilder(); if (createdAfter != null) { whereBuilder.append(" and created >= ?"); argsList.add(createdAfter.toDate()); } if (createdBefore != null) { whereBuilder.append(" and created < ?"); argsList.add(createdBefore.toDate()); } if (modifiedAfter != null) { whereBuilder.append(" and modified >= ?"); argsList.add(modifiedAfter.toDate()); } if (modifiedBefore != null) { whereBuilder.append(" and modified < ?"); argsList.add(modifiedBefore.toDate()); } String where = whereBuilder.toString(); StringBuilder sqlBuilder = new StringBuilder("select state, status, count(*) all_instances,") .append(" count(case when next_activation < current_timestamp then 1 else null end) queued_instances") .append(" from nflow_workflow where executor_group = ? and type = ?").append(where).append(" group by state, status"); String query = sqlBuilder.toString(); Object[] argsArray = argsList.toArray(new Object[argsList.size()]); final Map<String, Map<String, WorkflowDefinitionStatistics>> stats = new LinkedHashMap<>(); jdbc.query(query, argsArray, new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { String state = rs.getString("state"); Map<String, WorkflowDefinitionStatistics> stateStats = stats.get(state); if (stateStats == null) { stateStats = new LinkedHashMap<>(); stats.put(state, stateStats); } String status = rs.getString("status"); WorkflowDefinitionStatistics statusStats = new WorkflowDefinitionStatistics(); statusStats.allInstances = rs.getLong("all_instances"); statusStats.queuedInstances = rs.getLong("queued_instances"); stateStats.put(status, statusStats); } }); return stats; } @Inject  StatisticsDao(@NFlow JdbcTemplate jdbcTemplate, ExecutorDao executorDao); Statistics getQueueStatistics(); Map<String, Map<String, WorkflowDefinitionStatistics>> getWorkflowDefinitionStatistics(String type,
      DateTime createdAfter, DateTime createdBefore, DateTime modifiedAfter, DateTime modifiedBefore);  }
@Test public void getWorkflowDefinitionStatisticsWorks() { WorkflowInstance i1 = constructWorkflowInstanceBuilder().setNextActivation(null).setStatus(created).build(); long id = instanceDao.insertWorkflowInstance(i1); Map<String, Map<String, WorkflowDefinitionStatistics>> stats = statisticsDao.getWorkflowDefinitionStatistics(i1.type, null, null, null, null); Map<String, WorkflowDefinitionStatistics> stateStats = stats.get("CreateLoan"); assertThat(stateStats.get(executing.name()), is(nullValue())); assertThat(stateStats.get(inProgress.name()).queuedInstances, is(2L)); assertThat(stateStats.get(inProgress.name()).allInstances, is(2L)); assertThat(stateStats.get(created.name()).queuedInstances, is(0L)); assertThat(stateStats.get(created.name()).allInstances, is(1L)); WorkflowInstance i2 = new WorkflowInstance.Builder().setId(id).setNextActivation(now().minusDays(1)).build(); instanceDao.updateNotRunningWorkflowInstance(i2); stats = statisticsDao.getWorkflowDefinitionStatistics(i1.type, null, null, null, null); stateStats = stats.get("CreateLoan"); assertThat(stateStats.get(executing.name()), is(nullValue())); assertThat(stateStats.get(inProgress.name()).queuedInstances, is(2L)); assertThat(stateStats.get(inProgress.name()).allInstances, is(2L)); assertThat(stateStats.get(created.name()).queuedInstances, is(1L)); assertThat(stateStats.get(created.name()).allInstances, is(1L)); }
@Test public void getStatisticsWorksWithCreatedLimits() { WorkflowInstance i1 = constructWorkflowInstanceBuilder().setNextActivation(null).build(); instanceDao.insertWorkflowInstance(i1); Map<String, Map<String, WorkflowDefinitionStatistics>> stats = statisticsDao.getWorkflowDefinitionStatistics(i1.type, now() .plusDays(1), now().plusDays(2), null, null); assertThat(stats.size(), is(0)); }
@Test public void getStatisticsWorksWithModifiedLimits() { WorkflowInstance i1 = constructWorkflowInstanceBuilder().setNextActivation(null).build(); instanceDao.insertWorkflowInstance(i1); Map<String, Map<String, WorkflowDefinitionStatistics>> stats = statisticsDao.getWorkflowDefinitionStatistics(i1.type, null, null, now().plusDays(1), now().plusDays(2)); assertThat(stats.size(), is(0)); }
@Test public void getStatisticsWorksWithCreatedAndModifiedLimits() { WorkflowInstance i1 = constructWorkflowInstanceBuilder().setNextActivation(null).build(); instanceDao.insertWorkflowInstance(i1); Map<String, Map<String, WorkflowDefinitionStatistics>> stats = statisticsDao.getWorkflowDefinitionStatistics(i1.type, now() .minusDays(1), now().plusDays(1), now().minusDays(1), now().plusDays(1)); assertThat(stats.size(), is(1)); }